赞
踩
这里是记录我使用bash脚本的常用浓缩代码,很多都是根据下面脚本改造。
注意赋值号左右不能有空格
a=5
b=6
result=$[a+b]
echo "result 为: $result"
#result 为: 11
read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量
#!/bin/sh
read name
echo "$name It is a test"
# sad It is a test
# 1.显示普通字符串 echo "It is a test" # It is a test echo It is a test # It is a test # 2.显示转义字符 echo "\"It is a test\"" # "It is a test" # 3.显示变量 name="zhangsan" echo $name is a good name # zhangsan is a good name echo "$name is a good name" # zhangsan is a good name # 4.显示换行 echo -e "Zhangshan! \n" # -e 开启转义 echo "is a good name" # 输出如下 # Zhangshan! # is a good name # 5.显示不换行 echo -e "Zhangshan! \c" # -e 开启转义 \c 不换行 echo "is a good name" # 输出如下 # Zhangshan! is a good name # 6.显示结果定向至文件 echo "It is a test" > myfile # 7.原样输出字符串,不进行转义或取变量(用单引号) echo '$name\"' # $name\" # 8.显示命令执行结果(用的是反引号 `, 非单引号 ') echo `date` # 2024年 04月 29日 星期一 21:58:21 CST
参数处理 | 说明 |
---|---|
$# | 传递到脚本的参数个数 |
$* | 以一个单字符串显示所有向脚本传递的参数。如"$*“用「”」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 |
$$ | 脚本运行的当前进程ID号 |
$! | 后台运行的最后一个进程的ID号 |
$@ | 与$*相同,但是使用时加引号,并在引号中返回每个参数。如"$@“用「”」括起来的情况、以"$1" “$2” … “$n” 的形式输出所有参数。 |
$- | 显示Shell使用的当前选项,与set命令功能相同。 |
$? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
echo "bash例子"; echo "脚本名称:$0"; echo "脚本运行的当前进程ID号: $$" echo "后台运行的最后一个进程的ID号: $!" echo "参数字符串 : $*" # 循环获取参数 for loop in $@ do echo "参数: $loop" done # 换行 echo -e " \n" if [ $# == 1 ] then echo "第一个参数为:$1"; elif [ $# == 2 ] then echo "第一个参数为:$1"; echo "第二个参数为:$2"; elif [ $# == 3 ] then echo "第一个参数为:$1"; echo "第二个参数为:$2"; echo "第三个参数为:$3"; else echo "参数不对" fi
输出
bash例子
脚本名称:test.sh
脚本运行的当前进程ID号: 279888
后台运行的最后一个进程的ID号:
参数字符串 : 11 33 56
参数: 11
参数: 33
参数: 56
第一个参数为:11
第二个参数为:33
第三个参数为:56
循环语法
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
例子1
for i in 1 2 3 4 5
do
echo "The value is: $i"
done
for f_name in $(ls .)
do
echo "$f_name content:" >> all.txt
cat $f_name >> all.txt
done
语法格式
while condition
do
command
done
例子:
#!/bin/bash
int=1
while [ $int -le 5 ]
do
echo $int
let "int++"
done
其实和其他的编程语言的 if else 一样,
格式可以写多行或者单行,单行就要用分号隔开就好啦
# 多行
if [ condition ]
then
command1
command2
...
commandN
fi
# 单行
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
if else 如果用的是方括号 […] 在判断语句中大于使用 -gt,小于使用 -lt。
if [ "$a" -gt "$b" ]; then
...
fi
如果使用 ((…)) 作为判断语句,大于和小于可以直接使用 > 和 <。
if (( a > b )); then
...
fi
比较运算符
比较符 | 含义 |
---|---|
-eq | 等于则为真 |
-ne | 不等于则为真 |
-gt | 大于则为真 |
-ge | 大于等于则为真 |
-lt | 小于则为真 |
-le | 小于等于则为真 |
a=10
b=20
if [ $a == $b ]
then
echo "a == b"
elif [ $a -gt $b ]
then
echo "a > b"
elif [ $a -lt $b ]
then
echo "a < b"
else
echo "没有符合的条件"
fi
比较符 | 含义 |
---|---|
= | 等于则为真 |
!= | 不相等则为真 |
-z 字符串 | 字符串的长度为零则为真 |
-n 字符串 | 字符串的长度不为零则为真 |
num1="ru1noob" num2="runoob" if test $num1 = $num2 then echo '两个字符串相等!' else echo '两个字符串不相等!' fi num3="" if [ -z num3 ] then echo 'num3 长度为0!' fi if [ -n num2 ] then echo 'num2 长度不为0!' fi #两个字符串不相等! # num2 长度不为0!
这个就是判断文件、目录是否存在、文件是否可读可写可执行、文件是否是字符型还是块设备等判断。
比较符 | 含义 |
---|---|
-e 文件名 | 如果文件存在则为真 |
-r 文件名 | 如果文件存在且可读则为真 |
-w 文件名 | 如果文件存在且可写则为真 |
-x 文件名 | 如果文件存在且可执行则为真 |
-s 文件名 | 如果文件存在且至少有一个字符则为真 |
-d 文件名 | 如果文件存在且为目录则为真 |
-f 文件名 | 如果文件存在且为普通文件则为真 |
-c 文件名 | 如果文件存在且为字符型特殊文件则为真 |
-b 文件名 | 如果文件存在且为块特殊文件则为真 |
cd /bin
if [ -e ./bash ]
then
echo '文件已存在!'
else
echo '文件不存在!'
fi
比如我在桌面上检测文件夹是否存在,不存在就创建对应的文件
# 创建文件夹 if [ -d ~/Desktop/test_fd ] then echo 'Folder exist!' else echo 'Folder no exist!' mkdir ~/Desktop/test_fd echo 'test_df will be created!' fi cd ~/Desktop/test_fd # 创建文件 if [ -e 1.txt ] then echo '1.txt exist!' else echo '1.txt not exist!' touch 1.txt echo '1.txt will be created!' ls -al > 1.txt fi echo '1.txt content: ' ls -al .
输出结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。