Shell脚本基础笔记(1)
1、Bash中命令分割每个命令或是命令序列都是通过分号(;)或换行符来分隔 $ cmd1 ;cmd2 等价于 $cmd1 $cmd2 2、终端打印echo、printf双引号 --- 变量名替换;特殊字符序转义字符() -e才生效 单引号 ---变量名不会被扩展,依照原样输出;特殊字符序转义字符() -e才生效 m11@xubuntu:~$ echo -e "aaanbbb" aaa bbb m11@xubuntu:~$ echo -e 'aaanbbb' aaa bbb printf --- 按格式打印字符串 root@dxx-VirtualBox:~# printf "%-5s %-10s %-4.2fn" $var bbb 1.123456 aaaa bbb 1.12 3、变量3.1、变量赋值和判相等var=value var = value 3.2、变量继承export 变量名 :当前shell脚本执行的任何应用程序都会继承这个变量 3.3、获取变量长度 ${#变量名}3.4、检查是否是root用户 $UID = 0 超级用户3.5、修改bash的提示字符 $SP13.6、判断变量是否空${parameter:+expression} : 如果parameter有值且不为空,则赋expression的值 root@xubuntu:/home/m11# aaa="bbb"&&echo ${aaa:+"----"} ---- root@xubuntu:/home/m11# aaa=""&&echo ${aaa:+"----"} ${parameter:=expression} : 如果parameter为空,则赋expression的值;不为空,则使用原值 root@xubuntu:/home/m11# aaa=""&&echo ${aaa:="----"} ---- root@xubuntu:/home/m11# aaa="aaa"&&echo ${aaa:="----"} aaa 4、计算4.1 let、$(())、$[]计算中变量'$'字符可有可无。 包裹计算表达式的$(())、和$[]必须有'$'字符。 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=aa*bb && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$aa*bb && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=[aa*bb] && echo $rs bash: let: rs=[aa*bb]: syntax error: operand expected (error token is "[aa*bb]") root@xubuntu:/home/m1# aa=111;bb=222 && let rs=$[aa*bb] && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$[$aa*bb] && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=(($aa*bb)) && echo $rs bash: syntax error near unexpected token `(' root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$(($aa*bb)) && echo $rs 24642 root@xubuntu:/home/m11# aa=111;bb=222 && let rs=$((aa*bb)) && echo $rs 24642 4.2 bc 高级数学运算echo "4 * 5" |bc echo "scale=10;3/8 " |bc 设置小数精度 aaa=10;echo "obase=2;$aaa " |bc 进制转换 echo "sqrt(100)" |bc 平方根 echo "10^2" |bc 平方 5、重定向0 stdin 1 stdout 2 stderr > 只将标准输出进行重定向,等价于 1> 2> 只将错误输出进行重定向 2> xxx 1> xxx1 将错误输出到xxx中,将标准输出到xxx1中 1> test 2>&1 将将标准输出到test中,并且错误输出传递给1输出通道 等价于&> test tee 将数据重定向到文件中,同时提供数据副本作为后续命令的stdin cat<<END 将END间的数据都当做数据 #!/bin/bash cat<<END <xml>adsfasdf <h1>asdfasdf </h1> </xml> END 6、数组1)普通数组定义、使用、赋值、打印 arry=(1 2 3 4 ) root@xubuntu:/home/m11/test# echo ${arry[2]} 3 root@xubuntu:/home/m11/test# echo ${arry[*]} 1 2 3 4 root@xubuntu:/home/m11/test# echo ${arry[@]} 1 2 3 4 root@xubuntu:/home/m11/test# arry[1]=12 root@xubuntu:/home/m11/test# echo ${arry[@]} 1 12 3 4 root@xubuntu:/home/m11/test# echo ${#arry[*]} 4 2)关联数组(一定要先声明)root@xubuntu:/home/m11/test# declare -A ass_arry root@xubuntu:/home/m11/test# ass_arry=([name]="dxx" [age]="12") root@xubuntu:/home/m11/test# echo ${ass_arry[name]} dxx root@xubuntu:/home/m11/test# echo ${!ass_arry[*]} name age root@xubuntu:/home/m11/test# echo ${!ass_arry[@]} name age 7、别名root@xubuntu:/home/m11/test# alias lh='ls -lh' 定义别名 root@xubuntu:/home/m11/test# unalias lh 删除别名 忽略别名,使用转义字符 '',格式 command 8、命令pgrep 进程名字 :获取进程的PID cat /proc/1977/environ :查看1977进程的环境变量 root@dxx-VirtualBox:~# pgrep nm-applet 1977 root@dxx-VirtualBox:~# cat /proc/1977/environ GNOME_KEYRING_PID=USER=dxxLANGUAGE=en_USLC_TIME=zh_CN.UTF-8XDG_SEAT=seat0JAVA_TOOL_OPTIONS=-javaagent:/usr/share/java/jayatanaag.jar XDG_SESSION_TYPE=x11COMPIZ_CONFIG_PROFIL (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |