2016-05-15

dev
unknown 2016-05-15 09:51:46 +08:00
parent 893fec4233
commit 994371b765
32 changed files with 511 additions and 0 deletions

View File

@ -0,0 +1,5 @@
#!/bin/bash
# testing the at command
at -f 4.sh 22:10

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -0,0 +1,14 @@
#!/bin/bash
# testing lsof with file descriptors
exec 3>test
exec 6>test
exec 7<test
lsof -a -p $$ -d0,1,2,3,6,7

View File

@ -0,0 +1,16 @@
#!/bin/bash
# using a temporary directory
tempdir=`mktemp -d dir.XXXXXX`
cd $tempdir
tempfile1=`mktemp temp.XXXXXX`
tempfile2=`mktemp temp.XXXXXX`
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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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 0<test
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

View File

@ -0,0 +1,9 @@
#!/bin/bash
# using an alternative file descriptor
exec 3>test
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"

View File

@ -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

View File

@ -0,0 +1,5 @@
#!/bin/bash
# 清除日志
cat /dev/null > [Logname]

View File

@ -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

View File

@ -0,0 +1 @@
#!/bin/bash

8
脚本函数/new 100644
View File

@ -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

View File

@ -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[*]}"

View File

@ -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 $?"

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,8 @@
#!/bin/bash
#using a library file the wrong way
. ./脚本库.sh
result=`addem 10 15`
echo "The result is $result"

View File

@ -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"

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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[*]}

View File

@ -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"

View File

@ -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
}

View File

@ -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 : $?"