您的位置:首页 > 运维架构 > Shell

linux shell script 例子1

2010-02-04 09:22 176 查看
1. 如何获得脚本参数:

#!/bin/she
#
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) param="$2"
echo "found the -b option";;
-c) echo "found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done

count=1
for para in "#@"
do
echo "parameter #$count : $para"
count=$[ $count + 1 ]
done


2.判断输入是否符合条件:

#!/bin/sh
#
read input
if [[ $input == r* && ${#input} -eq 10 ]]; then #check the input character bits
echo "yes, your input right."
else
echo "no ,your input error."
fi


3.如何让用户输入,如果你想得到用户输入一定要记得read command.

#!/bin/sh
#
#
read -n1 -t 10 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
echo "fine, continue on ... ";;
N | n) echo
echo "ok, goodbye
exit;;
esac

echo "this is the end of the script"


4.从文件中读出数据

#!/bin/sh
#
#
#
count=1
cat testfile | while read line
do
echo "Line $count : $line"
count=$[ $count + 1 ]
done

echo "finished processing the file"
#you can also use the read command to read data stored in a file on the linux system. each call to the read command reads a single line of text from the file. when there are no more lines left in the file,the read command will exit with a non-zero exit status.


5.创建自己的重定向文件描述符

#!/bin/bash
# storing STDOUT, then coming back to it
exec 3>&1
exec 1>test14out
echo "This should store in the output file"
echo "along with this line."
exec 1>&3
echo "Now things should be back to normal"
#This example is a little crazy, so let’s walk through it piece by piece. First, the script redirects file descriptor 3 to the current location of file descriptor 1, which is STDOUT. This means that any output sent to file descriptor 3 will go to the monitor.The second exec command redirects STDOUT to a file. The shell will now redirect any output sent to STDOUT directly to the output file. However, file descriptor 3 still points to the original location of STDOUT, which is the monitor. If you send output data to file descriptor 3 at this point, it’ll still go to the monitor, even though STDOUT is redirected.After sending some output to STDOUT, which points to a file, the script then redirects STDOUT to the current location of file descriptor 3, which is still set to the monitor. This means that nowSTDOUT is pointing to its original location, the monitor.


6.创建重定向输入描述符
#!/bin/bash
# redirecting input file descriptors
exec 6<&0
exec 0< testfile
count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now? " answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "Sorry, this is the end.";;
esac
#In this example, file descriptor 6 is used to hold the location for STDIN. The script then redirects STDIN to a file. All of the input for the read command comes from the redirected STDIN, which is now the input file.When all of the lines have been read, the script returns STDIN to its original location by redirecting it to file descriptor 6. The script tests to make sure that STDIN is back to normal by using another read command, which this time waits for input from the keyboard.


7.创建可读写的文件描述符

#!/bin/bash
# testing input/output file descriptor
exec 3<> testfile
read line <&3
echo "Read: $line"
echo "This is a test line" >&3
$ cat testfile
This is the first line.
This is the second line.
This is the third line.
$ ./test16
Read: This is the first line.
$ cat testfile
This is the first line.
This is a test line
ine.
This is the third line.


以上所有例子均摘自 wiley linux command line and shell scripting bible. may 2008
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: