发布时间:2025-12-09 16:21:35 浏览次数:4
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
显示shell中所有变量:set
说明:在终端输入sh进入到shell交互式界面,当然也可以直接在黑屏终端下操作测试,使用exit回退到终端
$n n为数字,$0代表该脚本名称,$1-$9代表第一到第九个参数,十以上的参数需要用大括号包含,如${10}
sh-4.2$ vim test2.shsh-4.2$ chmod u+x test2.sh sh-4.2$ ./sh: ./: 是一个目录sh-4.2$ ./test2.sh 1 2 3 4 5 6 7 8 9 10文件夹的名字是 :./test2.sh第一个参数:1第二个参数:2第三个参数:3第四个参数:4第五个参数:5第六个参数:6第七个参数:7第八个参数:8第九个参数:9第十个参数:10$*与$@ : $* 功能描述:这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体
$@ 功能描述:这个变量也代表命令行中所有的参数,不过$@把每个参数区分对待
$? : 最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0,则证明上一个命令执行不正确了。
1 #!/bin/bash2 3 echo '------------$*--------------'4 for i in $*5 do6 # echo "(带引号)i的值为:$i" 7 echo "(不带引号)i的值为:" $i8 done9 10 11 echo '------------$@--------------'12 for j in $@13 do14 # echo "(带引号)j的值为:$j" 15 echo "(不带引号)j的值为:" $j16 done sh-4.2$ sh test12.sh 1 2 3------------$*--------------(不带引号)i的值为: 1(不带引号)i的值为: 2(不带引号)i的值为: 3------------$@--------------(不带引号)j的值为: 1(不带引号)j的值为: 2(不带引号)j的值为: 3sh-4.2$ vim test12.sh ------------$*--------------(不带引号)i的值为: 1 2 3------------$@--------------(不带引号)j的值为: 1(不带引号)j的值为: 2(不带引号)j的值为: 31 #!/bin/bash2 3 echo '------------$*--------------'4 for i in "$*"5 do6 # echo "(带引号)i的值为:$i" 7 echo "(不带引号)i的值为:" $i8 done9 10 11 echo '------------$@--------------'12 for j in "$@"13 do14 # echo "(带引号)j的值为:$j" 15 echo "(不带引号)j的值为:" $j16 done选项值:
-p 指定读取值时的提示符
-t 指定读取值时等待的时间(秒)
1 #!/bin/bash2 3 4 read -p "请输入第一个参数:" num15 6 echo "您输入的第一个参数是 $num1 "7 8 9 read -t 3 -p "请在3秒内输入第二个参数:" num210 11 echo "您输入的第二个参数是 $num2"-lt:小于(less than)
-le:小于等于(less equal)
-eq:等于(equal)
-ne:不等于(not equal)
-gt:大于(greater than)
-ge:大于等于(greater equal)
-r:有读的权限(read)
-w:有写的权限(write)
-x:-x 有执行的权限(execute)
sh-4.2$ echo [ -r test3.sh ][ -r test3.sh ]sh-4.2$ [ -r test3.sh ]sh-4.2$ echo $?0sh-4.2$ [ -x test3.sh ]sh-4.2$ echo $?1sh-4.2$ ls -l总用量 16-rwxrw-r--. 1 atguigu atguigu 33 11月 21 20:59 helloworld.sh-rwxrw-r--. 1 atguigu atguigu 64 11月 21 21:20 test01.sh-rwxrw-r--. 1 atguigu atguigu 331 11月 21 21:26 test2.sh-rw-rw-r--. 1 atguigu atguigu 210 11月 21 21:41 test3.sh按照文件类型判断:
-f:文件存在并且是一个常规的文件(file)
-d:文件存在并是一个目录(directory)
-e:文件存在(existence)
sh-4.2$ [ -f test]test01.sh test2.sh test3.sh test4.sh/ sh-4.2$ [ -f test3.sh ]sh-4.2$ echo $?0sh-4.2$ [ -d test4.sh ]sh-4.2$ echo $?-a、-o、!:在中括号内使用
&&、||、!:在中括号外使用,计算多个中括号中的条件判断
sh-4.2$ [ -r test4.sh -a -w test4.sh -a -x test4.sh/ ]sh-4.2$ echo $?0sh-4.2$ ls -l总用量 16-rwxrw-r--. 1 atguigu atguigu 33 11月 21 20:59 helloworld.sh-rwxrw-r--. 1 atguigu atguigu 64 11月 21 21:20 test01.sh-rwxrw-r--. 1 atguigu atguigu 331 11月 21 21:26 test2.sh-rw-rw-r--. 1 atguigu atguigu 210 11月 21 21:41 test3.shdrwxrwxr-x. 2 atguigu atguigu 6 11月 21 21:48 test4.sh -rwxrw-r--. 1 atguigu atguigu 33 11月 21 20:59 helloworld.sh-rwxrw-r--. 1 atguigu atguigu 64 11月 21 21:20 test01.sh-rwxrw-r--. 1 atguigu atguigu 331 11月 21 21:26 test2.sh-rw-rw-r--. 1 atguigu atguigu 210 11月 21 21:41 test3.shdrwxrwxr-x. 2 atguigu atguigu 6 11月 21 21:48 test4.shsh-4.2$ [ -r test4.sh/ ] && [ -w test4.sh/ ] && [ -x test4.sh/ ]sh: [: 参数太多sh-4.2$ [ -r test4.sh ]&&[ -w test4.sh ]&&[ -x test4.sh/ ]sh: [: 参数太多sh-4.2$ [ -r test4.sh/ ] && [ -w test4.sh/ ]sh-4.2$ echo $?0sh-4.2$ [ -r test4.sh/ ]&&[ -w test4.sh/ ]sh-4.2$ echo $?if [ 条件判断式 ]
then
语句1
else
语句2
fi
语法:
case $变量名 in
"值1")
语句1
;;
"值2")
语句2
;;
……
*)
语句*
;;
esac
与java一样
1 #!/bin/bash2 3 echo "----------------break-------------------"4 for ((i=1;i<=10;i++))5 do6 if [ $i -eq 5 ]7 then8 break9 fi10 echo "i的值为:$i"11 done12 13 echo "----------------continue-------------------"14 15 for ((i=1;i<=10;i++))16 do17 if [ $i -eq 5 ]18 then19 continue20 fi21 echo "i的值为:$i" 22 done23基本语法:basename [string/pathname] [suffix]
basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来
选项suffix:如果suffix被指定了,basename会将pathname或string中的suffix去掉
sh-4.2$ basename /a/b/c/d/e.txte.txtsh-4.2$ basename /a/b/c/d/e.txt .txtesh-4.2$ basename /a/b/c/d/e.txt txt基本语法:dirname 文件绝对路径
功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分)
sh-4.2$ dirname /a/b/c/d/e.txt/a/b/c/dsh-4.2$[function] funname [()]
{
action;
[return int;]
}
| function | 说明定义函数,可以省略 |
| funname | 自定义函数名,遵循标识符规则 |
| () | 可以省略,和function不能同时省略 |
| action | 封装的功能 |
| return int | 函数返回值,只能通过$?系统变量来获取,可以显示添加。如果不添加,将以最后一条命令运行的结果作为返回值。注意return后的数值在[0~255]之间,如果超过255,将返回该值与256的余数。 |
| ; | 语句结束标志,可以不写 |
funname [……]
echo "函数的返回值为:$?"
| funname | 要调用的函数名 |
| …… | 传递参数,如果无需传参即省略 |
| $? | 获取函数的返回值 |
| "" | 需要使用双引号,可以使用特殊变量 |