1.autoftp

#!/bin/bash

ftp -n192.168.137.165<<!# 使用非交互模式的ftp,用!作分隔符

user abcpassword# 用户名和密码

get$1# 以命令行参数1作为要获取的文件名

bye# 退出ftp

!# !分隔符,命令结束


2.computesum

# 从命令行输入多个以空格分隔的数字,输出全部数字的和

#!/bin/bash

sum=0

for ccc

do

if [ `expr $ccc \>= 0` = 1 ]

then

echo -e "$ccc+\c"# \c表示输出加数之后不按<Enter>

sum=`expr $sum + $ccc`

fi

done

echo -e "\b=$sum"# \b表示退格


3.diff

# shell编写的diff脚本,比较从命令行输入的两个整数的值是否相等

#!/bin/bash

if test $1 = $2

then

echo$1=$2

else

echo$1!=$2

fi

# 在命令的逻辑判断语句中,用fi表示结束if语句


4.echodate

#!/bin/bash

echo `date +%y%m%d`



5.fact.sh

# 从命令行输入数字,计算该数字的阶乘并输出

# 函数中使用递归,函数的两个参数$1=本次阶乘因子,

# $2=上次阶乘结果

#!/bin/bash

function fact()

{

# 如果阶乘因子等于0,表明阶乘计算完成,输出结果返回

if [ $1 -eq 0 ]

then

echo $2

return

fi

# 将当前的阶乘因子与上一次递归计算所得的阶乘结果相乘,

# 并将结果存放于变量t

t=`expr $2 \* $1` # 调用expr计算两个数的积

#递归调用

fact `expr $1 - 1` $t# 递归调用当前函数

}


# 调用函数fact计算阶乘,第一个参数为从命令行输入的要求结成的变量的值

# 第二个参数是初始的递归结果,设为1

fact $1 1# 调用函数fact计算命令行参数1的阶乘


6.sum

#!/bin/bash

sum=`expr $1 +$2`# 在使用expr进行计算时,运算符两边应该加空格

echo $1+$2=$sum


7.testcmdline

# 从命令行输入多个以空格分隔的参数,使用while循环枚举全部参数

#!/bin/bash

n=1#初始化变量n

#while循环,测试$*是否为空

#$*包含了全部命令行参数,在循环体中调用shift向参

#数向左移除,导致$*最终变为空而循环结束

while [ -n "$*" ]

do

echo$n个参数=$1

n=`expr$n + 1`

shift

done


#注意:在循环体中使用shift将命令行参数左移,这样,没循环一次,命令行参

#数将从左侧被移走一个,是原来处于第二位命令行参数变成第一位。最

#$*#代表的全部命令行参数将为空,循环结束。


8.weekday

# 输入数字,输出该数字对应的是星期几

#!/bin/bash

if [ $# -lt 1 ]

then

echoUsage:$0 数字

exit

fi


case $1 in

1)

echomonday;;

2)

echotuesday;;

3)

echowednesday;;

4)

echothursday;;

5)

echofriday;;

6)

echosaturday;;

7)

echosunday;;

*)

echoinvalidate;

esac


9.greetings

#!/bin/bash

# This is thefirst Bash shell program

# ScriptName:greetings

echo

echo –e "Hello $LOGNAME, \c"

echo"it's nice talking to you."

echo"Your present working directory is:"pwd

# Show the name ofpresent directory

echo

echo –e "The time is `date +%T`!.\nBye"

echo


10.ex4read

# !/bin/bash

# This script isto test the usage of read

# Scriptname:ex4read

echo "=== examples for testing read==="

echo -e "What is your name? \c"

read name

echo "Hello $name"

echo

echo -n "Where do you work? "

read

echo "I guess $REPLY keeps youbusy!"

echo

read -p "Enter your job title:"#自动读给REPLY

echo "I thought you might be an$REPLY."

echo

echo "=== End of the script==="

# read variable读取变量给variable

# read x y可同时读取多个变量

# read自动读给REPLY

# read –p “Pleaseinput: ”自动读给REPLY


11.ex4if

#!/bin/bash

# scriptname:ex4if

echo -n "Please input x,y: "

read x y

echo "x=$x, y=$y"

if (( x > y )); then

echo "x is larger than y"

elif (( x == y)); then

echo "x is equal to y"

else

echo "x is less than y"

fi

12.chkperm

#!/bin/bash

# Using the oldstyle test command: [ ]

# filename:chkperm

file=testing

if [ -d $file ]

then

echo "$file is a directory"

elif [ -f $file ]

then

if [ -r $file -a -w $file -a -x $file ]

then# nested if command

echo "You have read,write,and execute permission on $file."

fi

else

echo "$file is neither a file nor a directory. "

fi

13.chkperm2

#!/bin/bash

# Using the newstyle test command: ` `

# filename:chkperm2

file=./testing

if [[ -d $file ]]

then

echo "$file is a directory"

elif [[ -f $file ]]

then

if [[ -r $file && -w $file && -x $file ]]

then# nested if command

echo "You have read,write,and execute permission on $file."

fi

else

echo "$file is neither a file nor a directory. "

fi


14.name_grep

#!/bin/bash

# filename:name_grep

name=Tom

if grep "$name" /etc/passwd>& /dev/null

then

:

else

echo "$name not found in /etc/passwd"

exit 2

fi

15.tellme

#!/bin/bash

echo -n "How old are you? "

read age

if [ $age -lt 0 -o $age -gt 120 ]

then

echo"Welcome to our planet! "

exit 1

fi

if[ $age -ge 0 -a $age -le 12 ]

then

echo "Children is the flowersof the country"

elif [ $age -gt 12 -a $age -le 19 ]

then

echo "Rebel without acause"

elif [ $age -gt 19 -a$age -le 29 ]

then

echo "You got the world bythe tail!!"

elif [ $age -ge30 -a$age -le 39 ]

then

echo "Thirtysomething..."

else

echo "Sorry I asked"

fi


16.tellme2

#!/bin/bash

echo -n "How old are you? "

read age

if (( age < 0 || age > 120 ))

then

echo "Welcome to our planet!"

exit 1

fi

if ((age >= 0 && age <=12))

then

echo "Children is the flowersof the country"

elif ((age >= 13 && age <=19 ))

then

echo "Rebel without a cause"

elif (( age >= 19 &&age <=29 ))

then

echo "You got the world bythe tail!!"

elif(( age >=30 &&age <= 39 ))

then

echo "Thirtysomething..."

else

echo "Sorry I asked"

fi

17.idcheck.sh

#!/bin/bash

# Scriptname:idcheck.sh

# purpose: checkuser id to see if user is root.

# Only root has auid of 0.

# Format for idoutput: uid=501(tt) gid=501(tt) groups=501(tt)

# root’s uid=0 uid=0(root) gid=0(root) groups=0(root)…

id=`id | awk -F'[=(]''{print $2}'`# get user id

echo "your user id is: $id"

if(( id == 0 ))# [ $id -eq 0 ]

then

echo "you aresuperuser."

else

echo "you are notsuperuser."

fi


18.yes_no.sh

#!/bin/bash

# test case

# scriptname:yes_no.sh

echo -n "Do you wish to proceed[y/n]: "

read ans

case $ans in

y|Y|yes|Yes)

echo "yes is selected"

;;

n|N|no|No)

echo "no is selected"

;;

*)

echo "`basename $0`: Unknown response"

exit 1

;;

esac

19.forloop.sh

#!/bin/bash

# Scriptname:forloop.sh

for name in Tom Dick Harry Joe

do

echo "Hi $name"

done

echo "out of loop"


20.forloop2.sh

#!/bin/bash

# Scriptname:forloop2.sh

for name in `cat namelist`

do

echo "Hi $name"

done

echo "out of loop"


21.mybackup.sh

#!/bin/bash

# Scriptname:mybackup.sh

# Purpose: Createbackup files and store

# them in a backupdirectory.

#

backup_dir=backup

mkdir $backup_dir

for file in *.sh

do

if [ -f $file ]

then

cp $file $backup_dir/${file}.bak

echo "$file is backed up in $backup_dir"

fi

done


22.greet.sh

#!/bin/bash

# Scriptname:greet.sh

# usage: greet.shTom John Anndy


echo "== using \$* =="

for name in $*# same as for name in $@

do

echo Hi $name

done

echo "== using \$@ =="

for name in $@# same as for name in $*

do

echo Hi $name

done

echo '== using "$*" =='

for name in "$*"

do

echo Hi $name

done

echo '== using "$@" =='

for name in "$@"

do

echo Hi $name

done


23.permx.sh

#!/bin/bash

# Scriptname:permx.sh

#

for file# Empty wordlist

do

if [[ -f $file && ! -x $file ]]

then

chmod +x $file

echo " == $file now has execute permission"

fi

done





24.months.sh

#!/bin/bash

# Scriptname:months.sh

for month in Jan Feb Mar Apr May Jun JulAug Sep Oct Nov Dec

do

for week in1 2 3 4

do

echo -n "Processing the month of $month. OK? "

read ans

if [ "$ans" = n-o -z"$ans" ]

then

continue 2

else

echo -n "Process week $week of $month?"

read ans

if [ "$ans" = n -o -z "$ans"]

then

continue

else

echo "Now processing week $week of $month."

sleep 1# Commands go here

echo"Doneprocessing..."

fi

fi

done

done


25.runit.sh

#!/bin/bash

# Scriptname:runit.sh


PS3="Select a program to execute:"

select programin 'ls -F' pwd date

do

$program

done

26.goodboy.sh

#!/bin/bash

# Scriptname:goodboys.sh

PS3="Please choose one of the threeboys : "

select choice in tom dan guy

#select choice

do

case $choice in

tom)

echo Tom is a cool dude!

break;;# break out of the select loop

dan | guy )

echo Dan and Guy are both wonderful.

break;;

*)

echo "$REPLY is not one of your choices"

echo "Try again."

;;

esac

done


27.doit.sh

#!/bin/bash

# Name: doit.sh

# Purpose: shiftthrough command line arguments

# Usage: doit.sh[args]

while (( $# > 0 ))# or [ $# -gt 0 ]

do

echo$*

shift

done


28.shft.sh

#!/bin/bash

# Using 'shift' tostep through all the positional parameters.


until [ -z "$1" ]# Until all parameters used up...

do

echo "$1"

shift

done

echo# Extra line feed.

exit 0




29.ex4str

#!/bin/bash

dirname="/usr/bin/local/bin";

echo "dirname=$dirname"

echo -n '${#dirname}='; sleep 4;echo"${#dirname}"

echo

echo -n '${dirname:4}=';sleep 4;echo "${dirname:4}"

echo

echo -n '${dirname:8:6}='; sleep 4; echo${dirname:8:6}

echo

echo -n '${dirname#*bin}='; sleep 4; echo${dirname#*bin}

echo

echo -n '${dirname##*bin}='; sleep 4;echo${dirname##*bin}

echo

echo -n '${dirname%bin}='; sleep 4;echo${dirname%bin}

echo

echo -n '${dirname%%bin}='; sleep 4;echo${dirname%%bin}

echo

echo -n '${dirname%bin*}='; sleep 4;echo${dirname%bin*}

echo

echo -n '${dirname%%bin*}='; echo${dirname%%bin*}

echo

echo -n '${dirname/bin/sbin}='; echo${dirname/bin/sbin}

echo

echo -n '${dirname//bin/lib}='; echo${dirname//bin/lib}

echo

echo -n '${dirname/bin*/lib}='; echo${dirname/bin*/lib}

30.ex4fun2.sh

#!/bin/bash

JUST_A_SECOND=1

fun ()

{ #A somewhat more complex function

i=0

REPEATS=5

echo

echo "And now the fun really begins."

echo

sleep $JUST_A_SECOND #Hey, wait a second!

while [ $i -lt $REPEATS ]

do

echo "----------FUNCTIONS---------->"

echo "<------------ARE-------------"

echo "<------------FUN------------>"

echo

let "i+=1"

done

}

# Now, callthe functions.

fun

exit 0


31.ex4fun3.sh

# f1

# Will give anerror message, since function "f1" not yet defined.

# declare -ff1

# This doesn'thelp either.

# f1

# Still an errormessage.

# However...

f1 () {

echo "Calling function\"f2\" from within function \"f1\"."

f2

}

f2 () {

echo "Function\"f2\"."

}

# f1

# Function"f2" is not actually called until this point

# although it isreferenced before its definition.

# This ispermissible.


32.ex4fun4.sh

#!/bin/bash

# Functions andparameters

DEFAULT=default # Default param value.

func2 () {

if [ -z "$1" ]# Is parameter #1 zero length?

then

echo "-Parameter #1is zero length -"

else

echo "-Param #1 is\"$1\" -"

fi

variable=${1:-$DEFAULT}

echo "variable = $variable"

if [ -n "$2" ]

then

echo "- Parameter #2is \"$2\" -"

fi

return 0

}

echo

echo "Nothing passed"

func2# Called withno params

echo

echo "One parameter passed."

func2 first#Called with one param

echo

echo "Two parameters passed."

func2 first second # Called with two params

echo

echo "\"\"\"second\" passed."

func2 "" second

# The firstparameter is of zero?length

echo

exit 0

# End ofscript

33.ex4fun5.sh

#!/bin/bash

# function andcommand line arguments

# Call this scriptwith a command line argument,

# something like$0 arg1.

func ()

{

echo "$1"

}

echo "First call to function: no argpassed."

echo "See if command-line arg isseen."

Func# No!Command-line arg not seen.

echo"=================================="

echo

echo "Second call to function:command-line arg passed explicitly."

func $1

# Now it'sseen!

exit 0

34.ex4fun6.sh

#!/bin/bash

# purpose: Maximumof two integers.

max2 () #Returns larger of two numbers.

{if [ -z $2 ]

then

echo "Need to pass two parameters to the function."

exit 1

fi

if [[ $1 == $2 ]] # [ $1 -eq $2]

then

echo "The two numbers are equal."

exit 0

else

if [ $1 -gt $2 ]

then

return $1

else

return $2

fi

fi

}

read num1 num2

echo "num1=$num1, num2=$num2"

max2 $num1 $num2

return_val=$?

echo "The larger of the two numbersis $return_val."

exit 0