标签:pat 调用 string $@ export 综合练习 length who script
[root@oracle ~]# echo $BASHPID #没有结果?? [root@oracle ~]# echo $$ #显示执行进程的 id 号 3131 [root@oracle ~]# ps ax | grep bash 2589 ? Ss 0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients" 2780 pts/1 Ss+ 0:00 bash 3041 ? S 0:00 /bin/bash /usr/bin/eio 3131 pts/2 Ss 0:00 bash 3161 pts/2 R+ 0:00 grep bash
#!/bin/bash [ ! -n "$1" ] && echo -e "wrong\nusage : $0 canshu1 " && exit -1 || echo "right" #-n 测试参数的长度是否为 0,不为 0 则返回 0,表示 true 测试: [root@oracle ~]# ./f2.sh 没有接参数 wrong usage : ./f2.sh canshu1 [root@oracle ~]# echo $? 255 [root@oracle ~]# ./f2.sh ff 接了参数 right [root@oracle ~]# echo $? 0
1、$# #!/bin/bash in=1 for arg in "$#" do echo "arg $in = $arg" let "in=in+1" done 测试结果:$#表示参数总数 [root@oracle ~]# ./f1.sh a b c arg 1 = 3 2、$* #!/bin/bash in=1 for arg in "$*" do echo "arg $in = $arg" let "in=in+1" done 测试:$*参数以一行显示 [root@oracle ~]# ./f1.sh a b c arg 1 = a b c 3、$@ #!/bin/bash in=1 for arg in "$@" do echo "arg $in = $arg" $((in+=1)) done 测试:$@参数以空格为分隔 [root@oracle ~]# ./f1.sh a b c arg 1 = a arg 2 = b arg 3 = c
[root@oracle ~]# vim &> { sleep 2; > eval ‘kill -9 $!‘ &> /dev/null; > } [1] 3855 #停了 2 秒 [1]+ Stopped vim You have new mail in /var/spool/mail/root [root@oracle ~]# [1]+ 已杀死 vim
#!/bin/bash echo -e "please input you id:" read string #ln=${#string} #这 2 句注释掉的和下面的那句起同样的作用 #ln=$( expr "$string" : ‘.*‘) ln=$( expr length $string ) ((ln < 6)) && echo "$string shi fei fa de id hao" || echo " id $string shi he fa de "
[root@desktop Desktop]# num=1234HHHjjj123 [root@desktop Desktop]# echo $num 1234HHHjjj123 [root@desktop Desktop]# echo ${num} 1234HHHjjj123 [root@desktop Desktop]# echo ${#num}
[root@oracle ~]# stringZ=123aaa123bbb456ccc 不要前面 6 个字符: [root@oracle ~]# echo ${stringZ:6} 123bbb456ccc 从编号为 3 的字符,即第 4 个字符开始,提取 6 个字符 [root@oracle ~]# echo ${stringZ:3:6} aaa123
[root@oracle ~]# stringZ=123aaa123bbb456ccc 从第 6 个字符即 a 开始提取 6 个字符: [root@oracle ~]# echo `expr substr $stringZ 6 6` a123bb
[root@desktop Desktop]# SN=WINDD20110908DDSL [root@desktop Desktop]# echo ${SN:5:4}-${SN:9:2}-${SN:11:2} 2011-09-08
[root@oracle ~]# echo `expr match "$stringZ" ‘\(123[a-z]..[1-9]*\)‘` 123aaa123 [root@oracle ~]# echo `expr "$stringZ" : ‘\(123[a-z]..[1-9]*\)‘` 123aaa123
[root@oracle ~]# echo `expr "$stringZ" : ‘.*\([a-z]...\)‘` b456
[root@oracle ~]# echo "${stringQ#q*w}" 最短匹配 w20081010aabb [root@oracle ~]# echo "${stringQ##q*w}" 最长匹配 20081010aabb
[root@desktop Desktop]# cat /boot/grub/grub.conf |grep kernel|grep 2.6 |cut -d ‘ ‘ -f 2 /vmlinuz-2.6.32-71.el6.x86_64 [root@desktop Desktop]# VV=$(cat /boot/grub/grub.conf |grep kernel|grep 2.6 |cut -d ‘ ‘ -f 2) [root@desktop Desktop]# echo ${VV#/[a-z]*-} 2.6.32-71.el6.x86_64
[root@oracle ~]# echo "${stringQ%0*b}" 从后面最短匹配 qqww2008101 [root@oracle ~]# echo "${stringQ%%0*b}" 从后面最长匹配 qqww2
#!/bin/bash DIR=$1 fix="TXT txT tXt tXT Txt TXt TxT" for LINE in $fix do old=$LINE new=txt for FILE in $(find $DIR -name "*.$old") do mv -v $FILE ${FILE%$old}$new done done 测试: [root@desktop Desktop]# ./chname.sh ./ `./2.TXT‘ -> `./2.txt‘ `./3.TXT‘ -> `./3.txt‘ `./1.TXT‘ -> `./1.txt‘
N 声明了,只是没有设置参数 [root@desktop Desktop]# N= [root@desktop Desktop]# N1=qq [root@desktop Desktop]# echo "${N-dd}" 没有设置参数的 N 没被替换 You have new mail in /var/spool/mail/root [root@desktop Desktop]# echo "${N:-dd}" 没有设置参数的 N 被替换 dd [root@desktop Desktop]# echo "${N1-dd}" 设置了参数的 N1 没被替换 qq [root@desktop Desktop]# echo "${N1:-dd}" 设置了参数的 N1 没被替换 qq 没有声明的 N2,被默认值替换 [root@desktop Desktop]# echo "${N2-dd}" dd [root@desktop Desktop]# echo "${N2:-dd}" dd
[root@desktop Desktop]# M= [root@desktop Desktop]# M1=rr [root@desktop Desktop]# echo "${M=aa}" [root@desktop Desktop]# echo "${M:=aa}" aa [root@desktop Desktop]# echo "${M1=aa}" rr [root@desktop Desktop]# echo "${M1:=aa}" rr [root@desktop Desktop]# echo "${M2=aa}" aa [root@desktop Desktop]# echo "${M2:=aa}" aa
[root@desktop Desktop]# H= [root@desktop Desktop]# H1=oo [root@desktop Desktop]# echo "${H+yy}" yy [root@desktop Desktop]# echo "${H:+yy}" [root@desktop Desktop]# echo "${H1+yy}" yy [root@desktop Desktop]# echo "${H1:+yy}" yy [root@desktop Desktop]# echo "${H2+yy}" [root@desktop Desktop]# echo "${H2:+yy}"
实例 1: echo `basename $PWD` # 打印当前工作路径最后一段子目录名字 echo "${PWD##*/}" # 另一种方法的实现 echo `basename $0` # 得到脚本的名字 echo $0 # 另一种方法的实现 echo "${0##*/}" # 另一种方法的实现 filename=test.data echo "${filename##*.}" # 移除"."之前的内容,得到 data 文件名的后缀 实例 2: 下面的例子实现将特定的文件名后缀,比如 .gif 统统改为其他后缀 for filename in *.$1 # 遍历当前整个目录中所有指定的文件后缀 do mv $filename ${filename%$1}$2 #去除掉输入的文件名后缀再追加另外指定的一个 done exit 0 实例 3: path_name=/home/bozo/ideas/thoughts.for.today echo "path_name = $path_name" t=${path_name##/*/} # 根据表达式最大可能的移除/部分,最后只留下文件名 echo "path_name, stripped of prefixes = $t" t=${path_name%/} ; t=${t##*/}; # 同样的效果 实例 4:下面的例子将一个用户 yangwawa 主目录的文件拷贝到另一个用户 joe(可以用$1代替写在脚本中)的主目录中FILENAME=/home/yangwawa/.bash_profile cp -v $FILENAME{,${FILENAME/yangwawa/joe}}
提取目录中的文件名: [root@desktop Desktop]# FILENAME=/etc/sysconfig/network-scripts/ifcfg-eth0 [root@desktop Desktop]# basename $FILENAME ifcfg-eth0 [root@desktop Desktop]# echo ${FILENAME##*/} ifcfg-eth0 [root@desktop Desktop]# echo ${FILENAME%/*} /etc/sysconfig/network-scripts 拼接目录: [root@desktop Desktop]# BACK_DIR=/usr/local/share [root@desktop Desktop]# echo $BACK_DIR/${FILENAME##*/} /usr/local/share/ifcfg-eth0 替换文件路径中目录的名字: [root@desktop Desktop]# FILENAME=/home/student/.mozilla/firefox/profiles.ini [root@desktop Desktop]# echo ${FILENAME/student/visitor} /home/visitor/.mozilla/firefox/profiles.ini
#!/bin/bash declare -a BOOKS BOOKS[0]="Windows 2007" BOOKS[1]="Windows xp" BOOKS[2]="Oracle" BOOKS[3]="IBM ATX 5" BOOKS[4]="RedHat" echo "${BOOKS[@]}" #将数组作为一串字符打印出来 echo ++++++++++++++++++++++ TOTAL=${#BOOKS[@]} #以空格为依据取字符串的长度 for ((X_B=0;X_B<TOTAL;X_B++)) do echo "$X_B --> ${BOOKS[$X_B]}" done 测试: [root@desktop Desktop]# chmod u+x showbooks.sh [root@desktop Desktop]# ./showbooks.sh Windows 2007 Windows xp Oracle IBM ATX 5 RedHat ++++++++++++++++++++++ 0 --> Windows 2007 1 --> Windows xp 2 --> Oracle 3 --> IBM ATX 5 4 --> RedHat
标签:pat 调用 string $@ export 综合练习 length who script
原文地址:https://www.cnblogs.com/zoujiaojiao/p/10938328.html