From 994371b76594ac667d2f074d62a23c75b1adb1f5 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 May 2016 09:51:46 +0800 Subject: [PATCH] 2016-05-15 --- 控制脚本/定时执行脚本.sh | 5 ++ 控制脚本/捕捉信号.sh | 16 ++++++ 控制脚本/捕捉脚本的退出.sh | 13 +++++ 控制脚本/移除捕捉.sh | 16 ++++++ ...出当前脚本打开的文件描述符.sh | 14 +++++ 理解输入和输出/创建临时目录.sh | 16 ++++++ .../创建本地临时文件.sh | 22 ++++++++ ...以重定向的文件描述符中恢复.sh | 13 +++++ .../关闭文件描述符.sh | 16 ++++++ .../创建读写文件描述符.sh | 7 +++ .../创建输入文件描述符.sh | 22 ++++++++ .../创建输出文件描述符.sh | 9 ++++ .../在tmp目录创建临时文件.sh | 12 +++++ .../快速清除文件或日志.sh | 5 ++ 理解输入和输出/记录信息.sh | 10 ++++ 脚本函数/4.sh | 1 + 脚本函数/new | 8 +++ 脚本函数/从函数返回数组.sh | 25 +++++++++ 脚本函数/使用return命令.sh | 12 +++++ .../使用全局变量带来的问题.sh | 21 ++++++++ 脚本函数/使用函数输出.sh | 11 ++++ .../使用命令行中传递的参数.sh | 16 ++++++ 脚本函数/使用局部变量.sh | 21 ++++++++ 脚本函数/使用库函数.sh | 8 +++ 脚本函数/全局变量.sh | 12 +++++ 脚本函数/函数递归.sh | 16 ++++++ 脚本函数/在函数中使用参数.sh | 32 ++++++++++++ 脚本函数/基本的脚本函数.sh | 17 ++++++ 脚本函数/想函数传数组数据.sh | 23 ++++++++ 脚本函数/累加数组中的值.sh | 20 +++++++ 脚本函数/脚本库.sh | 20 +++++++ 脚本函数/默认退出状态码.sh | 52 +++++++++++++++++++ 32 files changed, 511 insertions(+) create mode 100644 控制脚本/定时执行脚本.sh create mode 100644 控制脚本/捕捉信号.sh create mode 100644 控制脚本/捕捉脚本的退出.sh create mode 100644 控制脚本/移除捕捉.sh create mode 100644 理解输入和输出/列出当前脚本打开的文件描述符.sh create mode 100644 理解输入和输出/创建临时目录.sh create mode 100644 理解输入和输出/创建本地临时文件.sh create mode 100644 理解输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh create mode 100644 理解输入和输出/创建自己的重定向/关闭文件描述符.sh create mode 100644 理解输入和输出/创建自己的重定向/创建读写文件描述符.sh create mode 100644 理解输入和输出/创建自己的重定向/创建输入文件描述符.sh create mode 100644 理解输入和输出/创建自己的重定向/创建输出文件描述符.sh create mode 100644 理解输入和输出/在tmp目录创建临时文件.sh create mode 100644 理解输入和输出/快速清除文件或日志.sh create mode 100644 理解输入和输出/记录信息.sh create mode 100644 脚本函数/4.sh create mode 100644 脚本函数/new create mode 100644 脚本函数/从函数返回数组.sh create mode 100644 脚本函数/使用return命令.sh create mode 100644 脚本函数/使用全局变量带来的问题.sh create mode 100644 脚本函数/使用函数输出.sh create mode 100644 脚本函数/使用命令行中传递的参数.sh create mode 100644 脚本函数/使用局部变量.sh create mode 100644 脚本函数/使用库函数.sh create mode 100644 脚本函数/全局变量.sh create mode 100644 脚本函数/函数递归.sh create mode 100644 脚本函数/在函数中使用参数.sh create mode 100644 脚本函数/基本的脚本函数.sh create mode 100644 脚本函数/想函数传数组数据.sh create mode 100644 脚本函数/累加数组中的值.sh create mode 100644 脚本函数/脚本库.sh create mode 100644 脚本函数/默认退出状态码.sh diff --git a/控制脚本/定时执行脚本.sh b/控制脚本/定时执行脚本.sh new file mode 100644 index 0000000..44a4049 --- /dev/null +++ b/控制脚本/定时执行脚本.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# testing the at command + +at -f 4.sh 22:10 diff --git a/控制脚本/捕捉信号.sh b/控制脚本/捕捉信号.sh new file mode 100644 index 0000000..9d4473b --- /dev/null +++ b/控制脚本/捕捉信号.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# testing signal trapping + +trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT SIGTERM + +echo this is a test program + +count=1 + +while [ $count -le 10 ] +do + echo "Loop #$count" + sleep 5 + count=$[ $count+1 ] +done diff --git a/控制脚本/捕捉脚本的退出.sh b/控制脚本/捕捉脚本的退出.sh new file mode 100644 index 0000000..ce3d5aa --- /dev/null +++ b/控制脚本/捕捉脚本的退出.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# trapping the script exit + +trap "echo byebye" EXIT + +count=1 +while [ $count -le 5 ] +do + echo "Loop #$count" + sleep 3 + count=$[ $count + 1 ] +done diff --git a/控制脚本/移除捕捉.sh b/控制脚本/移除捕捉.sh new file mode 100644 index 0000000..22f4083 --- /dev/null +++ b/控制脚本/移除捕捉.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# removeing a set trap + +trap "echo byebye" EXIT + +count=1 +while [ $count -le 5 ] +do + echo "Loop #$count" + sleep 3 + count=$[ $count + 1 ] +done +#移除捕捉 +trap - EXIT +echo "I just removed the trap" diff --git a/理解输入和输出/列出当前脚本打开的文件描述符.sh b/理解输入和输出/列出当前脚本打开的文件描述符.sh new file mode 100644 index 0000000..417bfa4 --- /dev/null +++ b/理解输入和输出/列出当前脚本打开的文件描述符.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# testing lsof with file descriptors + +exec 3>test +exec 6>test +exec 7 $tempfile1 +exec 8> $tempfile2 + +echo "Sending data to directory $tempdir" +echo "This is a test line of data for $tempfile1" >&7 +echo "This is a test line of data for $tempfile2" >&8 + diff --git a/理解输入和输出/创建本地临时文件.sh b/理解输入和输出/创建本地临时文件.sh new file mode 100644 index 0000000..6d15a9d --- /dev/null +++ b/理解输入和输出/创建本地临时文件.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# creating and using a temp file + +tempfile=`mktemp test.XXXXXX` + +exec 3>$tempfile + +echo "This script writes to temp file $tempfile" + +echo "This is the first line" >&3 +echo "This is the second line" >&3 +echo "This is the last line" >&3 + +exec 3>&- + +echo "Done creating temp file. The contents are:" + +cat $tempfile + +rm -f $tempfile 2>/dev/null + diff --git a/理解输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh b/理解输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh new file mode 100644 index 0000000..0face23 --- /dev/null +++ b/理解输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +#storing STDOUT, then coming back to it + +exec 3>&1 +exec 1>test + +echo "This should store in output file" +echo "along with this line" + +exec 1>&3 + +echo "Now things should be back to normal" diff --git a/理解输入和输出/创建自己的重定向/关闭文件描述符.sh b/理解输入和输出/创建自己的重定向/关闭文件描述符.sh new file mode 100644 index 0000000..154e2bf --- /dev/null +++ b/理解输入和输出/创建自己的重定向/关闭文件描述符.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# testing closing file descriptors + +exec 3>test +echo "This is a test line of data" >&3 + +# closing file descriptor +exec 3>&- + +echo "This won't work" >&3 + +cat test + +#覆盖前一个test文件 +exec 3>test +echo "This'll be bad" >&3 diff --git a/理解输入和输出/创建自己的重定向/创建读写文件描述符.sh b/理解输入和输出/创建自己的重定向/创建读写文件描述符.sh new file mode 100644 index 0000000..53b7645 --- /dev/null +++ b/理解输入和输出/创建自己的重定向/创建读写文件描述符.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# testing inpiut/output file descriptor + +exec 3<> test +read line <&3 +echo "Read: $line" +echo "This is the test line" >&3 diff --git a/理解输入和输出/创建自己的重定向/创建输入文件描述符.sh b/理解输入和输出/创建自己的重定向/创建输入文件描述符.sh new file mode 100644 index 0000000..c47be1b --- /dev/null +++ b/理解输入和输出/创建自己的重定向/创建输入文件描述符.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# redirecting input file descriptors + +exec 3>&1 +echo "This is the 3 file descriptor" >&3 + +exec 6>&0 +exec 0test + +echo "This should display on the monitor" +echo "and this should be stored in the file" >&3 +echo "Then this should be back on the monitor" diff --git a/理解输入和输出/在tmp目录创建临时文件.sh b/理解输入和输出/在tmp目录创建临时文件.sh new file mode 100644 index 0000000..a4497a1 --- /dev/null +++ b/理解输入和输出/在tmp目录创建临时文件.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# creating a temp file in /tmp + +tempfile=`mktemp -t tmp.XXXXXX` + +echo "This is a test file" > $tempfile +echo "This is the second line of the test" >> $tempfile + +echo ”The temp is locate at : $tempfile“ +cat $tempfile +rm -f $tempfile diff --git a/理解输入和输出/快速清除文件或日志.sh b/理解输入和输出/快速清除文件或日志.sh new file mode 100644 index 0000000..bd9fa64 --- /dev/null +++ b/理解输入和输出/快速清除文件或日志.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# 清除日志 + +cat /dev/null > [Logname] diff --git a/理解输入和输出/记录信息.sh b/理解输入和输出/记录信息.sh new file mode 100644 index 0000000..84d31db --- /dev/null +++ b/理解输入和输出/记录信息.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# using the tee command for logging +#将输入一边发送到STDOUT,一边发送到日志文件 +tempfile=test +echo "This is the start of the test" | tee $tempfile +echo "This is the second line of the test" | tee -a $tempfile +echo "This is the end line of the test" | tee -a $tempfile + + diff --git a/脚本函数/4.sh b/脚本函数/4.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/脚本函数/4.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/脚本函数/new b/脚本函数/new new file mode 100644 index 0000000..c7cbc45 --- /dev/null +++ b/脚本函数/new @@ -0,0 +1,8 @@ +#!/bin/bash + +for (( i=1; i<=4; i++ )) +do + touch $i.sh + chmod 764 $i.sh + echo "#!/bin/bash" > $i.sh +done diff --git a/脚本函数/从函数返回数组.sh b/脚本函数/从函数返回数组.sh new file mode 100644 index 0000000..4425f55 --- /dev/null +++ b/脚本函数/从函数返回数组.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# returning an array value + +function arraydblr { + local origarry + local newarray + local elements + local i + origarry=(`echo "$@"`) + newarray=(`echo "$@"`) + elements=$[ $# - 1 ] + for (( i=0; i<=$elements; i++ )) + { + newarray[$i]=$[ ${origarry[$i]} * 2 ] + } + + echo ${newarray[*]} +} + +myarray=(1 2 3 4 5) +echo "The original array is : ${myarray[*]}" +arg1=`echo ${myarray[*]}` +result=(`arraydblr $arg1`) +echo "The new array is : ${result[*]}" diff --git a/脚本函数/使用return命令.sh b/脚本函数/使用return命令.sh new file mode 100644 index 0000000..412b86a --- /dev/null +++ b/脚本函数/使用return命令.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# using the return command in a function + +function db1 { + read -p "Enter a value:" value + echo "doubling the value" + return $[ $value * 2 ] +} + +db1 +echo "The new value is $?" diff --git a/脚本函数/使用全局变量带来的问题.sh b/脚本函数/使用全局变量带来的问题.sh new file mode 100644 index 0000000..2758588 --- /dev/null +++ b/脚本函数/使用全局变量带来的问题.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# demonstrating a bad use of variables + +function func1 { + temp=$[ $value + 5 ] + result=$[ $temp * 2 ] +} + +temlp=4 +value=6 + +func1 +echo "The result is $result" + +if [ $temp -gt $value ] +then + echo "Temp is larger" +else + echo "temp is smaller" +fi diff --git a/脚本函数/使用函数输出.sh b/脚本函数/使用函数输出.sh new file mode 100644 index 0000000..4959d64 --- /dev/null +++ b/脚本函数/使用函数输出.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# using the echo to return a value + +function db1 { + read -p "Enter a value:" value + echo $[ $value*2 ] +} + +result=`db1` +echo "The new value is $result" + diff --git a/脚本函数/使用命令行中传递的参数.sh b/脚本函数/使用命令行中传递的参数.sh new file mode 100644 index 0000000..d31fe8b --- /dev/null +++ b/脚本函数/使用命令行中传递的参数.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# using a global variable to pass a value + +function db1 { + # $1和$2 不能从命令行中传递,只能调用函数时,手动传递 + echo $[ $1 * $2 ] +} + +if [ $# -eq 2 ] +then + value=`db1 $1 $2` + echo "The result is $value" +else + echo "Usage: badtest1 a b" +fi diff --git a/脚本函数/使用局部变量.sh b/脚本函数/使用局部变量.sh new file mode 100644 index 0000000..198bd81 --- /dev/null +++ b/脚本函数/使用局部变量.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# demonstrating the local keyword + +function func1 { + local temp=$[ $value +5 ] + result=$[ $temp * 2 ] +} + +temp=4 +value=6 + +func1 + +echo "The result is $result" +if [ $temp -gt $value ] +then + echo "temp is larger" +else + echo "temp is smaller" +fi diff --git a/脚本函数/使用库函数.sh b/脚本函数/使用库函数.sh new file mode 100644 index 0000000..4b048c3 --- /dev/null +++ b/脚本函数/使用库函数.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +#using a library file the wrong way + +. ./脚本库.sh + +result=`addem 10 15` +echo "The result is $result" diff --git a/脚本函数/全局变量.sh b/脚本函数/全局变量.sh new file mode 100644 index 0000000..c4c3d70 --- /dev/null +++ b/脚本函数/全局变量.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# using a global variable to pass a value + +function db1 { + value=$[ $value * 2 ] +} + +read -p "Enter a value: " value +db1 +echo "The new value is : $value" + diff --git a/脚本函数/函数递归.sh b/脚本函数/函数递归.sh new file mode 100644 index 0000000..b362092 --- /dev/null +++ b/脚本函数/函数递归.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +function factorial { + if [ $1 -eq 1 ] + then + echo 1 + else + local temp=$[ $1 -1 ] + local result=`factorial $temp` + echo $[ $result * $1 ] + fi +} + +read -p "Please input a value: " value +result=`factorial $value` +echo "The factorial of $value is: $result" diff --git a/脚本函数/在函数中使用参数.sh b/脚本函数/在函数中使用参数.sh new file mode 100644 index 0000000..8ea4b1f --- /dev/null +++ b/脚本函数/在函数中使用参数.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# passing parameters to a function + +function addem { + if [ $# -eq 0 ] || [ $# -gt 2 ] + then + echo -1 + elif [ $# -eq 1 ] + then + echo $[ $1 + $1 ] + else + echo $[ $1 + $2 ] + fi +} + +echo -n "Adding 10 and 15:" +value=`addem 10 15` +echo $value + +echo -n "Let's try adding just one number: " +value=`addem 10` +echo $value + +echo -n "Now trying adding no number: " +value=`addem` +echo $value + +echo -n "Finally, try adding three or more numbers: " +value=`addem 10 15 20` +echo $value + diff --git a/脚本函数/基本的脚本函数.sh b/脚本函数/基本的脚本函数.sh new file mode 100644 index 0000000..e881e97 --- /dev/null +++ b/脚本函数/基本的脚本函数.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# using a function in script + +function func1 { + echo "This is an example of a function" +} + +count=1 +while [ $count -le 5 ] +do + func1 + count=$[ $count+1 ] +done +echo "This is the end of the loop" +func1 +echo "Now this is the end of the script" diff --git a/脚本函数/想函数传数组数据.sh b/脚本函数/想函数传数组数据.sh new file mode 100644 index 0000000..ab8b48d --- /dev/null +++ b/脚本函数/想函数传数组数据.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# trying to pass an array variable + +function testit { + echo "The parameters are : $@" + + #函数只会读取数组变量的第一个值 + thisarray=$1 + echo "The received array is ${thisarray[*]}" + + local newarray + newarray=(`echo "$@"`) + echo "The new array value is : ${newarray[*]}" +} + +myarray=(1 2 3 4 5) +echo "The original array is : ${myarray[*]}" + +#将数组变量当成一个函数参数,函数只会去函数变量第一个值 +#testit $myarray + +testit ${myarray[*]} diff --git a/脚本函数/累加数组中的值.sh b/脚本函数/累加数组中的值.sh new file mode 100644 index 0000000..266f77c --- /dev/null +++ b/脚本函数/累加数组中的值.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +#adding values in the array + +function addarray { + local sum=0 + local newarray + newarray=(`echo "$@"`) + for value in ${newarray[*]} + do + sum=$[ $sum + $value ] + done + echo $sum +} + +myarray=(1 2 3 4 5) +echo "The original array is : ${myarray[*]}" +arg1=`echo ${myarray[*]}` +result=`addarray $arg1` +echo "The result is $result" diff --git a/脚本函数/脚本库.sh b/脚本函数/脚本库.sh new file mode 100644 index 0000000..1c52b0f --- /dev/null +++ b/脚本函数/脚本库.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# myscript functions + +function addem { + echo $[ $1 + $2 ] +} + +function multem { + echo $[ $1 * $2 ] +} + +function divem { + if [ $2 -ne 0] + then + echo $[ $1/$2 ] + else + echo -1 + fi +} diff --git a/脚本函数/默认退出状态码.sh b/脚本函数/默认退出状态码.sh new file mode 100644 index 0000000..bd2d5b4 --- /dev/null +++ b/脚本函数/默认退出状态码.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# testing the exit status of a function + +func1() { + echo "Trying to display a non-existent file" + ls -l badfile +} + +#由于最后一条命令未执行成功,返回的状态码非0 +echo "testing the function" +func1 +echo "The exit status is : $?" + +func2() { + ls -l badfile + echo "Another test to display a non-existent file" +} + +#由于最后一条命令echo执行成功,返回的状态码为0 +echo "Another test" +func2 +echo "The exit status is : $?" + + + + + + + + + + + + + + + + + + + + + + + + + + + + +