摘自《Linux shell 脚本攻略》未完待续!!!Escape,格式printf"%-5s%-10s%-4.2f\n"1Sarath80.3456ColorReset=0,Black=40,Red=41,Green=42,yellow=32,blue=44,magenta=45,cyan=46,white=47echo-e"\e[1;31mThisisredtext\e[0m"获取进程运行时的环境变量cat/proc/$PID/evirongetprocessidpgrepgeditgetstringlengthlength=$(#var)识别当前shellecho$0echo$SHELL检查是否是超级用户if[$UID-ne0];thenechonotrootelseechorootfiintegeroperationno1=4no2=5letresult=no1+no2letno+=6result=$[no1+no2]result=$[$no1+5]result=$((no1+50))result=`expr3+4`result=$(expr$no1+5)浮点运算,精度,基地,广场回声“4*0.56”|bcecho"scale=2;3/8"|bcno=100echo"obase=2;$no"|bcno=1100100echo"obase=10;ibase=2;$no"|bcecho"sqrt(100)"|bcecho"10^10"|bc输出文本重定向到文件#overwriteecho"x">temp.txt#appendecho"x">>temp.txt#Nooutputls+2>out.txt#分别重定向cmd2>stderr.txt1>stdout.txt#两者都重定向到同一个文件cmd2>&1output.txtcmd&>output.txt#Discardsome_command2>/dev/null#从stdin读取,将行号添加到输出cata*|teeout.txt|cat-n#appendcata*|tee-aout.txt|cat-n文件重定向到命令#从文件中读取数据cmdtruncatemodefilewrite>>以附加模式写入文件#打开并读取文件描述符3exec3input.txtexec3output.txt#用于编写appendexec5>>input.txtarray#definearray_var=(123456)array_var[0]="test1"#printecho${array_var[0]}index=5echo${array_var[$index]}#打印列表echo${array_var[*]}echo${array_var[@]}#打印长度echo${#array_var[*]}关联数组#语句declare-Aass_array#InsideEmbeddedindexdeclarationass_aray=([index1]=val1[index2=val2])#独立索引声明ass_array[index1]=val1#获取索引列表echo${!array_var[*]}echo${!array_var[@]}aliasaliasinstall='sudoapt-getinstall'aliasrm='cp$@~/backup;rm$@'终端信息#获取终端的行数和列数tputcolstputlines#移动光标到(100,100)tputcup100100#设置终端背景色no=0~7tputsetbno#设置文本前景colortputserfno#设置文字样式加粗tputbold#设置下划线起止tputsmu1tputrmu1#删除当前光标位置到行尾的所有内容tputed防止输出发送到终端echo-e“输入密码:”stty-echoreadpasswordsttyechoechoechopasswordreadgetsetdateanddelay#readableformatdate#timestampsecondsdate+%s#datetotimestampdate--date"ThuNov1808:07:21IST2010"+%s#获取星期几--date"Jan202001"+%A#日期格式打印数据"+%d%B%Y"#设置日期时间date-s"21June200911:01:22"week%a%Amonth%b%Bday%d固定格式Date(mm/dd/yy)%Dyear%y%Yhour%I%Hminute%Msecond%Snanosecond%Ntimestamp%sdelaysleepno_of_secondsdebugscriptbash-xscript.shsh-xscript#启用禁用调试print#执行时显示参数和命令set-x#禁止调试set+x#读取命令时显示输入set-v#禁止打印输入set+vfunctionandparameterfunctionfname(){statements}fname(){statements}#RecursiveF(){echo$1;你好;sleep1;}#exportexport-ffname#获取命令或函数返回值echo$?;#命令传参command-p-v-kfname(){echo$1,$2;#参数12echo"$@";#在列表中一次打印所有参数"$1""$2""$3"echo"$*";#Parametersaretreatedasasingleentity"$1c$2c$3"wherecisthefirstcharacterofIFSreturn0;}forkbomb:(){:|:&};:#读取命令输出,双引号用于保留空格和换行符output="$(COMMANDS)"output=`ls|猫-n`