2018-02-08 shell脚本编程基础1

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$?
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容