分类:
2012-09-05 21:39:04
原文地址:shell实例 作者:luozhiyong131
Linux系统管理 http://blog.chinaunix.net/u3/117680/showart.php?id=2320993
Hello:
#!/bin/sh
echo "hello world"
s1:
#!/bin/sh
#set varible a
a="hello world"
#print a
echo "A is:"
echo $a
s2:
#!/bin/sh
echo "number of vars:"$#
echo "values of vars:"$*
echo "value of var1:"$1
echo "value of var2:"$2
echo "value of var3:"$3
echo "value of var4:"$4
s3:
#!/bin/bash
hello="var1"
echo $hello
function func1 {
local hello="var2"
echo $hello
}
func1
echo $hello
s4:
#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]
then
echo "please enter 2 no"
exit 1
fi
if [ $a -eq $b ] ; then
echo "number a = number b"
else if [ $a -gt $b ]
then
echo "number a>number b"
elif [ $a -lt $b ]
then
echo "number a
fi
fi
s5:
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder"
[ -f "$folder" ] || echo "this is not file"
S6:
#!/bin/bash
DIR=$1
#if the string empty
if [ "$DIR" = " " ]
then
echo "usage: `basename $0` directory to create" >&2
exit 1
fi
echo "dir" $DIR
if [ -d $DIR ]
then
echo "The directory already exist"
exit 0
else
echo "The directory does exist"
echo -n "Create is now? [Y/N]:"
read create
if [ "$create" = "y" ] || [ "$create" = "Y" ]
then
echo "creating now"
if [ mkdir $DIR ]
then
DIR=" "
fi
if [ "$DIR" = " " ]
then
echo "create directory sucess"
else
echo "create directory error"
fi
elif [ "$create" = "n" ] || [ "$create" = "N" ]
then
echo "does not create directory"
exit 0
else
echo "Errors order"
exit 1
fi
fi
s7:
#!/bin/bash
for day in Sun Mon Tue Wed Thu Fri Sat
do
echo $day
done
s8:
#!/bin/bash
for day in "Sun Mon Tue Wed Thu Fri Sat"
do
echo $day
done
s9
#!/bin/bash
counter=0
for files in *
do
counter=`expr $counter + 1`
done
echo "There are $counter files in `pwd` we need to process"
s10:
#!/bin/bash
echo -n "Pleasw enter number : "
read n
sd=0
rev=""
on=$n
echo "$n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
rev=$( echo $rev$sd )
done
echo "$on in a reverse order $rev"
s11
#!/bin/bash
if [ "$1" = "" ] || [ "$2" = "" ]
then
echo "Please enter file name"
exit 1
fi
if [ -e $2 ]
then
echo "The file already exists"
until [ ! -f $2 ]
do
sleep 1
done
fi
if [ ! `mv $1 $2` ]
then
echo "mv sucessful"
else
echo "mv error"
fi
S12:
#!/bin/bash
echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
[A-Z] ) echo "Uppercase letter";;
[a-z] ) echo "Lowercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac
Linux系统管理 http://blog.chinaunix.net/u3/117680/showart.php?id=2320993