一些shell脚本。

批量更改后缀名的shell脚本:
#################Start Script#############

#!/bin/bash
 E_BADARGS=65
case $# in
0|1)              # "|"  在这里的意思是或操作.
echo "Usage: `basename $0` old_file_suffix new_file_suffix"
exit $E_BADARGS   #  如果只有 0 个或1 个参数,那么就退出.
;;
esac
for filename in *.$1         #以第一个参数为扩展名的全部文件的列表
do
mv $filename ${filename%$1}$2 #从筛选出的文件中先去掉以第一参数结尾的扩展名,从筛选出的文件中先去掉以第一参数结尾的扩展名
done
exit

#################End Script#############


用select 来创建菜单 :
##############Start Script########
 #!/bin/bash
 
 PS3='Choose your favorite vegetable: ' #  设置提示符字串.
 
 echo
 
 select vegetable in "beans" "carrots" "potatoes" "onions"
 do
      echo
      echo "Your favorite veggie is $vegetable."
      echo "Yuck!"
      echo
   break  # 如果这里没有'break' 会发生什么?
 done
 
 exit 0
##############End Script########





关掉终端对于密码的 echo:
#################start Script#############
#!/bin/bash
echo
echo -n "Enter password "
read passwd
echo "password is $passwd"
echo -n "If someone had been looking over your shoulder, "
echo "your password would have been compromised."
echo && echo
stty -echo
echo -n "Enter password again "
read passwd
echo
echo "password is $passwd"
echo
stty echo
exit

##############End Script######## ######



把一个文件的内容全部转换为大写:
######start Script########

#!/bin/bash
if [ -z "$1" ]
then
echo "Usage: `basename $0` filename"
exit 2
fi
tr a-z A-Z <"$1"
exit 0


#########End Script######## ##


记录按键
#############################StartScript####################################
 1 #!/bin/bash
 2 # dd-keypress.sh:  记录按键,  不需要按回车.
 3 
 4 
 5 keypresses=4                      #  记录按键的个数.
 6 
 7 
 8 old_tty_setting=$(stty -g)        #  保存老的终端设置.
 9 
10 echo "Press $keypresses keys."
11 stty -icanon -echo                # 禁用标准模式.
12                                   #  禁用本地 echo.
13 keys=$(dd bs=1 count=$keypresses 2> /dev/null)
14 #  如果不指定输入文件的话, 'dd'  使用标准输入.
15 
16 stty "$old_tty_setting"           #  恢复老的终端设置.
17 
18 echo "You pressed the \"$keys\" keys."
19 
20 #  感谢 Stephane Chazelas,  演示了这种方法.
21 exit 0
################################EndScript#########################################




生成另一个脚本的shell

####################start Script##################

#!/bin/bash
OUTFILE=generated.sh
(
cat << 'EOF'

#!/bin/bash
OUTFILE=generated.sh
echo "This is a generated shell script."


echo "Generated file will be named: $OUTFILE"
EOF
) > $OUTFILE


if [ -f "$OUTFILE" ]
then
        chmod 755 $OUTFILE
else
        echo "Problem in creating file: \"$OUTFILE\""
fi
exit 0



case练习:

####################EndScript##################

#!/bin/bash
echo "press your chioceone,two,three"
read number
case $number in
one)
echo "your choice is one";;
two)
echo "your choice is two";;
three)
echo "your choice is three";;
*)
echo "Usage {one|two|three}"
esac







显示进度条:
####################Start Script##################
#!/bin/bash
i=0
while [ $i -lt 20 ]
do
       ((i++))
       echo -ne "=>\033[s"
       echo -ne "\033[40;50H"$((i*5*100/100))%"\033[u\033[1D"
   usleep 50000
done
echo

####################EndScript##################

####################Start Script##################
COUNTER=0
_R=0
_C=`tput cols`
_PROCEC=`tput cols`
tput cup $_C $_R
printf "["
while [ $COUNTER -lt 100 ]
do
    COUNTER=`expr $COUNTER + 1`
    sleep 0.1
    printf "=>"
    _R=`expr $_R + 1`
    _C=`expr $_C + 1`
    tput cup $_PROCEC 101
    printf "]%d%%" $COUNTER
    tput cup $_C $_R
done
printf "\n"
####################EndScript##################

####################startScript##################
i=0
while [ $i -lt 20 ]
do
       ((i++))
       echo -ne "=>\033[s"
       echo -ne "\033[40;50H"$((i*5*100/100))%"\033[u\033[1D"
   usleep 50000
done
echo
####################EndScript##################


###########startScript#########
  #!/bin/sh
    b=''
    for ((i=0;$i<=100;i+=2))
    do
            printf "[%-50s]%d%%\r" $b $i
            sleep 0.1
            b=#$b
    done
    echo
####################EndScript##################

本文出自 “Mr_Z” 博客,请务必保留此出处http://zhangrong.blog.51cto.com/2196532/962306

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值