linux下的shell脚本的书写
程序员文章站
2022-06-24 19:40:23
...
shell 脚本语言(解释性语言) (可以运行的文本,存的是命令)
C C++ 描述性语言 yum install gcc -y 安装C语言的编译器
脚本的存在意义:可以重复批量的执行
1.shell脚本的编写(shell是一个解释器)
#!/bin/bash 幻数
脚本书写时自动加入下面内容(添加脚本在书写时每个文件中都应该书写的内容,以便脚本的
vim /etc/vimrc
"map <F11> ms:call WESTOS()<cr>'s 按F11时添加
autocmd BufNewFile *.sh exec ":call WESTOS()" 新建的.sh文件自动添加
func WESTOS ()
call append(0,"################################")
call append(1,"# Author: wang #")
call append(2,"# Version: #")
call append(3,"# Mail: #")
call append(4,"# Date: ".strftime("%Y-%m-%d").(" #"))
call append(5,"# Description: #")
call append(6,"# #")
call append(7,"################################")
call append(8," ")
call append(9,"#!/bin/bash")
endfunc
2.diff命令
diff file file1 比较文件的不同(以第二个文件为主)
diff -u file file1 比较文件的不同并生成补丁
diff -u file file1 > file.path 将补丁导入文件中(也可以不以.path结尾)
diff -r westos/ mnt/ 比较两个目录
yum install patch -y 安装打补丁的软件
patch file file.path 给file文件打补丁,使得file和file1相同
patch -b file file.path 给file文件打补丁,并保留原文件.orig文件
3.cut命令(截取文件)
cut -d : -f 1 file 截取该文件的第一列 (-d 后跟分隔符)
cut -d : -f 1,3 file 截取文件的第一列和第三列
cut -d : -f 1-3 file 截取文件的1,2,3列
cut -d : -f 3- file 截取文件第三列以后的所有列
cut -c 1 file 截取文件的第一个字符
例1、只显示ip
ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
sh -x 脚本名 脚本排错 + 命令 没有+的是命令运行结果
4.&&和||
例2、在ping 任意ip时,通输出up,不通输出down
ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down
5.sort和uniq
sort -n 按纯属字排序(升序)
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件
sort -t 指定分隔符
sort -k 指定要排序的列
uniq -c 输出每个的个数并将重复的只输出一次
uniq -d 输出重复的
uniq -u 输出不重复的
例3、输出/mnt下的最大的文件名
ls -Sl /mnt/ | grep -v total | awk -F " " 'NR==1{print $9}'
6.test命令
test命令和[]等同 []两边必须有空格
[ "$A" = "$B" ] =
[ "$A" != "$B" ] !=
[ "$A" -eq "$B" ] =
[ "$A" -ne "$B" ] !=
[ "$A" -le "$B" ] <=
[ "$A" -lt "$B" ] <
[ "$A" -ge "$B" ] >=
[ "$A" -gt "$B" ] >
[ "$A" -ne "$B" -a "$A" -gt "$B" ] !=并且>
[ "$A" -ne "$B" -o "$A" -gt "$B" ] !=或>
[ -z "$A" ] 不为空
[ -n "$A" ] 空
例4、判断一个数是不是10以内的数
[ -z "$1" ]&& {
echo "please give me a number!!!"
exit 1
}
[ "$1" -lt "10" -a "$1" -gt "0" ] && {
echo "$1 is between 1~10"
}|| {
echo "$1 is not between 1~10"
}
[ "file1" -ef "file2" ] 判断两个的节点(大小)互为硬链接
[ "file1" -nt "file2" ] file1的建立时间早于file2
[ "file1" -ot "file2" ] file1的建立时间迟于file2
[ -e " file" ] 是否存在
[ -f " file" ] 是否普通文件
[ -L " file" ] 是否软链接
[ -b " file" ] 是否是块设备
[ -S " file" ] 是否套接字
[ -d " file" ] 是否是目录
[ -c " file" ] 是否是字符设备
例5:检测文件的类型
[ -z "$1" ]&&{
echo "please input a file name after script!!!"
exit 1
}
[ -e "$1" ]||{
echo "$1 is not exist!!!"
exit 0
}
[ "-L" "$1" ]&&{
echo "$1 is a link"
exit 0
}
[ "-f" "$1" ]&&{
echo "$1 is a common file"
exit 0
}
[ "-b" "$1" ]&&{
echo "$1 is a block file"
exit 0
}
[ "-S" "$1" ]&&{
echo "$1 is a S file"
exit 0
}
[ "-d" "$1" ]&&{
echo "$1 is a dir "
exit 0
}||{
echo "$1 is a file"
}
例6、输入无论大小写时均可通过(tr 转换大小写)
word=$(echo $1 | tr 'A-Z' 'a-z')
[ "$word" = "hello" ]&& {
echo yes
}||{
echo no
}
例7、创建用户用户存在时输出存在,不存在时创建该用户
[ "$#" -eq "2" ]||{
echo "please input username and password after script!!!"
exit 1
}
Check_User=`getent passwd $1`
[ -n "$Check_User" ]&&{
echo "$1 is exist!!!"
exit 1
}||{
useradd $1
echo $2 | passwd --stdin $1
}