本篇博客参考:https://www.cnblogs.com/jjzd/p/6014301.html
1、如何给脚本传递参数:
./script argument
栗子1:传递一个参数给脚本,若参数为q或Q退出脚本,否则就显示用户参数
[root@server1 shell]# ./argument.sh s
UnkownAgruments:s
[root@server1 shell]# ./argument.sh q
quiting......
[root@server1 shell]# ./argument.sh Q
quiting......
[root@server1 shell]# cat argument.sh
#!/bin/bash
if [ $1 == q -o $1 == Q ];then
echo "quiting......"
exit 0
else
echo "UnkownAgruments:$1"
fi
【注意】: -o:或 exit 0:正常退出
[ $1 == q -o $1 == Q ] 中[]两边必须有空格
栗子2:传递一个存在的用户,判断其UID与GID是否一样,一样则显示为“good guy“,否则显示“bad guy“
[root@server1 shell]# ./guy.sh root
root is good guy
[root@server1 shell]# ./guy.sh nginx
nginx is bad guy
[root@server1 shell]# cat guy.sh
#!/bin/bash
uid=`id -u $1`
gid=`id -g $1`
if [ $uid -eq $gid ];then
echo $1 is good guy
else
echo $1 is bad guy
fi
不使用id命令获得其id号
[root@server1 shell]# ./guy1.sh mysql
mysql is good guy
[root@server1 shell]# ./guy1.sh saslauth
saslauth is bad guy
[root@server1 shell]# ./guy1.sh nginx
nginx is bad guy
[root@server1 shell]# cat guy1.sh
#!/bin/bash
uid=`grep "^$1" /etc/passwd | cut -d: -f3`
gid=`grep "^$1" /etc/passwd | cut -d: -f4`
if [ $uid -ne $gid ];then
echo $1 is bad guy
else
echo $1 is good guy
fi
栗子3:给脚本传递一个参数,如果是一个文件则输出”file”,如果是一个目录则输出”dir”,否则输出”No”
[root@server1 shell]# ./fildir.sh /mnt/
dir
[root@server1 shell]# ./fildir.sh /mnt/Makefile
file
[root@server1 shell]# cat fildir.sh
#!/usr/bin/env bash
if [ -d $1 ];then
echo "dir"
elif [ -f $1 ];then
echo "file"
else
echo "No"
fi
2、计算传递进来的参数个数
[root@server1 shell]# ./like.sh ss aa ww
ss like apple,aa like banana,ww like orange,3
[root@server1 shell]# ./like.sh ss aa
ss like apple,aa like banana, like orange,2
[root@server1 shell]# ./like.sh ss aa ss rrr
ss like apple,aa like banana,ss like orange,4
[root@server1 shell]# cat like.sh
#!/usr/bin/env bash
echo $1 like apple,$2 like banana,$3 like orange,$#
3、在脚本里获得脚本名称
输出的脚本名称与执行路径相同
[root@server1 ~]# ./name.sh
./name.sh
[root@server1 ~]# /root/name.sh
/root/name.sh
4、前面的命令是否运行成功
[root@server1 ~]# /root/name.sh
/root/name.sh
0
/root/name.sh: line 4: ehoc: command not found
127
[root@server1 ~]# cat name.sh
#!/bin/bash
echo $0
echo $?
ehoc xixi
echo $?
[root@server1 ~]# cdscdc
-bash: cdscdc: command not found
[root@server1 ~]# echo $?
127
[root@server1 ~]# cd
[root@server1 ~]# echo $?
0
5、 如何调试 bash 脚本
(1)将 -xv 参数加到 #!/bin/bash 后
栗子:
[root@server1 ~]# ./name.sh
#!/bin/bash -xv
echo $0
+ echo ./name.sh
./name.sh
echo $?
+ echo 0
0
if [$? -eq 0 ]
then
echo "OK"
./name.sh: line 8: syntax error: unexpected end of file
(2)bash -v 脚本
[root@server1 ~]# bash -x name.sh
+ echo name.sh
name.sh
+ echo 0
0
name.sh: line 8: syntax error: unexpected end of file
6、检查脚本中是否存在语法错误
[root@server1 ~]# bash -n name.sh
name.sh: line 8: syntax error: unexpected end of file
[root@server1 ~]# cat name.sh
#!/bin/bash
echo $0
echo $?
if [$? -eq 0 ]
then
echo "OK"
7、如果某文件的某行第一个元素为“SS“,输出第二个元素
[root@server1 shell]# awk '{if($1=="SS") print$2 }' xixi sds
[root@server1 shell]# cat xixi
SSS ee aa
SSS ll sasa
SSS sdsdsd sdccdc
SSS dcdc dcdcd
SS sds dsd
某行第一个元素为“SSS“,输出第2,3个元素
[root@server1 shell]# awk '{if($1=="SSS") print$2,$3}' xixi
ee aa
ll sasa
sdsdsd sdccdc
dcdc dcdcd
8、 举例如何写一个函数
[root@server1 shell]# ./fundction.sh
xixi is me
[root@server1 shell]# cat fundction.sh
#!/bin/bash
function haha()
{
echo $1 is me
}
haha xixi
9、如何连接两个字符串
[root@server1 shell]# ./connect.sh
hello wangtian
[root@server1 shell]# cat connect.sh
#!/bin/bash
v1="hello "
v2="wangtian"
v3=${v1}${v2}
echo $v3
10、两个整数如何进行相加
(1)let
[root@server1 shell]# A=1
[root@server1 shell]# B=2
[root@server1 shell]# let D=$A+$B
[root@server1 shell]# echo $D
3
(2)C=$(($A+$B))
[root@server1 shell]# C=$(($A+$B))
[root@server1 shell]# echo $C
3
(3)C=$[$A+$B]
[root@server1 shell]# E=$[$A+$C]
[root@server1 shell]# echo $E
4
(4)expr 算术表达式,表达式各操作数及运算符之间要有空格
[root@server1 shell]# F= expr $A + $C
4
(5)bc
[root@server1 shell]# echo $A+$B | bc
3.5
11、检查某文件是否存在
参考1的栗子
12、shell 脚本中所有循环语法
until命令和while命令类似,while能实现的脚本until同样也可以实现,但区别是until循环的退出状态是不为0,退出状态是为0(与while刚好相反),即whie循环在条件为真时继续执行循环而until则在条件为假时执行循环。
(1)for循环(do和for分行写,则不加分号)
for 变量 in 列表;do
循环体
done
(2)while
while 条件;do
循环体
done
(3)untiil
until 条件; do
循环体
done
栗子:1..100求和
[root@server1 shell]# ./add.sh
5050
[root@server1 shell]# cat add.sh
#!/bin/bash
sum=0
for i in {1..100};do
let sum+=i
done
echo $sum
[root@server1 shell]# ./add1.sh
5050
[root@server1 shell]# cat add1.sh
#!/bin/bash
i=0
sum=0
while [ $i -le 100 ];do
let sum+=i
let i+=1
done
echo $sum
[root@server1 shell]# ./add1.sh
5050
[root@server1 shell]# cat add1.sh
#!/bin/bash
i=0
sum=0
until [ $i -gt 100 ];do
let sum+=i
let i+=1
done
echo $sum
13、 bash 脚本文件的第一个符号是什么
# ###qwq####不懂
14、
命令:[ -z "" ] && echo 0 || echo 1 的输出是什么
0
[ -a FILE ] 如果 FILE 存在则为真。
[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。
[ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真。
[ -d FILE ] 如果 FILE 存在且是一个目录则为真。
[ -e FILE ] 如果 FILE 存在则为真。
[ -f FILE ] 如果 FILE 存在且是一个普通文件则为真。
[ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。
[ -h FILE ] 如果 FILE 存在且是一个符号连接则为真。
[ -k FILE ] 如果 FILE 存在且已经设置了粘制位则为真。
[ -p FILE ] 如果 FILE 存在且是一个名字管道(F如果O)则为真。
[ -r FILE ] 如果 FILE 存在且是可读的则为真。
[ -s FILE ] 如果 FILE 存在且大小不为0则为真。
[ -t FD ] 如果文件描述符 FD 打开且指向一个终端则为真。
[ -u FILE ] 如果 FILE 存在且设置了SUID (set user ID)则为真。
[ -w FILE ] 如果 FILE 如果 FILE 存在且是可写的则为真。
[ -x FILE ] 如果 FILE 存在且是可执行的则为真。
[ -O FILE ] 如果 FILE 存在且属有效用户ID则为真。
[ -G FILE ] 如果 FILE 存在且属有效用户组则为真。
[ -L FILE ] 如果 FILE 存在且是一个符号连接则为真。
[ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read则为真。
[ -S FILE ] 如果 FILE 存在且是一个套接字则为真。
[ FILE1 -nt FILE2 ] 如果 FILE1 has been changed more recently than FILE2,or 如果 FILE1 exists and FILE2 does not则为真。
[ FILE1 -ot FILE2 ] 如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。
[ FILE1 -ef FILE2 ] 如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。
[ -o OPTIONNAME ] 如果 shell选项 “OPTIONNAME” 开启则为真。
[ -z STRING ] “STRING” 的长度为零则为真。
[ -n STRING ] or [ STRING ] “STRING” 的长度为非零 non-zero则为真。
15、 命令 “export” 有什么用 ?
export用于声明一个环境变量
export variable=value
该环境变量只在本进程和其子进程中可以访问
如果是在系统级的配置文件中,如/etc/profile中export 一个变量,那么这个变量就会在整个系统运行期间都起作用。
如果在用户级~/.bash_profile之类的文件中,则每次启动shell都会去读这个文件,所以每次打开shell也是可以取到这个值的。
如果在某一次运行中,手动export一个变量,则只在这次shell的使用中,才能访问这个变量,或者在该shell启动的其他程序中,也是可以访问这个变量的,因为它们是这个shell的子进程。
nux的桌面程序,是不读取个人目录下的~/.bash_profile等配置文件的,因此这些export的变量对用户桌面程序是没有意义的
linux系统中的export命令在命令帮助手册上有这样的描述:export的效力仅及于该此登陆操作,登录,指的是shell的生命周期。