shell编程

1,简单数组打印,使用for和while

#!/bin/sh

#city=(`ls /etc/init.d`); //也利用ls获得数组
city=("liuzhou" "wuhan" "beijng" "heifei" "liaoning" "shanghai" "xian");
number=${#city[@]} //计算出数组个数
          
echo "use while: "
i=0       
while [ $i -lt $number ]
do        
    echo -n "${city[$i]}  "
    let i=i+1;
done      
echo ""   
echo "There are $number"
          
          
echo "use for: "
for var in ${city[@]}; do
    echo -n "$var  "
done      
echo ""   
echo "There are $number"

2,读取网卡信息,并存储在文件(使用ifconfig和awk)

#!/bin/sh
#follow an argument such as "./get_net.sh [argument]"
#read the information:"ip" "netmask" "mac"
#and store the network information in file

PWD=`pwd`
network=("eth0" "lo")

if [ $# != 1 ]; then
    echo "follow only one argument"
    echo "example: ./get_net.sh [argument]"
    exit
fi

if [ "$1"x = "${network[0]}"x ]; then  //判断两个字符串是否一致
    IPADDR=`ifconfig ${network[0]} | grep "inet addr:" | awk -F"Bcast:" '{print$1}' 
    | awk -F":" '{print$2}'`
    MAC=`ifconfig ${network[0]} | grep "Ethernet" | awk -F"HWaddr " '{print$2}'`
    NETMASK=`ifconfig ${network[0]} | grep "inet addr:" | awk -F"Mask:" '{print$2}'`

    if [ -f $PWD/${network[0]} ]; then  //如果文件以及存在,先删除,再存储
        rm -rf $PWD/${network[0]}
    fi
    echo ip=$IPADDR >> $PWD/${network[0]}
    echo mac=$MAC >> $PWD/${network[0]}
    echo netmask=$NETMASK >> $PWD/${network[0]}

elif [ "$1"x = "${network[1]}"x ]; then
    IPADDR=`ifconfig lo | grep "inet addr" | awk -F"Mask:" '{print$1}' | awk -F":" '{print$2}'`
    NETMASK=`ifconfig lo | grep "inet addr" | awk -F"Mask:" '{print$2}'`

    if [ -f $PWD/${network[1]} ]; then
        rm -rf $PWD/${network[1]}
    fi

    echo ip=$IPADDR >> $PWD/${network[1]}
    echo netmask=$NETMASK >> $PWD/${network[1]}

else
    echo "illegal argument, use "ifconfig -a" to get information"
    echo "example: ./get_net.sh [argument]"
    exit
fi

3,一键解压脚本

#!/bin/bash
#Uncompress all kinds of files
#such as:.tar.bz2 .tar.gz .bz2  .gz .zip .tgz .tbz .rar .Z .7z

if [ $# != 1 ]; then
    echo "follow only a file"
    echo "./autoex.sh [argument]"
fi

if [ -f $1 ] ; then
   case $1 in
    *.tar.bz2)  tar -xjf $1;;
    *.tar.gz )  tar -xzf $1;;
    *.tar)      tar -xf $1;;
    *.bz2)      bunzip2 -d $1;;
    *.rar)      rar x $1     ;;
    *.gz)       gunzip -d $1;;
    *.zip)      unzip $1;;
    *.tgz)      tar -xzf $1;;
    *.tbz2)     tar -xjf $1;;
    *.7z)       7z x $1;;
    *.Z)        uncompress $1  ;;
    *)          echo "'$1' cannot be extracted via extract()" ;;
   esac
else
    echo "'$1' is not a valid file"
fi

补充shell下内部参数:

      $0 ---- 当前程序的名称,而$1,$2 后续参数
  $# ---- 表示参数的个数
  $? ---- 上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非0值。
  $* ---- 传递给程序的所有参数组成的字符串。
  $@ ---- 取到所有的参数值
  $$ ---- 本程序的(进程ID号)PID
  $! ---- 上一个命令的PID
  
学习链接:

shell编程基础:http://wiki.ubuntu.org.cn/Shell编程基础#if_.E8.AF.AD_.E5.8F.A5

linux下awk用法:http://blog.csdn.net/ajaxuser/article/details/5953870

shell数组:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html

解压大全:http://www.cnblogs.com/eoiioe/archive/2008/09/20/1294681.html

原文地址:https://www.cnblogs.com/xiaoxing/p/3968490.html