1:bash特性之狀態返回值
變量$?,用于保存命令執行成功與否的狀態,0表示成功,1-255表示失敗,以命令ls為例:
-
執行成功
[root@localhost ~]# ls /usr/ bin etc games include lib lib64 libexec local sbin share src tmp [root@localhost ~]# echo $? 0
-
命令輸入錯誤
[root@localhost ~]# lss /usr -bash: lss: command not found [root@localhost ~]# echo $? 127
-
命令參數輸入錯誤
[root@localhost ~]# ls /usrrr ls: cannot access /usrrr: No such file or directory [root@localhost ~]# echo $? 2
其他錯誤狀態,$?也會是非0結果。
2:命令行展開
2.1:使用命令行展開,創建/tmp下的a\_c,a\_d,b\_c,b\_d文件。
[root@localhost ~]# mkdir -pv {a,b}_{c,d} mkdir: created directory `a_c' mkdir: created directory `a_d' mkdir: created directory `b_c' mkdir: created directory `b_d' [root@localhost ~]# ls -d {a,b}_{c,d} a_c a_d b_c b_d
2.2:在/tmp下創建如下文件。
[root@localhost ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,etc{/rc.d/init.d,/sysconfig/network-scripts},dev,lib/modules,lib64,proc,sbin,sys,tmp,usr/local{/bin,/sbin},var{/lock,/log,/run}} [root@localhost ~]# tree /tmp/mylinux//tmp/mylinux/ |-- bin |-- boot | `-- grub |-- dev |-- etc | |-- rc.d | | `-- init.d | `-- sysconfig | `-- network-scripts |-- lib | `-- modules |-- lib64 |-- proc |-- sbin |-- sys |-- tmp |-- usr | `-- local | |-- bin | `-- sbin `-- var |-- lock |-- log `-- run24 directories, 0 files
3:如何定義一個命令的別名?如何在一個命令中引用另一個命令的結果?
使用反引“或$()可引用其他命令的執行結果:
-
使用反引號
[root@localhost ~]# dirname `which touch` /bin # 顯示命令touch所在的路徑名
-
使用$()
[root@localhost ~]# dirname $(which touch) /bin
4:顯示/var目錄下所有以字母“l”開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其他字符)的文件和目錄
[root@localhost ~]# ls -d /var/l*[[:digit:]]*[[:lower:]] /var/l13u /var/lfs3k
5:顯示/etc目錄下以任意一個數字開頭且以一個非數字結尾的文件和目錄
[root@localhost ~]# ls -d /etc/[[:digit:]]*[^[:digit:]] /etc/222kkk /etc/333uuu
6:顯示/etc目錄下以非字母開頭且后面跟了一個字母和其他任意長度任意字符的文件和目錄
[root@localhost ~]# ls -d /etc/[^[:alpha:]][[:alpha:]]* /etc/1djj
7:在/tmp下創建以tfile開頭,后跟當前時間的文件
[root@localhost ~]# touch /tmp/tfile-`date +%F-%H-%M-%S` [root@localhost ~]# touch /tmp/tfile-`date +%F-%H-%M-%S` [root@localhost ~]# ls /tmp tfile-2016-08-25-08-14-35 tfile-2016-08-25-08-14-36
8:復制/etc目錄下以字母“p”開頭,以非數字結尾的文件到/tmp/mytest1目錄中
[root@localhost ~]# mkdir /tmp/mytest1 [root@localhost ~]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/ [root@localhost ~]# ls /tmp/mytest1/ pam.d passwd passwd- pki plymouth pm popt.d postfix ppp prelink.conf.d printcap profile profile.d protocols
9:復制/etc目錄下所有以.d結尾的目錄和文件至/tmp/mytest2目錄中
[root@localhost ~]# mkdir /tmp/mytest2 [root@localhost ~]# cp -r /etc/*.d /tmp/mytest2 [root@localhost ~]# ls /tmp/mytest2 bash_completion.d dracut.conf.d makedev.d prelink.conf.d rc1.d rc5.d statetab.d yum.repos.d chkconfig.d init.d modprobe.d profile.d rc2.d rc6.d sudoers.d cron.d ld.so.conf.d pam.d rc.d rc3.d rsyslog.d sysctl.d depmod.d logrotate.d popt.d rc0.d rc4.d rwtab.d xinetd.d
10:復制/etc目錄下所有以字母l或n或m開頭,以.conf結尾的文件至目錄/tmp/mytest3中
[root@localhost ~]# mkdir /tmp/mytest3 [root@localhost ~]# cp -r /etc/[lmn]*.conf /tmp/mytest3 [root@localhost ~]# ls /tmp/mytest3 ld.so.conf libaudit.conf libuser.conf logrotate.conf mke2fs.conf nsswitch.conf
原創文章,作者:wangzhenyu177,如若轉載,請注明出處:http://www.www58058.com/47152