[root@fttsaxf ~]# read num 1 [root@fttsaxf ~]# echo $num 11.1.1、read可以同时接受用户输入的多个值,赋值给多个变量
[root@fttsaxf while_import_file]# read -p "请输入用户名和密码:" u_name u_pwd 请输入用户名和密码:feng 123456 [root@fttsaxf while_import_file]# echo $u_name feng [root@fttsaxf while_import_file]# echo $u_pwd 1234561.2、位置变量
$1、$2......
1.2.1、位置变量传参的形式[root@fttsaxf script]# cat position.sh #!/bin/bash echo "第一个位置变量 $1" echo "第二个位置变量 $2" echo "第三个位置变量 $3" echo "所有位置变量的内容:$*" echo "位置变量的数量:$#" echo "脚本名字是:$0" [root@fttsaxf script]# bash position.sh fan feng love 第一个位置变量 fan 第二个位置变量 feng 第三个位置变量 love 所有位置变量的内容:fan feng love 位置变量的数量:3 脚本名字是:position.sh1.2.2、位置变量关于字符串的匹配
[root@fttsaxf script]# cat casev2.sh #!/bin/bash case $1 in start) echo "启动程序" ;; stop) echo "关闭程序" ;; restart|reload) echo "重启程序" ;; *) echo "请检查输入是否正确" ;; esac [root@fttsaxf script]# bash casev2.sh start 启动程序 [root@fttsaxf script]# bash casev2.sh stop 关闭程序 [root@fttsaxf script]# bash casev2.sh reload 重启程序 [root@fttsaxf script]# bash casev2.sh 1235 请检查输入是否正确2、while
while无限循环的两种格式:
1、while : 注意:"while" 和 ":" 之间有一个空格
2、while true
[root@fttsaxf while_import_file]# cat student_info.txt
name age sex grade
cali 36 m 80
liyu 24 m 90
ly 20 f 93
[root@fttsaxf while_import_file]# cat while_import_file.sh
#!/bin/bash
# 方法一:导入
while read u_name u_age u_sex u_grade
do
echo -e "名字是$u_name t性别是$u_sex t年龄为$u_age t分数是$u_grade"
done < student_info.txt
echo "#####################"
# 方法二:
cat student_info.txt | while read u_name u_age u_sex u_grade
do
echo -e "名字是$u_name t性别是$u_sex t年龄为$u_age t分数是$u_grade"
done
[root@fttsaxf while_import_file]# bash while_import_file.sh
名字是name 性别是sex 年龄为age 分数是grade
名字是cali 性别是m 年龄为36 分数是80
名字是liyu 性别是m 年龄为24 分数是90
名字是ly 性别是f 年龄为20 分数是93
#####################
名字是name 性别是sex 年龄为age 分数是grade
名字是cali 性别是m 年龄为36 分数是80
名字是liyu 性别是m 年龄为24 分数是90
名字是ly 性别是f 年龄为20 分数是93
3、字符串的比较
[[]]
[root@fttsaxf rough_book]# cat if.sh read -p "请输入你的名字:" name if [[ $name == "root" ]] then echo "welcome to login" else echo "请输入正确的用户名" fi [root@fttsaxf rough_book]# bash test 请输入你的名字:rot^Ho^H 请输入正确的用户名 [root@fttsaxf rough_book]# bash if.sh 请输入你的名字:root welcome to login3.1、数值比较是双圆括号
(())
[root@fttsaxf while_import_file]# cat test #!/bin/bash if (( $1 > $2)) then echo "第一个数大于第二个数" else echo "第一个数小于第二个数" fi [root@fttsaxf while_import_file]# bash test 1 2 第一个数小于第二个数



