注意事项
在定义变量的时候左右两边一定不要有空格,不然容易出现一些错误
例子: bb= 123
这样是错误的
正确: bb=123
整数型变量自加
let i++
return 和 echo
#!/bin/bashtest(){echo 100return 104}result=`test`echo "返回状态码是:${?}"echo "返回值是:${result}"#更加深刻去理解return与$?之间的关系#return 这里的意思是执行函数test返回的结果是#${?}则是说明返回最新返回的一个值#!/bin/bashfile=$1test(){if [ -f ${file} ];thenreturn 50elsereturn 100fi}test[ $? -eq 50 ]&& echo "存在"[ $? -eq 100 ]&& echo "不存在"
统计文件行
#!/bin/bashfile=$1test(){i=0while read linedolet i++done<${file}echo ${i}}testcat -n /etc/passwdwc -l /etc/passwdgrep -n /etc/passwdless -N /etc/passwdsed '=' /etc/passwd
调用另外一个脚本的函数
#test.sh#!/bin/bash. ~/shell/sb.shtest#sb.sh#!/bin/bashtest(){echo "调用的sb.sh的test函数"}
