shell基础

编完后加 执行权,将a.sh放入 /usr/local/bash 用户可以执行

数字>1.txt清空 不加空格

Echo “”>1.txt写入必有””

> 写入文件并覆盖旧文件  >> 加到文件的尾部,保留旧文件内容。

if ....  hen  ....;elif .... then ;else  ....fi   (fi结束,不能丢)

比较字符串、判断文件是否存在及是否可读等通常用" [ ] "来表示条件测试。

里面的空格必须有,可以通过man test查看

mailfolder=/var/spool/mail/james

[ -r "$mailfolder" ]' '{ echo "Can not read $mailfolder" ; exit 1; }

echo "$mailfolder has mail from:"

grep "^From " $mailfolder

该脚本首先判断mailfolder是否可读。可读则打印"From" 一行。不可读打印错误信息后脚本退出。我们使用花括号以匿名函数的形式将两个命令放到一起作为一个命令使用。

select 表达式是一种bash的扩展应用,尤其擅长于交互式使用。从一组不同的值中进行选择。

echo "What is your favourite OS?"

select var in "Linux" "Gnu Hurd" "Free BSD" "Other"   ;do  break done

echo "You have selected $var"

while ...; do .... done   while-loop 将运行直到表达式测试为真。will run while the expression that we test for is true. 关键字"break" 用来跳出循环;;关键字"continue"

 for-loop表达式查看一个字符串列表 (字符串用空格分隔) 然后将其赋给一个变量:

for var in ....; do  .... done

打印ABC到屏幕上for var in A B C  do  echo "var is $var" done

脚本showrpm,其功能是打印一些RPM包的统计信息:

# list a content summary of a number of RPM packages

# USAGE: showrpm rpmfile1 rpmfile2 ...

# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpm

for rpmpackage in $*; do  if [ -r "$rpmpackage" ];then

  echo " $rpmpackage "   rpm -qi -p $rpmpackage  else

  echo "ERROR: cannot read file $rpmpackage"  fi    done

特殊的变量$*,该变量包含了所有输入的命令行参数值。

echo *.jpg 将打印当前目录下所有jpg文件

echo "*.jpg"     echo '*.jpg'   这将打印"*.jpg" 两次。

单引防止任何变量扩展;双引号防止通配符扩展但允许变量扩展;转义字符——反斜杆

当要将几行文字传递给一个命令时here document 一个 "Here document.quot; << 开头,后面接上一个字符串,这个字符串还必须出现在here document.末尾。

# 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="$1"   NEW="$2"

shift 删除第一个参数

shift 删除第二个参数

for file in $*; do

  if [ -f "$file" ]  then

   newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`

   if [ -f "$newfile" ]; then  echo "ERROR: $newfile exists already"

   else  echo "renaming $file to $newfile ..."

    mv "$file" "$newfile"    fi   fi   done

第一个if表达式判断输入命令行参数是否小于3 (特殊变量$# 表示包含参数的个数) 。如果输入参数小于3个,则将帮助文字传递给cat命令,然后由cat命令将其打印在屏幕上后退出。

如果输入参数等于或大于3个,我们就将第一个参数赋值给变量OLD,第二个参数赋值给变量NEW。下一步,我们使用shift命令将第一个和第二个参数从参数列表中删除

函数 functionname()

xtitlebar的脚本使用这个脚本您可以改变终端窗口的名称。help的函数被使用了两次。

# vim: set sw=4 ts=4 et: 如何作用

help()

{   cat <<HELP

xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole

USAGE: xtitlebar [-h] "string_for_titelbar"

OPTIONS: -h help text

EXAMPLE: xtitlebar "cvs"

HELP

  exit 0

}

# in case of error or if -h is given we call the function help:

[ -z "$1" ] && help

[ "$1" = "-h" ] && help

# send the escape sequence to change the xterm titelbar:

echo -e "33]0;$107"

  命令行参数 $* $1, $2 ... $9 等特殊变量,这些特殊变量包含了用户从命令行输入

help()

{ cat <<HELP

This is a generic command line parser demo.

USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2

HELP  exit 0 }

while [ -n "$1" ]; do

case $1 in   -h) help;shift 1;; # function help is called

  -f) opt_f=1;shift 1;; # variable opt_f is set

  -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2

  --) shift;break;; # end of options

  -*) echo "error: no such option $1. -h for help";exit 1;;   *) break;; esac done

echo "opt_f is $opt_f" echo "opt_l is $opt_l" echo "first arg is $1" echo "2nd arg is $2"

运行该脚本cmdparser -l hello -f -- -somefile1 somefile2

opt_f is 1 opt_l is hello first arg is -somefile1 2nd arg is somefile2

任何优秀的脚本都应该具有帮助和输入参数。并且写一个伪脚本(framework.sh),该脚本包含了大多数脚本都需要的框架结构,是一个非常不错的主意。这时候,在写一个新的脚本时

cp framework.sh myscript

二进制到十进制的转换 脚本 b2d 将二进制数 (比如 1101) 转换为相应的十进制数。这也是一个用expr命令进行数学运算的例子:

# vim: set sw=4 ts=4 et:

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

  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' `

  # now cut out the last char

  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:

  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

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"

#

  该脚本使用的算法是利用十进制和二进制数权值 (1,2,4,8,16,..),比如二进制"10"可以这样转换成十进制:   0 * 1 + 1 * 2 = 2

  为了得到单个的二进制数我们是用了lastchar 函数。该函数使用wc -c计算字符个数,然后使用cut命令取出末尾一个字符。Chop函数的功能则是移除最后一个字符。

  文件循环程序 :或许您是想将所有发出的邮件保存到一个文件中的人们中的一员,但是在过了几个月以后,这个文件可能会变得很大以至于使对该文件的访问速度变慢。下面的脚本rotatefile 可以解决这个问题。这个脚本可以重命名邮件保存文件(假设为outmail)为outmail.1,而对于outmail.1就变成了outmail.2 等等等等...

# vim: set sw=4 ts=4 et:

ver="0.1"

help()

{  cat <<HELP

rotatefile -- rotate the file name

USAGE: rotatefile [-h] filename

OPTIONS: -h help text

EXAMPLE: rotatefile out

This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1

and create an empty out-file

The max number is 10

version $ver

HELP

  exit 0

}

error()

{   echo "$1"

  exit 1

}

while [ -n "$1" ]; do

case $1 in

  -h) help;shift 1;;

  --) break;;

  -*) echo "error: no such option $1. -h for help";exit 1;;

  *) break;;

esac

done

# input check:

if [ -z "$1" ]  then

error "ERROR: you must specify a file, use -h for help"

fi

filen="$1"

# rename any .1 , .2 etc file&:

for n in 9 8 7 6 5 4 3 2 1; do

  if [ -f "$filen.$n" ]; then

    p=`expr $n + 1`

    echo "mv $filen.$n $filen.$p"

    mv $filen.$n $filen.$p

  fi

done

# rename the original file&:

if [ -f "$filen" ]; then

  echo "mv $filen $filen.1"

  mv $filen $filen.1

fi

echo touch $filen

touch $filen

  这个脚本是如何工作的呢在检测用户提供了一个文件名以后我们进行一个91的循环。文件9被命名为10,文件8重命名为9等等。循环完成之后,我们将原始文件命名为文件1同时建立一个与原始文件同名的空文件。

调试:sh -x strangescript  这将执行该脚本并显示所有变量的值。

   sh -n your_script   这将返回所有语法错误。

   echo  ls  wc  cp  mv  rm  grep

  cut -b colnum file&: 指定欲显示的文件内容范围,并将它们输出到标准输出设备比如:输出每行第5个到第9个字符cut -b5-9 file.txt千万不要和cat命令混淆,这是两个完全不同的命

  cat   file  read var: 提示用户输入,并将输入赋值给变量

  sort file.txt: file.txt文件中的行进行排序

  uniq: 删除文本文件中出现的行列比如: sort file.txt | uniq

  expr: 进行数学运算Example: add 2 and 3expr 2 "+" 3

  find  tee: 将数据输出到标准输出设备(屏幕) 和文件比如somecommand | tee outfile

  basename file&: 返回不包含路径的文件名比如 basename /bin/tux将返回 tux

  dirname file&: 返回文件所在路径比如dirname /bin/tux将返回 /bin

  head file&: 打印文本文件开头几行   tail file : 打印文本文件末尾几行

  sed: Sed是一个基本的查找替换程序。可以从标准输入(比如命令管道)读入文本,并将结果输出到标准输出(屏幕)。该命令采用正则表达式(见参考)进行搜索。不要和shell中的通配符相混淆。比如:将linuxfocus 替换为 LinuxFocus cat text.file | sed 's/linuxfocus/LinuxFocus/' > newtext.file

  awk: awk 用来从文本文件中提取字段。缺省地,字段分割符是空格,可以使用-F指定其他分割符。cat file.txt | awk -F, '{print $1 "," $3 }'这里我们使用,作为字段分割符,同时打印第一个和第三个字段。如果该文件内容如下: Adam Bor, 34, IndiaKerry Miller, 22, USA命令输出结果为:Adam Bor, IndiaKerry Miller, USA

   find . -mtime -1 -type f -print

  用来查找过去24小时(-mtime -2则表示过去48小时)内修改过的文件。如果您想将所有查找到的文件打一个包,则可以使用以下脚本:

    tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print`

case表达式可以用来匹配一个给定的字符串,而不是数字。

制作 smartzip的脚本,该脚本可以自动解压bzip2, gzip zip 类型的压缩文件:

ftype=`file "$1"`                    $1 就是字符串 articles.zip

case "$ftype" in

"$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

较为通用的shell有标准的Bourne shell (sh)C shell (csh)

shell是用户和Linux内核之间的接口程序,它是命令语言、命令解释程序及程序设计语言的统称。 Linux传递命令时,内核会做出相应的反应

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值