(6-1)shell编程 (1)
来源:互联网 发布:百度作业帮优化答案 编辑:程序博客网 时间:2024/06/14 07:45
1、shell编程的hello word![root@baolibin shell]# pwd/usr/local/shell[root@baolibin shell]# vim hello.sh[root@baolibin shell]# chmod +x hello.sh[root@baolibin shell]# more hello.sh#!/bin/bash#hello wordecho 'hello word!'[root@baolibin shell]# ./hello.shhello word!2、变量分类:本地变量环境变量局部变量位置变量特殊变量[root@baolibin shell]# echo $JAVA_HOME/usr/local/jdk[root@baolibin shell]# echo $HADOOP_HOME/usr/hadoop[root@baolibin shell]# echo ${JAVA_HOME}/usr/local/jdk[root@baolibin shell]# echo $JAVA_HOMEi love computerlove computer[root@baolibin shell]# echo ${JAVA_HOME}i love computer/usr/local/jdki love computer[root@baolibin shell]#2.1、本地变量:只对当前shell进程有效,对子进程和其它shell进程无效。定义:VAR_NAME=VALUE变量引用:${VAR_NAME}取消变量:unset VAR_NAME[root@baolibin shell]# baozi=www.hadoop.com[root@baolibin shell]# echo $baoziwww.hadoop.com[root@baolibin shell]# pstreeinit─┬─NetworkManager ├─abrtd ├─acpid ├─atd ├─auditd───{auditd} ├─automount───4*[{automount}] ├─bluetoothd ├─bonobo-activati───{bonobo-activat} ├─certmonger ├─console-kit-dae───63*[{console-kit-da}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─dbus-launch ├─devkit-power-da ├─gconfd-2 ├─gdm-binary─┬─gdm-simple-slav─┬─Xorg │ │ ├─gdm-session-wor │ │ ├─gnome-session─┬─at-spi-registry │ │ │ ├─gdm-simple-gree │ │ │ ├─gnome-power-man │ │ │ ├─metacity │ │ │ ├─plymouth-log-vi │ │ │ ├─polkit-gnome-au │ │ │ └─{gnome-session} │ │ └─{gdm-simple-sla} │ └─{gdm-binary} ├─gnome-settings-───{gnome-settings} ├─gvfsd ├─hald─┬─hald-runner─┬─hald-addon-acpi │ │ ├─hald-addon-inpu │ │ └─hald-addon-rfki │ └─{hald} ├─5*[mingetty] ├─modem-manager ├─mysqld_safe───mysqld───15*[{mysqld}] ├─polkitd ├─pulseaudio───2*[{pulseaudio}] ├─rpc.statd ├─rpcbind ├─rsyslogd───3*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─sshd───sshd───bash───pstree ├─udevd───2*[udevd] └─wpa_supplicant[root@baolibin shell]# bash[root@baolibin shell]# echo $baozi[root@baolibin shell]# exitexit[root@baolibin shell]# echo $baoziwww.hadoop.com[root@baolibin shell]# unset baozi[root@baolibin shell]# echo $baozi[root@baolibin shell]#2.2、环境变量:自定义的环境变量对当前shell进程及其子shell进程有效,对其它的shell进程无效。定义:export VAR_NAME=VALUE对所有shell进程都有效需要配置到配置文件中:vi /etc/profile source /etc/profile[root@baolibin shell]# export xiaobaozi=www.hbase.com[root@baolibin shell]# echo $xiaobaoziwww.hbase.com[root@baolibin shell]# bash[root@baolibin shell]# echo $xiaobaoziwww.hbase.com[root@baolibin shell]# exitexit[root@baolibin shell]# echo $xiaobaoziwww.hbase.com[root@baolibin shell]# unset xiaobaozi[root@baolibin shell]# echo $xiaobaozi[root@baolibin shell]#2.3、局部变量:函数调用结束,变量就会消失。对shell脚本中某代码片段有效。定义:local VAR_NAME=VALUE。2.4、位置变量:$1,$2......${10}.../test.sh a b$0:脚本本身$1:脚本的第一个参数$2:脚本的第二个参数[root@baolibin shell]# vi test.sh[root@baolibin shell]# more test.sh#!/bin/bash#--------------echo $0echo $1echo $2[root@baolibin shell]# chmod +x test.sh[root@baolibin shell]# ./test.sh 4 5./test.sh45[root@baolibin shell]# ./test.sh 4 5 6./test.sh45[root@baolibin shell]#2.5、特殊变量:$?:接收上一条命令的返回状态码$#:参数个数$*:或者$@:所有的参数$?:接收上一条命令的返回状态码:[root@baolibin shell]# lshello.sh test.sh[root@baolibin shell]# echo $?0[root@baolibin shell]# hehehaha-bash: hehehaha: command not found[root@baolibin shell]# echo $?127[root@baolibin shell]# echo $?0[root@baolibin shell]#$#:参数个数:[root@baolibin shell]# vi count.sh[root@baolibin shell]# more count.sh#!/bin/bash#----------echo $#[root@baolibin shell]# chmod +x count.sh[root@baolibin shell]# ./count.sh0[root@baolibin shell]# ./count.sh a1[root@baolibin shell]# ./count.sh a b2[root@baolibin shell]# ./count.sh a b c3[root@baolibin shell]#$*:或者$@:所有的参数:[root@baolibin shell]# vi alloy.sh[root@baolibin shell]# more alloy.sh#!/bin/bash#---------echo $#echo $*echo $@[root@baolibin shell]# chmod +x alloy.sh[root@baolibin shell]# ./alloy.sh0[root@baolibin shell]# ./alloy.sh a1aa[root@baolibin shell]# ./alloy.sh a b2a ba b[root@baolibin shell]#2.6:单引号、双引号、反引号''单引号不解析变量。""双引号会解析变量。``反引号是执行并引用一个命令的执行结果,类似于$(...)。[root@baolibin shell]# echo $baozi[root@baolibin shell]# baozi=www.hive.com[root@baolibin shell]# echo $baoziwww.hive.com[root@baolibin shell]# echo '$baozi'$baozi[root@baolibin shell]# echo "$baozi"www.hive.com[root@baolibin shell]# echo `$baozi`-bash: www.hive.com: command not found[root@baolibin shell]# [root@baolibin shell]# echo `ls`alloy.sh count.sh hello.sh test.sh[root@baolibin shell]# lsalloy.sh count.sh hello.sh test.sh[root@baolibin shell]# echo $(ls)alloy.sh count.sh hello.sh test.sh[root@baolibin shell]#[root@baolibin shell]# echo $(ll)总用量 16 -rwxr-xr-x. 1 root root 47 4月 18 22:07 alloy.sh -rwxr-xr-x. 1 root root 32 4月 18 22:04 count.sh -rwxr-xr-x. 1 root root 43 4月 16 22:51 hello.sh -rwxr-xr-x. 1 root root 52 4月 18 21:58 test.sh[root@baolibin shell]# ll总用量 16-rwxr-xr-x. 1 root root 47 4月 18 22:07 alloy.sh-rwxr-xr-x. 1 root root 32 4月 18 22:04 count.sh-rwxr-xr-x. 1 root root 43 4月 16 22:51 hello.sh-rwxr-xr-x. 1 root root 52 4月 18 21:58 test.sh[root@baolibin shell]#
0 0
- shell 编程(1)
- Shell编程(1)
- shell编程(1)
- shell编程(1)
- Shell编程(1)
- (6-1)shell编程 (1)
- Shell编程学习(1)----shell和shell中的变量
- Linux Shell编程(1)——shell编程简介
- Shell编程(1):Hello Shell
- Shell编程摘录(1)-
- Shell 编程速成(1)
- shell编程珠玑(1)
- Shell 编程1(基础)
- Unix Shell 编程 (1)
- shell 编程基础(1)
- Shell编程基础(1)
- Shell编程基础(1)
- shell 高级编程(1)
- 如何在NAS上安装Git Server
- java获取客户端请求IP地址 获取公网ip
- Linux 重启后重新Mount磁盘
- 信号SIG_TERM与信号SIG_KILL
- 线程
- (6-1)shell编程 (1)
- UILabel的效果
- 升级 AppCompat V21 开发纸墨设计风格应用so easy
- Android SDK下载和更新失败的解决方法!!!
- 建立一个对象数组,设立一个函数max,选出成绩最高的(第三章第五题)
- 8为Java 大牛的博客
- 最佳安全工具
- 知识框
- (6-2)shell编程 (2)