note of SHELL (四)

本文集展示了一系列实用的Shell脚本案例,包括文件操作、随机数生成、递归计算等功能,旨在帮助读者理解和掌握Shell脚本的基础用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#--------------------------------------------------------------
#
#
#!/bin/sh
line_length=20
line_count=$1
char="*"
 
function usage
{
  name=$(basename $0)
  echo -e "usage:$name line_count_number/n"
  exit 1
}

argc=$#
[[ ! $argc -eq 1 ]] && usage

length=0
count=0
>"test.txt"
file="test.txt"

  while [[ $count -le $line_count ]]  ;do
while [[ $length -le $line_length ]]  ;do
    echo -e "${char}/c">>$file
    ((length = length+1))
    done
    echo >>$file
    ((count = count+1))
    done

 

#----------------------------------------------
#name:random2.sh
#note:generate a limited random number
#!/bin/sh
function check
{
  argc=$#
  if [[ $argc -lt 1 ]] ;then
    echo -e "ERROR! usage: $0 limit_number/n"
    exit
    fi
}

function random
{
  echo -e "the random number is :$RANDOM/n"
}

function limit_random
{
  result=$(($RANDOM%$NUMBER+1))
  echo -e "the limit number is :$result/n"
}

NUMBER=$1
#check

if [[ -z $1 ]] ;then
  random
  else
  limit_random
  fi

 

#----------------------------------
#name:read2.sh
#note:read 2 variables from a file
#!/bin/sh
while   read a  b   ;do
  if [ "$a" = "ok" ] || [ "$b" = "ok" ] ;then
    echo -e " you are passed/n"
  fi
done<test

 

 

#---------------------------------------------------------
#
#
#!/bin/sh
i=0
while (read data) ;do
${a[$i]}=$data
((i=i+1))
done<"test.txt"

echo -e "${ a[0]}/n"

 

  

#--------------------------------------------------
#name:recursive.sh
#note:calculate  a recursive  number
#!/bin/sh
if [ -z $1 ] ;then
echo -e "usage:$0 recursive_number/n"
exit 1
fi
  
recursive ()
{
     local  number=$1
  if [ $number -eq 0  ] ;then
   result=1
  else
  let "decnum=$number-1"
  recursive $decnum
  let "result=$number*$?"
  fi
  return $result
}
recursive $1
echo -e  "the recusive of $1 is $result/n"

 

#---------------------------------------------------------
#name:search.sh
#note:return the result of searching
#!/bin/sh
PATTERN=$1
FILENAME=$2
grep "$PATTERN" $FILENAME>/dev/null
if [[ $? -eq 0 ]] ;then
  echo -e "$PATTERN is found in this file/n"
  else
  echo -e "$PATTERN is not here/n"
  fi

 

##########################################################
#!/bin/sh
#scriptname:select.sh
#purpose:select.....do......done
function choose
{
echo -e "choose one person/n"
select choice in peter YangTT ;do
case $choice in
peter)
echo -e "He will success/n"
break ;;
YangTT)
echo -e "I love this girl so much/n"
break ;;
*)
echo -e "please choose again/n";;
esac
done
}
choose

 

#####################################
#
#
#!/bin/sh
set `date`
while [[ $# -gt 0 ]]
do
  echo  "$1"
  shift
done

 

######################################
#################testfile#############
#apple
#pear
#peach
#banana
#
#scriptname:speller.sh
#purpose:read a line from a file,and update the content
#!/bin/sh
exec <testfile
while read fruit ;do
  echo -e "Do you like $fruit(y/n)/n"
  read answer</dev/tty
  case $answer in
  n|N)
  echo -e "OK,Type what you like/n"
  read new</dev/tty
  sed -e s/$fruit/$new/ testfile>tmp
  mv tmp testfile
  echo -e "updata success/n";;
  *)
  echo -e "skiping /n";;
  esac
done
  

 

###############################################
#scriptname:square.sh
#purpose:calculate number's square
#!/bin/sh
function square
{
  ((result=$1*$1))
  echo -e "the result of square $1 is $result/n"
}

echo -e "input a number/n"
read data
square $data

 

 

#------------------------------------------------
#
#
#!/bin/sh
string=$1
argc=$#
if [[ ! $argc -eq 1 ]] ;then
  echo -e "usage:$0 test_string/n"
fi

case $gtring in
+([0-9]))
echo -e "it is number/n"
;;
+([a-zA-Z]))
echo -e "it is character/n"
;;
*)
echo -e  "unknown/n"
;;
esac
  

 

 

##############################################################
#scriptname:trap2.sh
#purpose:using trap function
#!/bin/sh
trap 'echo -e "ctrl+c will not terminate this programme/n"' 2
echo -e "type /"stop/" to exit/n"
while true
do
  echo -n "switch->"
  read data
  if [ $data = "stop" ] ;then
    break
  fi
done

 

 

 

#------------------------------------------
#name:trap.sh
#note:ctrl+c  signal 2,when hit ctrl+c ,execute function hi and exit
#!/bin/sh
function hi
{
  echo -e "hi,all!!/n"
}

trap 'hi ;exit 1'  2


read hi
echo $hi

 

 

 

#-------------------------------------------
#name:update.sh
#note:creating a specified file
#!/bin/sh
clear
echo -e "/nDo you want to create a file?(Y|N)/n"
read answer
case $answer in
Y|y)  ;;
*)  echo -e "/n all right ,operation is cancle!/n";;
esac

cat /dev/null >test

for input in wuhan danian shanghai beijing ;do
    echo $input >>test
done
  

 

 

 

#--------------------------------------------------
#name:print_pic
#note:print a figure
#!/bin/sh
a=0
while true ;do
  a=$(expr $a + 1) ;
  case $a in
  1) echo -e "+/c" ;;
  2) echo -e "*/c" ;;
  3) echo -e "*/c" ;;
  4) echo -e "*/c" ;;
  5) echo -e "*/c" ;;
  6) echo -e "*/c" ;;
  7) echo -e "*/c" ;;
  8) echo -e "*/c" ;;
  9) echo -e "*/c" ;;
  10) echo -e "+/n" ;;
  *)  echo "finish!"
   exit 0 ;;
  esac
  sleep 1
  done

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值