Shell的输入输出
-
示例1 从键盘 接收一个字符串并输出到屏幕上。
方法二
代码如下:
#!/bin/bash read -p "Pleast input a string:" str echo "You input is $str"
运行如下:
[root@localhost hexo]# vim input-output2.sh [root@localhost hexo]# bash input-output2.sh Pleast input a string:demo You input is demo
-
示例2:编写一个shell脚本,从键盘接收一个文件名,创建该文件,再将/hexo/input-output1.sh文件中的内容copy到该文件中。
代码如下:
#!/bin/bash echo "Please input a filename:" read f_name touch #f_name cp input-output1.sh $f_name
运行如下:
[root@localhost hexo]# vim creat-copy.sh [root@localhost hexo]# bash creat-copy.sh Please input a filename: demo touch: missing file operand Try 'touch --help' for more information. [root@localhost hexo]# cat demo #!/bin/bash echo "Please input a string:" read str echo "You input is:$str"
-
示例3:echo命令与重定向的使用
运行如下:
[root@localhost hexo]# cat file [root@localhost hexo]# echo hello >> file [root@localhost hexo]# cat file hello [root@localhost hexo]# echo "The time is `date`" The time is Mon Oct 26 00:41:12 PDT 2020 [root@localhost hexo]# echo "The time is `date`" >> test [root@localhost hexo]# cat test hello haha The time is Sat Oct 24 09:46:57 PDT 2020 The time is Mon Oct 26 00:41:28 PDT 2020
Shell变量
内容变量
内部变量,也称系统变量或预定义变量或Shell变量,由系统提供,不用定义,不能修改脚本调用时,常用此变量对参数和命令返回值进行判断。
变量名 | 含义 |
---|---|
$# | 命令行参数的个数 |
$n | 位置参 数,$1表示第 一个 参数,$2表示第二 个参数,以此类推 |
$0 | 当前程序的名称 |
$? | 前一个命令或函数的返回码 0表示成功 |
$* | 以"参数1 参数2 … "形式保存 所有 参数 |
$@ | 以"参数1"“参数2” … 形式保存所有参数 |
$$ | 本程序的(进程ID号)PIDPID |
$! | 上一个命令的PID |
-
使用举例
代码如下:
#!/bin/bash echo "This script name is $0" echo "You've input $# paramenters. They are $*" echo "The 1st paramenters is :$1" echo "The 2ed paramenters is :$2" echo "The PID of this program is $$" echo "The return code of teh last command is $?"
运行如下:
[root@localhost hexo]# vim InterArg.sh [root@localhost hexo]# bash InterArg.sh This script name is InterArg.sh You've input 0 paramenters. They are The 1st paramenters is : The 2ed paramenters is : The PID of this program is 10612 The return code of teh last command is 0
环境变量
当Shell程序启动时,都自动设置 一组变量,这组变量就是环境 变量。
全局环境变量在 /etc/profile 文件中设置;
用户环境变量在用户家目录下的 .bash_profile 或 .bashrc 文件中设置
**常用的环境 变量:**PATH,HO ME,SHELL
定义新的环境变量:
1)间的接口执行export命令(重启后失效)间的接口
2)间的接口修改.bash_profile文件 -> export 变量-> source .bash_profile
用户变量
**定义变量:**即用即定义,无变量类型。变量名只能由字母、数字和下划线组成且不能以数字开头
调用变量:
字符串 数组 运算符
Shell字符串
**字符串定义:**单引号、双引号或不用引号表示的一串字符,一般情况 下建议用双引号表示
单引号里的任 何字符都会原样输出,不会处理变量
双引号里可以有变量和转义字符
**字符串拼接:**使用变量进行拼接
获取字符串长度:
Shell数组
**数组定义:**Bash Shell 只支持一维数组,初始化时不需要定义数组 大小数组 元素的下标由0开始。Shell 数组用括号来表示,元素用"空格"符号分割
读取数组中的元素:
读取整个数组:
-
使用举例
代码如下:
#!/bin/bash arr=(1 "mandy" "while there is life,there is hope") echo "First element is ${arr[0]}" echo "Second element is ${arr[1]}" echo "Third element is ${arr[2]}" echo "The whole array is ${arr[*]}"
运行如下:
[root@localhost hexo]# vim read-array.sh [root@localhost hexo]# bash read-array.sh First element is 1 Second element is mandy Third element is while there is life,there is hope The whole array is 1 mandy while there is life,there is hope
Shell运算符
运算符 | 解释 |
---|---|
( ) [ ] | 括号 |
! ~ ++ – + - | 取反、按位取反、自增、自减、正负号 |
+ - * / % | 加、减、乘、除、取模 |
<< >> | 左移 、右移 |
< <= >= > | 小于、小于等于、大于等于、大于 |
== != | 等于、不等于 |
& ^ | | 与、异或、或 |
&& || | 逻辑与、逻辑或 |
? : | 条件 |
= + = -= *= /= &= ^= | 赋值 |
, | 逗号 |
-
使用举例
代码如下:
#!/bin/bash a=5 b=4 echo ${a}+${b} let sum=a+b let sub=a-b let mul=a*b let div=a/b echo "the sum is $sum" echo "the sub is $sub" echo "the mul is $mul" echo "the div is $div" echo "Sum : $a + $b = $[$a + $b]" echo "Sub : $a - $b = $[$a - $b]" echo "Mul : $a * $b = $[$a * $b]" echo "Div : $a / $b = $[$a / $b]"
运行如下:
[root@localhost hexo]# vim arithmetic.sh [root@localhost hexo]# bash arithmetic.sh 5+4 the sum is 9 the sub is 1 the mul is 20 the div is 1 Sum : 5 + 4 = 9 Sub : 5 - 4 = 1 Mul : 5 * 4 = 20 Div : 5 / 4 = 1
Shell程序
Shell程序调试
语法格式: bash -x/-v 脚本文件名
• -n 用于检查语法错误,不会执行程序
• -x 逐行显示执行的命令和结果,类似单步调试-
• -v 一边读取命令一边显示结果,与-x的不同是-x只显示能执行的命令
Shell选择结构
test命令
**命令格式:**test 表达式或[ 表达式 ]
**命令功能:**用来判断表达式的结果。
作用: 进行数值、字符和文件三种类型的测试
用来判断表达式的返回值
如果表达式为真,返回值为0(TRUE)
如果表达式为假,返回值为1(FALSE)
-
使用举例
代码如下:
#!/bin/bash a=100 b=50 test ${a} -eq ${b} echo $?
运行如下:
[root@localhost hexo]# vim judge.sh [root@localhost hexo]# bash judge.sh 1
if语句
分支格式
不同分支语句格式:
要点:
if语句进行数值、字符和文件三种类型的测试
[ ] 中条件表达式两边与括号之间,if与括号间都要有空格
if…then语句如果放在同一行,条件语句后必须要有分号;不推荐
数值比较
参数 | 说明 |
---|---|
-eq (equal to) | 等于则为真 |
-ne (not equal to) | 不等于则为真 |
-gt (greater than) | 大于则为真 |
-ge (greater or equal) | 大于等于则为真 |
-lt (less than) | 小于则为真 |
-le (less or equal) | 小于等于则为真 |
-
举例:输入两个数比较大小
代码如下:
#!/bin/bsah echo "Plaese input num1:" read num1 echo "Please input num2:" read num2 if [ ${num1} -eq ${num2} ] then echo "${num1} is equal to ${num2}" elif [ ${num1} -gt ${num2} ] then echo "${num1} is greater than ${num2}" else echo "${num1} is less than ${num2}" fi
运行如下:
[root@localhost hexo]# vim num-comp.sh [root@localhost hexo]# bash num-comp.sh Plaese input num1: 4 Please input num2: 6 4 is less than 6 [root@localhost hexo]# bash num-comp.sh Plaese input num1: 5 Please input num2: 5 5 is equal to 5 [root@localhost hexo]# bash num-comp.sh Plaese input num1: 6 Please input num2: 4 6 is greater than 4
字符比较
参数 | 说明 |
---|---|
= | 等于则为真 |
!= | 不相等则为真 |
-z 字符串 | 字符串的长度为零则为真 |
-n 字符串 | 字符串的长度不为零则为真 |
-
示例:检查输入的字符串是否为空
代码如下:
#!/bin/bash echo "PLease input a string:" read str1 if [ -z ${str1} ] then echo "This string is null." else echo "This string is not null." fi
运行如下:
[root@localhost hexo]# vim char-comp.sh [root@localhost hexo]# bash char-comp.sh PLease input a string: This string is null. [root@localhost hexo]# bash char-comp.sh PLease input a string: test This string is not null.
文件测试
参数 | 说明 |
---|---|
-e 文件名 | 如果文件存在则为真 |
-r 文件名 | 如果文件存在且可读则为真 |
-w 文件名 | 如果文件存在且可写则为真 |
-x 文件名 | 如果文件存在且可执行则为真 |
-s 文件名 | 如果文件存在且至少有一个字符则为真 |
-d 文件名 | 如果文件存在且为目录则为真 |
-f 文件名 | 如果文件存在且为普通文件则为真 |
-c 文件名 | 如果文件存在且为字符型特殊文件则为真 |
-b 文件名 | 如果文件存在且为块特殊文件则为真 |
-
示例: 用户输入一串字符,判断/hexo/testlog.txt是否存在,如果存在,写入字符,如果不存在,则创建文件后写入。
代码如下:
#!/bin/bash echo "Please input a string:" read str filename=testlog.txt if [ ! -e ${filename} ] then touch ${filename} fi echo ${str} >> ${filename}
运行如下:
[root@localhost hexo]# vim filetest.sh [root@localhost hexo]# bash filetest.sh Please input a string: It is never too old to learn. [root@localhost hexo]# cat testlog.txt It is never too old to learn.
逻辑连接操作
Shell中的逻辑连接操作符:与( -a )、或( -o )、非( ! )优先级:“!” >“ -a” > “-o”
-
示例1:编写shell脚本,判断 /etc目录下是否同时存在hostname与hosts文件,并输出结果。
代码如下:
#!/bin/bash filename1=/etc/hostname filename2=/etc/hosts if [ -e ${filename1} -a -e ${filename2} ] then echo "${filename1} and ${filename2} all exist" else echo "${filename1} or ${filename2} does not exist" fi
运行如下:
[root@localhost hexo]# vim log-con.sh [16]+ Stopped vim log-con.sh [root@localhost hexo]# bash log-con.sh /etc/hostname and /etc/hosts all exist
-
示例2:变量是否为y或者Y
代码如下:
#!/bin/bash SAN="A" if [ $SAN = "Y" -o $SAN="y" ] then echo "It is y or Y" else echo "It is not y or Y" fi
运行如下:
[root@localhost hexo]# vim log-con2.sh [root@localhost hexo]# bash log-con2.sh It is y or Y
case语句
语句格式如下:
-
使用举例
代码如下:
#!/bin/bash case $1 in 1) echo "Monday" ;; 1) echo "Tuesday" ;; 3) echo "Wednesday" ;; 4) echo "Tursday" ;; 5) echo "Friday" ;; esac
运行如下:
[root@localhost hexo]# vim casetest1.sh [root@localhost hexo]# bash casetest1.sh 1 Monday [root@localhost hexo]# bash casetest1.sh 3 Wednesday [root@localhost hexo]# bash casetest1.sh 5 Friday
说明:
取值可以为变量或常量
执行完匹配模式的命令后不再继续其他模式
模式字符串中可以使用通配符:
当一个模式字符串中包含多个模式时,可以使用“|”隔开,表示“或”的关系
case的各模式之间不应重复出现
-
示例:用户从键盘输入一个字符,然后判断该字符是否为字母、数字或其他字符,并输出相应的提示信息
代码如下:
#!/bin/bash read -p "Please input a character:" key case ${key} in [a-z]|[A-Z]) echo "${key} is a letter.";; [0-9]) echo "${key} is digit.";; *) echo "${key} is not a letter or digit.";; esac
运行如下:
[root@localhost hexo]# vim casetest2.sh [root@localhost hexo]# bash casetest2.sh Please input a character:a a is a letter. [root@localhost hexo]# bash casetest2.sh Please input a character:4 4 is digit. [root@localhost hexo]# bash casetest2.sh Please input a character:% % is not a letter or digit.
Shell循环结构
for语句
语句格式如下:
说明:
循环in列表中的值执行命令
in列表可以由命令生成
正则表达式
由命令生成
脚本参数列表
字符串
文件名
数组
-
示例1:输出列表中的所有值
代码如下:
#!/bin/bash for str in "redhat" "centos" "ubunto" "susi" do echo $str done array=(redhat centos ubunto susi) for name in ${array[@]} do echo $name done
运行如下:
[root@localhost hexo]# vim fortest1.sh [root@localhost hexo]# bash fortest1.sh redhat centos ubunto susi redhat centos ubunto susi
-
示例2:备份当前目录下所有.sh文件
代码如下:
#!/bin/bash mkdir fortest for file in `ls*.sh` do cp $file fortest/$file.sh done
-
示例3:输入一个数n,求从1到该数的和 (C语言风格)
代码如下:
#!/bin/bash read -p "Please input a number:" input sum=0 for ((i=0;i<=${input};i++)) do let sum=i+sum done echo "Sum=$sum"
运行如下:
[root@localhost hexo]# vim fortest3.sh [root@localhost hexo]# bash fortest3.sh Please input a number:5 Sum=15