简单SHELL实例

IF判断:

#! /bin/bash
num1=$((2*3))
num2=$((1+5))
if test $num1 -eq $num2
then
    echo 'The two numbers are equal!'
else 
    echo 'The two numbers are not equal!'fi



red交互:

#!/bin/bash
read -p 'please input your password: ' password

if test $password == 'bob' 
then
    read -p 'haha~, 1+2=?' result
    declare -i myres=1+2
    if test $myres -eq $result
    then
        echo 'wellcome to my home~'  
    fi
else  
    echo 'wow no ~'  
fi


字符串

#! /bin/bash
name='rockie bob'
echo "Hello ,I know your name \"$name\""

str1='hello'
str2='word'
#echo $str1 $str2 I\'m bob;

string='abcd'
echo ${#string} #输出 4

数组例子:

array_name=(value0 value1 value2 value3)

# 取得数组元素的个数
echo length=${#array_name[@]}
# 或者
length=${#array_name[*]}
# 取得数组单个元素的长度
lengthn=${#array_name[n]}

for循环

#! /bin/bash

arr=('a' 'b' 'c')

for ((i=0;i<${#arr[@]};i++))
do
   eval echo \${arr[i]}
done

while循环

#!/bin/bash
int=0
while (($int<5))
do
   echo $int
   let "int++"
done

echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film:' ""

while read keybord
do
   echo "Yeah! great film the $keybord"
done

awk用法实例:

demoData
--------------------------------
Name    1st     2nd     3th
VBird   23000   24000   25000
DMTsai  21000   20000   23000
Bird2   43000   42000   41000

/bin/cat ./demoData |\
/usr/bin/awk 'NR==1 {printf "%10s %10s %10s %10s %10s \n" , $1, $2, $3, $4, "Total"}
NR>=2 {total=$2+$3+$4
printf "%10s %10d %10d %10d %10.2f \n", $1, $2, $3, $4, total}'

break用法

#!/bin/bash  
for i in "a b c d" 
do   
   echo "$i "
   for j in `seq 10`  
   do  
      if [ $j -eq 5 ];then 
         break  
      fi  
      echo "$j "
   done
done

continue实例

#!/bin/bash
while :
do
   echo -n "Input a number between 1 to 5: "
   read aNum
   case $aNum in
        1|2|3|4|5) 
        echo "Your number is $aNum!"
        ;;
        *) 
        echo "You do not select a number between 1 to 5!"
        continue
        echo "Game is over!"
        ;;
    esac
done

显示系统内的用户:

#! /bin/bash

/bin/cat /etc/passwd |awk 'BEGIN {FS=":"}
{if($3 >=500) printf "hi, youname:%10s,  userId: %10s   groupId:%10s \r\n", $1, $3, $4}'


9x9乘法表

#!/bin/bash

for a in `seq 1 9`
do
    echo ""
    for b in `seq 1 9`
    do
        if [ $a -ge $b ]
        then
            echo -n "$a x $b = $(expr $a \* $b)" 
        fi
    done
done
echo ""


统计一个目录下文件:

#! /bin/sh

#切换工作目录至位置参数给出的目录
#依次向这些目录中的每个文件或子目录问好
#统计这些目录下的各种文件及子目录个数,并显示

if [ $# -lt 1 ]
then
    echo "parameter's number error!";
    exit 1;
fi;

while [ $# -gt 0 ]
do
    if [ -f $1 ] 
    then
        echo "$1 is not a dir!";
    else   
        cd $1

        bfiles=0;  #块设备文件
        cfiles=0;  #字符设备文件
        pfiles=0;  #管道文件
        lfiles=0;  #链接文件/目录
        sfiles=0;  #sock文件
        files=0;   #普通文件
        dirs=0;    #目录文件
        total=0;   #总文件数
        for var in `ls $1`
        do
            total=`expr $total + 1`; 
            echo "hello $var";
            if [ -b $var ]
            then
                bfiles=`expr $bfiles + 1`;  
            elif [ -c $var ] 
            then
                cfiles=`expr $cfiles + 1`;
            elif [ -S $var ]
            then
                sfiles=`expr $sfiles + 1`;
            elif [ -p $var ]
            then
                pfiles=`expr $pfiles + 1`;
            elif [ -h $var ] 
            then
                lfiles=`expr $lfiles + 1`;
            elif [ -f $var ]
            then
                files=`expr $files + 1`;
            elif [ -d $var ]
            then
                dirs=`expr $dirs + 1`;
            fi;
        done
     
        echo "the number of bfiles is $bfiles";
        echo "the number of cfiles is $cfiles";
        echo "the number of lfiles is $lfiles";
        echo "the number of sfiles is $sfiles";
        echo "the number of pfiles is $pfiles";
        echo "the number of files is $files";
        echo "the number of dirs is $dirs";
        echo "total file number  is $total";
    fi;
    echo "************************************"
    shift;
done

svn 脚本:

#! /bin/bash

/usr/bin/svn up
/usr/bin/svn st

if [$? != '']
echo 'input svn log:'
read info
/usr/bin/svn ci -m $info

ssh登录:

#! /bin/expect

set timeout 30
/usr/bin/ssh fubh@10.99.102.60
expect "*password:"
send  "123456\r"

从ftp服务器上的/home/data 到 本地的/home/databackup:

#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
mget *
close
bye
!

ftp自动登录上传文件。

####本地的/home/databackup to ftp服务器上的/home/data####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
hash
cd /home/data
lcd /home/databackup
prompt
mput *
close
bye
!

系统统计:

#!/bin/bash
#author bob <983194768@qq.com>

/bin/date +%F >> /temp/sysinfo

echo "disk info:" >> /tmp/sysinfo
/bin/df -h >> /tmp/sysinfo

echo >> /tmp/sysinfo
echo "online users:" >> /tmp/sysinfo
/usr/bin/who | /bin/grep -v root >>/tmp/sysinfo

echo  >> /tmp/sysinfo
echo "memory info:" >> /tmp/sysinfo
/usr/bin/free -m >> /tmp/sysinfo

echo  >> /tmp/sysinfo

#write root 
/usr/bin/write root </tmp/sysinfo && /bin/rm /tmp/sysinfo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值