现在的位置: 首页 > 综合 > 正文

一些shell脚本。

2018年05月16日 ⁄ 综合 ⁄ 共 3486字 ⁄ 字号 评论关闭
批量更改后缀名的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

抱歉!评论已关闭.