exec 1>testout
echo "this should store in the output file"
echo "along with this line."
exec 1>&3
echo "Now things should be back to nomarl"
exec 4<&0
exec 0<testin
count=1
while read line
do
echo "Line #$count:$line"
count=$[ $count + 1 ]
done
exec 0<&4
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "continue...";
esac
#创建读写文件描述符
exec 8<> testfile
read line <&8
echo "Read:$line"
echo "This is a test line" >&8
#关闭文件描述符
exec 8>&-
#列出文件描述服
#`/usr/sbin/lsof -a -p $$`|more
#禁止命令输出
#2 > /dev/null
#创建本地临时文件
tempfile=`mktemp test.XXXXXX`
exec 4>$tempfile
echo "This is the first line">&3
exec 4>&-
#在/temp中创建临时文件
tmpfile=`mktemp -t tmp.XXXXXX`
echo "The temp file is located at:$tempfile"
cat $tempfile
rm -f $tempfile
#创建临时文件夹
tmpdir=`mktemp -d dir.XXXXXX`
cd $tmpdir
tempfile1=`mktemp temp.XXXXXX`
ls -l
cd ..
#记录消息
a=`date | tee testfile;
cat testfile;
date | tee -a testfile;
cat testfile`
信号处理
#!/bin/bash
#信号处理
trap "echo 'get a sign'" SIGINT SIGTERM
trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 10 ]
do
echo "Loop #$count"
sleep 10
count=$[ $count+1 ]
done
echo "This is the end of the test program"
trap - EXIT#移除捕获
#后台牧师运行
#./test6.sh &
#不使用终端的情况下运行脚本
#nohup ./test6.sh &
#查看作业
#jobs
#重新启动作业
#bg 2(作业序号)//后台
#fg 2//前台
#优先级
#nice -n 10 ./test6.sh
#renice 10 -p 25904(进程号)
#预计时间运行at命令
#at -f test6.sh 20:00
#batch命令,系统平均负载低于0.8时运行,可以设定时间,比at命令更好
#corn表格可以设定循环运行,格式:
#min hour dayofmonth month dayofweek command
#每个月第一天运行:
#12 16 * * 1 command
#每个月最后一天运行:
#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command
函数的使用
#!/bin/bash
#函数
#使用返回值
function func1
{
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=`func1`
echo "the new value is $result"
#传递参数
function func2
{
echo $[ $1+$2 ]
}
result=`func2 2 2`
echo "the new result is $result"
#局部变量, 递归
function func3
{
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1-1 ]
local result=`func3 $temp`










