背景知识Shell 是用户与内核进行交互操作的一种接口,是 Linux 最重要的软件之一。目前最流行的 Shell 称为 bash Shell,bash Shell 脚本编程以其简洁、高效而著称,多年来成为 Linux 程序员和系统管理员解决实际问题的利器。Shell 是操作系统的最外层。Shell 合并编程语言以控制进程和文件,以及启动和控制其它程序。Shell 通过提示您输入,向操作系统解释该输入,然后处理来自操作系统的任何结果输出来管理您与操作系统之间的交互。Shell 提供了与操作系统通信的方式。此通信以交互的方式(来自键盘的输入立即操作)或作为一个 Shell 脚本执行。Shell 脚本是Shell 和操作系统命令的序列,它存储在文件中。当登录到系统中时,系统定位要执行的 Shell 的名称。在它执行之后,Shell 显示一个命令提示符。普通用户的此提示符通常是一个 $(美元符)。当提示符下输入命令并按下 Enter 键时,Shell 对命令进行求值,并尝试执行它。取决于命令说明,Shell 将命令输出写到屏幕或重定向到输出。然后它返回命令提示符,并等待您输入另一个命令。命令行是输入所在的行,它包含 Shell 提示符。每行的基本格式如下: $ 命令 参数(一个或多个)Shell 视命令行的第一个字(直到第一个空格)为命令,所有后继字为自变量。在此推荐大家使用zsh,具体下载安装版本在 http://www.linuxidc.com/Linux/2013-09/90377.htm使用 Zsh 的九个理由 http://www.linuxidc.com/Linux/2013-05/84191.htmZsh使用心得三则 http://www.linuxidc.com/Linux/2012-08/67735.htmLinux下安装终极Shell Zsh http://www.linuxidc.com/Linux/2012-08/67734.htm 1:判断文件是否为块设备或字符设备文件,如果是将其拷贝到/dev 目录下#!/bin/bashread -p "please input a filename:" filename if test -b "$filename" -o -c "$filename";then echo "$filename is a device file" && cp $filename /dev else echo "$filename is not a device file" fi2:模拟 Linux 登陆 Shell#!/bin/bashx=wj #这是用户名 y=1111 #这是密码 read -p "please input yourname:" yourname read -p "please input yourkey:" yourkey if test "$x" = "$yourname" -a "$y" = "$yourkey" ;then bash #注意test后面比较大小的空格不能省 else echo"please try again" fi3:从键盘读取两个数,并比较两个数大小,并打印结果#!/bin/bashread -p "please input num1:" num1 read -p "please input num2:" num2 if test $num1 = $num2 ;then echo "num1=num2" elif test $num1 -gt $num2 ;then echo "num1>num2" else test $num1 -lt $num2 echo "num1<num2" fi 4:查找/root/目录下是否存在指定文件#!/bin/bashcd /root read -p "please input filename:" filename if test -e $filename;then echo "$filename is exist" else echo "$filename is not exist" fi5:删除当前目录下大小为 0 的文件#/bin/bashfor filename in `ls` do if test -d $filename;then b=1 else a=$(ls -l $filename | awk "{ print $5 }") if test $a -eq 0;then rm $filename fi fi done6:查找最大文件#!/bin/basha=0 for name in * do if test -d $name;then c=1 else b=$(ls -l $name | awk "{print $5}") if test $b -ge $a;then a=$b namemax=$name fi fi done echo "the max file is $namemax"7:打印当前用户(不用 whoami)#!/bin/bash echo "Current User is :"echo $(who | sed -n "1p" | awk "{print $1}")8:查找当前目录中所有大于 50k 的文件,把这些文件名写到一个文本文件中,并统计其个数#!/bin/bashfind ./ -size +50k > toingji.txt cat tongji.txt |wc -l 9:一个函数,利用 shift 计算所有参数乘积,假设参数均为整数#!/bin/bashresult=1 # $#表示参数个数 while test $# -gt 0 do result=`expr $1 * $result` shift done echo "the result is $result"10:测试一个文件是否已经被排序过#!/bin/bashread -p "please input your filename:" file sort -C $file # $?表示执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误) a=$? if test $a -eq 0 ;then echo "$file is youxu" else echo "$file is wuxu" fi11:判断用户是否在运行#!/bin/basha=$(ps | grep "$$" | awk "{print $2}") b=$( who| grep "$a" | awk "{print $1}") read -p "please input a username:" username if [ "$b" = "$username" ];then echo "the ueser is running" else echo "the ueser is not running" fi12:在当前目录下交互式创建文件夹(文件夹已存在则打印提示输入新名字)#!/bin/bashread -p "please input dirname:" file if test -d $file;then echo dir is exist please try again else read -p "create dir now?[y/n] " sel if test "$sel" = "y";then mkdir $file elif test "$sel" = "n";then bash else echo "Your input is wrong" fi fi13:获取本机 IP 地址#!/bin/basha=$(ifconfig | grep "inet 地址:" | awk "{print $2}" | sed "s/地址://g" |sed -n "1p" ) echo "ip is $a"14:打印无密码用户#!/bin/bashecho "no passwd user are:" a=$(sudo cat /etc/shadow | grep "*" | awk "BEGIN { FS=":" }{print $1}") #在处理输入文件以前,域分隔符(FS)被设为冒号 echo $a15:写一个脚本,利用循环计算 n的阶乘#!/bin/bashread -p "please input n:" n sum=1 for a in `seq 1 $n` do sum=`expr $sum * $a` done echo "n! = $sum"16:利用 case 语句测试输入字符是否是:小写/大写/数字/其他#!/bin/bashread -p "please input something:" Key case $Key in [[:lower:]]) echo "It is lowercase";; [[:upper:]]) echo "It is uppercase";; [0-9]) echo "It is number";; esac17:测试文件是否包含特定的文本内容#!/bin/bashread -p "enter a string: " string read -p "enter a filename: " filename grep -q "$string" $filename #在file中查找str(不输出找到的内容),成功返回0 if test $? -eq 0;then echo "The text exists in the file" else echo "Text does not exist in the file" fi18:检测给定的单词是否为词典中的单词#!/bin/bashread -p "enter a string:" string #/usr/share/dict/american-english是Ubuntu 系统中英式英语常用单词列表的字典文件 grep -q "$string" /usr/share/dict/american-english if test $? -eq 0;then echo "match directionary" else echo "not match directionary" fi本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-08/122057.htm