1、bash、source和给执行权限后打开脚本有什么区别?
[root@centos7 app]#echo $$
11730
[root@centos7 app]#vim f1
1 #!/bin/bash
2 echo "hello"
3 sleep 1000
[root@centos7 app]#chmod a+x f1
[root@centos7 app]#./f1
hello
^C
[root@centos7 app]#bash f1
hello
^C
[root@centos7 app]#source f1
hello
image.png
image.png
image.png
总结:bash和绝对路径及相对路径打开脚本时开了一个子进程,不影响当前进程;source打开脚本时仍然是在当前进程下,会影响当前进程。所以打开脚本时尽量不要用source。
2、变量的指向
[root@localhost tmp]#var1=wang
[root@localhost tmp]#var2=$var1
[root@localhost tmp]#echo $var1
wang
[root@localhost tmp]#echo $var2
wang
[root@localhost tmp]#var1=mage
[root@localhost tmp]#echo $var1
mage
[root@localhost tmp]#echo $var2
wang
总结:通过上面的例子我们可以看出var2=$var1,指的是var2等于var1指向的wang,而不是变量本身,echo$var2时仍指向wang。
3、脚本里不识别别名
[root@localhost tmp]#ll
total 56
-rw-r--r--. 1 root root 36 Feb 6 18:36 aa
-rw-r--r--+ 1 root root 30 Feb 5 23:05 abc
-rw-r--r--. 1 root root 27 Feb 6 18:36 bb
-rw-r--r--. 1 root root 17 Feb 5 23:12 bbb
-rw-r--r--. 1 root root 42 Feb 6 18:45 cc
-rw-r--r--. 1 root root 53 Feb 7 13:49 dd
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rw-r--r--. 1 root root 31 Feb 8 23:12 f1
-rw-r--r--. 1 root root 34 Feb 5 20:23 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
-rw-r--r--. 1 root root 62 Feb 7 23:04 test
drwx------. 2 root root 4096 Feb 8 23:13 vmware-root
-rw-------. 1 root root 828 Feb 6 14:07 yum_save_tx-2018-02-06-14-076XjYEV.yumtx
[root@localhost tmp]#vim f1
[root@localhost tmp]#cat f1
#!/bin/bash
echo "hello"
rm dd
[root@localhost tmp]#alias rm
alias rm='rm -i'
[root@localhost tmp]#chmod u+x f1
[root@localhost tmp]#./f1 ----因为rm是rm -i的别名,如果不在脚本里执行rm profile,则会出现提示信息,但执行脚本时未出现提示信息,说明脚本里不识别别名
hello
hello
[root@localhost tmp]#ll
total 52
-rw-r--r--. 1 root root 36 Feb 6 18:36 aa
-rw-r--r--+ 1 root root 30 Feb 5 23:05 abc
-rw-r--r--. 1 root root 27 Feb 6 18:36 bb
-rw-r--r--. 1 root root 17 Feb 5 23:12 bbb
-rw-r--r--. 1 root root 42 Feb 6 18:45 cc
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rwxr--r--. 1 root root 31 Feb 8 23:12 f1
-rw-r--r--. 1 root root 34 Feb 5 20:23 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
-rw-r--r--. 1 root root 62 Feb 7 23:04 test
drwx------. 2 root root 4096 Feb 8 23:13 vmware-root
-rw-------. 1 root root 828 Feb 6 14:07 yum_save_tx-2018-02-06-14-076XjYEV.yumtx
总结:脚本里不识别别名,所以脚本里不要用别名。
4、本地变量和环境变量的区别
- 本地变量:只对当前shell有效
- 环境变量:父进程定义的变量可以传给子进程,但子进程定义的变量无法传给父进程。
5、定义的变量为多行
[root@localhost tmp]#name=`cat /etc/fstab`
[root@localhost tmp]#echo $name
# # /etc/fstab # Created by anaconda on Mon Jan 8 14:04:26 2018 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=c4d2e0b5-3d91-453f-8df1-1636e2e7ec0c / ext4 defaults 1 1 UUID=7b46dd2d-2980-484a-9b85-69559e10c394 /boot ext4 defaults 1 2 UUID=71c894af-939e-49e6-966b-4b5d21e9f285 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
[root@localhost tmp]#echo "$name"
#
# /etc/fstab
# Created by anaconda on Mon Jan 8 14:04:26 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=c4d2e0b5-3d91-453f-8df1-1636e2e7ec0c / ext4 defaults 1 1
UUID=7b46dd2d-2980-484a-9b85-69559e10c394 /boot ext4 defaults 1 2
UUID=71c894af-939e-49e6-966b-4b5d21e9f285 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
总结:如果定义的变量值为多行,echo打印时要加上双引号,否则显示的是一行。
6、如果定义的变量是个未执行的命令
[root@localhost tmp]#cmd=hostname
[root@localhost tmp]#echo $cmd
hostname
[root@localhost tmp]#$cmd
localhost.localdomain
7、只读变量
[root@localhost tmp]#a=3.14
[root@localhost tmp]#readonly a
[root@localhost tmp]#a=4 ---不能修改
-bash: a: readonly variable
[root@localhost tmp]#unset a ---不能取消
-bash: unset: a: cannot unset: readonly variable
[root@localhost tmp]#readonly b ---不能先定义只读变量
[root@localhost tmp]#b=0
-bash: b: readonly variable
[root@localhost tmp]#readonly -p ---查看只读变量
declare -r BASHOPTS="checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath"
declare -ir BASHPID=""
declare -ar BASH_VERSINFO='([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")'
declare -ir EUID="0"
declare -ir PPID="29205"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="0"
declare -r a="3.14"
declare -r b
总结:声明只读变量后不能修改,不能取消,只能退出后才能取消,如果先定义为只读变量,就无法给只读变量赋值了。
8、小括号的作用
[root@localhost tmp]#(umask 777;touch /tmp/f2)
[root@localhost tmp]#ll f2
----------. 1 root root 0 Feb 8 23:44 f2
[root@localhost tmp]#touch f3
[root@localhost tmp]#ll f3
-rw-r--r--. 1 root root 0 Feb 8 23:44 f3
[root@localhost tmp]#name=mage;age=30;(age=20;echo $age;echo $name);echo $name;echo $age
20
mage
mage
30
总结:小括号相当于开了一个子进程,只对当前括号里的环境有效,外面的环境相当于父进程,父进程定义的变量可以传给小括号,而小括号内定义的变量无法传给外面的父进程。
9、位置变量
作用:调用脚本的参数
$1, $2, ...:对应第1、第2等参数,shift [n]换位置
$0: 命令本身
$*: 传递给脚本的所有参数,全部参数合为一个字符串
$@: 传递给脚本的所有参数,每个参数为独立字符串
$#: 传递给脚本的参数的个数
$@ $* 只在被双引号包起来的时候才会有差异
set --清空所有位置变量
- 各参数的含义
[root@localhost tmp]#cat -n arg.sh
1 #!/bin/bash
2 echo "the fist parameter is $1"
3 echo "the second parameter is $2"
4 echo "the tenth parameter is ${10}"
5 echo "the all parameter is $*"
6 echo "the all parameter is $@"
7 echo "the scriptname is `basename $0`"
8 echo "the parameter numbers is $#"
[root@localhost tmp]#chmod u+x arg.sh
[root@localhost tmp]#./arg.sh {a..z}
the fist parameter is a
the second parameter is b
the tenth parameter is j
the all parameter is a b c d e f g h i j k l m n o p q r s t u v w x y z
the all parameter is a b c d e f g h i j k l m n o p q r s t u v w x y z
the scriptname is arg.sh
the parameter numbers is 26
- $*和$@的区别
[root@localhost tmp]#cat f1.sh
#!/bin/bash
echo "all parameter is $*"
./f2.sh "$*"
[root@localhost tmp]#cat f2.sh
#!/bin/bash
echo "the first parameter is $1"
[root@localhost tmp]#./f1.sh aa bb cc
all parameter is aa bb cc
the first parameter is aa bb cc ---所有参数合成一个字符串
[root@localhost tmp]#cat f1.sh
#!/bin/bash
echo "all parameter is $*"
./f2.sh "$@"
[root@localhost tmp]#cat f2.sh
#!/bin/bash
echo "the first parameter is $1"
[root@localhost tmp]#./f1.sh aa bb cc
all parameter is aa bb cc
the first parameter is aa ---所有参数为单独的字符串
总结:$*表示传给脚本的所有参数合成一个字符串
$@表示传给脚本的所有参数为单独的字符串
- shift的作用
[root@localhost tmp]#cat shift.sh
#!/bin/bash
echo "all parameter is $*"
echo "the first parameter is $1"
shift
echo shift
echo "all parameter is $*"
echo "the first parameter is $1"
shift
echo shift
echo "all parameter is $*"
echo "the first parameter is $1"
[root@localhost tmp]#./shift.sh x y z
all parameter is x y z
the first parameter is x
shift
all parameter is y z
the first parameter is y
shift
all parameter is z
the first parameter is z
[root@localhost tmp]#cat shift.sh
#!/bin/bash
echo "all parameter is $*"
echo "the first parameter is $1"
shift 2
echo shift 2
echo "all parameter is $*"
echo "the first parameter is $1"
shift 2
echo shift 2
echo "all parameter is $*"
echo "the first parameter is $1"
[root@localhost tmp]#./shift.sh {a..n}
all parameter is a b c d e f g h i j k l m n
the first parameter is a
shift 2
all parameter is c d e f g h i j k l m n
the first parameter is c
shift 2
all parameter is e f g h i j k l m n
the first parameter is e
总结:shift的作用是将第一个参数移走,执行一次shift就移走第一参数,执行shift 2 就移走前面两个参数。
10、退出状态码
进程使用退出状态来报告成功或失败
- 0 代表成功,1-255代表失败
- $? 变量保存最近的命令退出状态
例如:
ping-c1-W1hostdown&>/dev/null
echo$?