shell脚本学习积累

创建shell脚本并运行

方式1:
vim myshell.sh
#文件开头是#!/bin/bash 或 #!/bin/sh
sh myshell.sh

方式2:
vim myshell
#需要设置文件为可执行的状态
chmod +x myshell
#作为可执行文件直接执行,有的后面可带输入参数
./myshell

条件测试

博主-条件测试传送门

条件测试中-z到-d参数的意思

&&的妙用,并用echo来反馈前一个命令任务是否成功:

零碎知识

变量与正则化

# 使用命令结果赋给变量,注意命令外面一定使用、、(这个不是常规的单引号)
PWD = ·pwd·

# find命令中模糊寻找(其中:一定要用双引号而不是单引号, -printf '%f\n'打印时可以单行打印)
find /root/rpmbuild/SRPMS -name "${NAME}-[0-9]*" -printf '%f\n'

基础shell脚本

打包过去24小时内修改过的文件

#!/bin/bash
tar -zcvf lastmodify.tar.gz `find . -mtime 1 -type f -print`

注意`` 可以将【一个命令的输出】作为作为【另一个命令的输入参数】,其中mtime是过去24小时修改的意思

自动解压 bzip2, gzip 和 zip不同类型的压缩包

创建一个smartzip的脚本实现,能实现自动的解压缩bzip2, gzip 和 zip不同类型的压缩包。
思路:利用file命令首先判断文件的类型,按照字符串的匹配来使用不同的解压缩命令进行解压缩

#!/bin/sh
# USAGE:smartzip file.zip
# EXAMPLE:smartzip articles.zip

#ftype变量是查询文件类型后的字符串信息,利用case来进行字符串匹配
#$1 就是脚本命令中输入参数的第一个
ftype=`file "$1"`
case "$ftype" in
#file查出的信息开头就是那个文件的名字,所以字符串拼接头就是$1,冒号后开始具体的文件类型
"$1: Zip archive"*)
unzip "$1" ;;
"$1: gzip compressed"*)
gunzip "$1" ;;
"$1: bzip2 compressed"*)
bunzip2 "$1" ;;
*) error "File $1 can not be uncompressed with smartzip";;
esac

case的脚本语法形式为:

打印一个rpm包的统计信息【含有输入参数】

vim一个showrpm的脚本文件,同时chmod +x showrpm命令

#!/bin/sh
# list a content summary of a number of RPM packages
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpm  <-- 实现查看某个文件下的rpm包的统计信息
# 正常测试:需要使用./showrpm rpmfile1 rpmfile2 来完成

for rpmpackage in $*; do
# -r判断这个变量属性是否可以read
if [ -r "$rpmpackage" ];then
echo "=============== $rpmpackage =============="
rpm -qi -p $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
fi
done

注意这里的$*表示输入行命令参数的所有值,如果是 1 表示输入命令行参数中的第一个参数, 1表示输入命令行参数中的第一个参数, 1表示输入命令行参数中的第一个参数,$#表示输入参数的个数。

for-loop的脚本语法形式为

重命名多个文件名【含有输入参数】

vim一个rename_file脚本,用于重名多个文件,将文件名的old字段替换为new字段

#!/bin/sh
# USAGE:./rename_file old new file1 file2 file3 【显然:输入参数至少要3个,前两个替换,后面至少一个文件】
# we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ] ; then
cat <<HELP
ren -- renames a number of files using sed regular expressions
USAGE: ren 'regexp' 'replacement' files...
EXAMPLE: rename all *.HTM files in *.html:
ren 'HTM$' 'html' *.HTM
HELP
exit 0
fi

# old被替换的字段。new待取代的字段
OLD="$1"
NEW="$2"
# The shift command removes one argument from the list of 【移除输入参数的最前面两个字段】
# command line arguments.
shift
shift
# $* contains now all the files:【遍历所有的文件名】
for file in $*; do
if [ -f "$file" ] ; then
# newfile变量存储为替换后的文件名
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
# 文件已存在,error ; 文件不存在,save
if [ -f "$newfile" ]; then
echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile ..."
mv "$file" "$newfile"
fi
fi
done

在这里插入图片描述
在这里插入图片描述

二进制转十进制【含有输入参数】

#!/bin/sh
# vim: set sw=4 ts=4 et:
# USAGE: b2d 110100
help()
{
cat <<HELP
b2h -- convert binary to decimal
USAGE: b2h [-h] binarynum
OPTIONS: -h help text
EXAMPLE: b2h 111010
will return 58
HELP
exit 0
}

# 输入参数错误函数
error()
{
# print an error and exit
echo "$1"
exit 1
}

# 获取字符串的最后一个字符
lastchar()
{
# return the last character of a string in $rval
# 判断字符串长度是否为0
if [ -z "$1" ]; then
# empty string
rval=""
return
fi
# wc puts some space behind the output this is why we need sed:
# 去除wc统计字符数量中的空格
numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
# now cut out the last char
# cut -b 截取第$numofchar个字符
rval=`echo -n "$1" | cut -b $numofchar`
}

# 删除字符串的最后一个字符
chop()
{
# remove the last character in string and return it in $rval
if [ -z "$1" ]; then
# empty string
rval=""
return
fi
# wc puts some space behind the output this is why we need sed:
numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
if [ "$numofchar" = "1" ]; then
# only one char in string
rval=""
return
fi
# 获取倒数第二个字节的位置
numofcharminus1=`expr $numofchar "-" 1`
# now cut all but the last char:
# 截取第1~numofcharminus1位置的字符,即删除最后一个字符
rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`
}


# 程序开始入口:
# 判断二进制字符串是否为空
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
--) shift;break;; # end of options
-*) error "error: no such option $1. -h for help";;
*) break;;
esac
done

# The main program:右移位权重累计和
# 初始化累计和为0,初始化权重值为0
sum=0
weight=1
# one arg must be given:
[ -z "$1" ] && help
binnum="$1"
binnumorig="$1"

while [ -n "$binnum" ]; do
# 获取最后一个字符
lastchar "$binnum
# 累计权重和
if [ "$rval" = "1" ]; then
sum=`expr "$weight" "+" "$sum"`
fi

# remove the last position in $binnum【移除最后一个字符】
chop "$binnum"
# 更新二进制字符串
binnum="$rval"
# 更新权重
weight=`expr "$weight" "*" 2`
done
echo "binary $binnumorig is decimal $sum"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值