Tools
首页
画图
音乐
采集
记事
博客
实验室
登录
lypeng
146
文章
11
分类
46
记事
分类
生活-[23]
Linux-[24]
前端-[9]
数据库-[16]
PHP-[31]
git-[7]
其他-[6]
python-[20]
算法-[4]
React-Native-[4]
中草药-[2]
广告位1
广告位2
首页
/ Linux
返回列表
(Linux学习四)shell脚本学习记录
阅读:886
发布:2016-05-08
作者:lypeng
教程地址:http://c.biancheng.net/cpp/view/6994.html 看完的感觉:程序语言之间都有点像,gt,lt,eq,这些在tp里面已经接触了!区别是这里更简洁,大于等于用ge,而不是geq。elseif->elif,后面多一个then像asp的写法,字符串直接有好多规则写法要记,一行一句,末尾不加;采用#号注释~等等等等,有好多要记的~ bash笔记,大多都是直接复制的,不过在虚拟机里面有做过测试! **1.第一个脚本** 将以下内容保存为test.sh ```bash #!/bin/bash # 这里是注释内容 echo "hello world" ``` 给脚本添加执行权限,以下三个任选一个都可以,-rwxrwxrwx,权限顺序ugo,a是所有,省略为a chmod u+x test.sh chmod +x test.sh chmod a+x test.sh **2.第二个脚本,获取用户输入用read** ```bash #!/bin/bash echo "please input your age:" read AGE echo "your age is $AGE" ``` 执行如下 [liyongpeng@localhost 桌面]$ ./t.sh please input your age: 88 your age is 88 [liyongpeng@localhost 桌面]$ **3.变量定义** 1).变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样 2).定义变量不许要($),使用变量的时候才加美元符($) 3).使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。 4).unset删除变量,被删除后不能再次使用;unset 命令不能删除只读变量。 ```bash myUrl="http://www.aaa.com" myNum=100 echo $myUrl echo ${myUrl} readonly myUrl myUrl="http://www.xxx.com" echo $myUrl ``` 执行结果如下,报了一个警告,只读变量 ```bash [liyongpeng@localhost 桌面]$ ./t.sh http://www.aaa.com http://www.aaa.com ./t.sh: line 7: myUrl: readonly variable http://www.aaa.com ``` **4.特殊变量** $0 当前脚本的文件名 $n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。 $# 传递给脚本或函数的参数个数。 $* 传递给脚本或函数的所有参数。 $@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。 $? 上个命令的退出状态,或函数的返回值。 $$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。 $* 和 $@ 都表示传递给函数或脚本的所有参数,不被双引号(" ")包含时,都以"$1" "$2" … "$n" 的形式输出所有参数。 但当它们被双引号(" ")包含时,"$*" 会将所有的参数作为一个整体,以"$1 $2 … $n"的形式输出所有参数;"$@" 会将各个参数分开,以"$1" "$2" … "$n" 的形式输出所有参数。 退出状态 $? 可以获取上一个命令的退出状态。所谓退出状态,就是上一个命令执行后的返回结果。 退出状态是一个数字,一般情况下,大部分命令执行成功会返回 0,失败返回 1。(奇葩) 不过,也有一些命令返回其他值,表示不同类型的错误。 **5.变量替换** -e,使用后替换\n \r\t\v等等 ```bash echo "you are a pig \n really?" [liyongpeng@localhost 桌面]$ ./t.sh you are a pig \n really? echo -e "you are a pig \n really?" [liyongpeng@localhost 桌面]$ ./t.sh you are a pig really? 命令结果保存到变量里面,如下 m=`pwd` echo "$m" [liyongpeng@localhost 桌面]$ ./t.sh /home/liyongpeng/桌面 ``` ${var} 变量本来的值 ${var:-word} 如果变量 var 为空或已被删除(unset),那么返回 word,但不改变 var 的值。 ${var:=word} 如果变量 var 为空或已被删除(unset),那么返回 word,并将 var 的值设置为 word。 ${var:?message} 如果变量 var 为空或已被删除(unset),显示message,否则显示变量var。 若此替换出现在Shell脚本中,那么脚本将停止运行。 ${var:+word} 如果变量 var 被定义,那么返回 word,但不改变 var 的值。 可以这么理解,-不改变原来的值,=赋值,+和?相反 **6.运算** +-*/% (加减乘除取于),注意 ```bash val=`expr 2 + 2` echo "Total value : $val" ``` 三点注意: 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。 完整的表达式要被 ` ` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。 乘法必须写成:val=`expr 12 \* 2`,加反斜线 比较运算 -eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 true。 -ne 检测两个数是否相等,不相等返回 true。 [ $a -ne $b ] 返回 true。 -gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。 -lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。 -ge 检测左边的数是否大等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。 -le 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。 布尔运算 ! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。 -o 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。 -a 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false。 **7.字符串拼接** ```bash your_name="qinjx" greeting="hello, "$your_name" !" greeting_1="hello, ${your_name} !" echo $greeting $greeting_1 提取与查找 your_name="0123456789" echo ${your_name:1:6} #123456 string="Alibaba is a great company" echo `expr index "$string" b`#4 ``` 注意 提取的时候,第一位是0开始,查找的时候第一位从1开始,好奇怪~ 查找不到会返回0 **8.数组** ```bash arr=('555' '666' '9999999999') echo "first is :"${arr[0]} echo "all is :"${arr[*]} echo "all is :"${arr[@]} # 取得数组元素的个数 lengtha=${#arr[@]} # 或者 lengthb=${#arr[*]} # 取得数组单个元素的长度 lengthc=${#arr[2]} echo $lengtha echo $lengthb echo $lengthc ``` **9.输出** ```bash echo -e "first is :"${arr[0]}"\nzhuanyi"//必须使用-e转义 printf "first is :"${arr[0]}"\nzhuanyi"//不需要-e ``` **10.if语句** ```bash if ...then... fi 语句; if ...then... else ... fi 语句; if ...then... elif ...then... else ... fi 语句。 ``` **11.case,esac** //==========示例========== ```bash #!/bin/bash option="${1}" case ${option} in -f) FILE="${2}" echo "File name is $FILE" ;; -d) DIR="${2}" echo "Dir name is $DIR" ;; *) echo "`basename ${0}`:usage: [-f file] | [-d directory]" exit 1 # Command to come out of the program with status 1 ;; esac //==========结果=============== $./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] $ ./test.sh -f index.htm $ vi test.sh $ ./test.sh -f index.htm File name is index.htm $ ./test.sh -d unix Dir name is unix ``` 这个例子,开始真没看懂~评论说$1,$2是参数,还没反映过来,看他的运行结果,懂了! **12.for** ```bash for loop in 1 2 3 45 6 do echo $loop done ``` **13\. while** ```bash while nn=0 while [ $nn -lt 5 ] do nn='expr $nn+1' echo $nn done ``` **14.function** ```bash function aa(){ read num if [ $num -gt 10 ] then echo "you are a good boy" else echo "you are a pig" fi } echo "please input a number gt 10" aa funWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" # 参数个数 echo "The string of the parameters is $* !" # 传递给函数的所有参数 } funWithParam 1 2 3 4 5 6 7 8 9 34 73 $# 传递给函数的参数个数。 $* 显示所有传递给函数的参数。 $@ 与$*相同,但是略有区别,请查看Shell特殊变量。 $? 函数的返回值。 ``` **15.输出到文件** ```bash $ `who` > users.txt $ echo 123 > test.txt ``` 如果不希望文件内容被覆盖,可以使用 >> 追加到文件末尾 例如:`$ echo 66666 >> test.txt` **16.文件包含** 创建一个b.sh文件,与t.sh同目录 内容如下: ```bash #!/bin/bash url="http://www.xxx.com" #修改t.sh内容如下 #!/bin/bash . ./b.sh echo $url ``` 注意包含,必须写为.空格文件名
------本文结束
感谢阅读------
上一篇:
(linux学习三)ubuntu常用命令及说明
下一篇:
最近折腾记录-关于Ubuntu16.04