当前位置: 技术问答>linux和unix
4个初级的shell编程
来源: 互联网 发布时间:2016-01-17
本文导语: 请大侠们帮忙做一下: 1.编写一个shell脚本,它把第二个位置参数所指定的文件拷贝到第一个位置参数指定的目录下。 2.编写一个shell脚本,显示当天日期,查找某给定的用户是否在系统中工作,如果在发一个"HELLO!". 3....
请大侠们帮忙做一下:
1.编写一个shell脚本,它把第二个位置参数所指定的文件拷贝到第一个位置参数指定的目录下。
2.编写一个shell脚本,显示当天日期,查找某给定的用户是否在系统中工作,如果在发一个"HELLO!".
3.编写一个shell脚本,求斐波那契函数前十项总和。
4.利用for循环把当前目录下后缀是.c的文件移动到指定位置下,显示移动后指定目录的内容。
1.编写一个shell脚本,它把第二个位置参数所指定的文件拷贝到第一个位置参数指定的目录下。
2.编写一个shell脚本,显示当天日期,查找某给定的用户是否在系统中工作,如果在发一个"HELLO!".
3.编写一个shell脚本,求斐波那契函数前十项总和。
4.利用for循环把当前目录下后缀是.c的文件移动到指定位置下,显示移动后指定目录的内容。
|
Q1
#! /bin/sh
# man command for option details
#
if [ $# -ne 2 ] ; then
echo "Usage: $0 "
exit 1
fi
if [ ! -d $1 -o ! -r $1 ] ; then
echo "The target dir doesn't exsit or not writable"
exit 1
fi
if [ ! -f $2 -o ! -r $2 ] ; then
echo "The source file doesn't exsit or not readible"
exit 1
fi
while : #infinite loop untill valid input has been given
do
echo "Remove the source file?(yes/no)"
read ANS
case "$ANS" in
"yes")
cp -f -i $2 $1
rm -f $2
break;;
"no")
cp -i $2 $1
break;;
esac
done
Q2
#! /bin/sh
# man command for option details
#
if [ $# -ne 1 ] ; then
echo "Usage: $0 "
exit 1
fi
USER=`who | awk '{print $1}' | grep "^{1}$" | head -n 1`
TTY=`who | awk '{print $2}'`
if [ ! -z "$USER" ] ; then
# send msg to all ttys that are used by user
#
for ttynum in $TTY
do
echo "HELLO! $USER" | write $USER $ttynum > /dev/null 2>&1
done
echo "Msgs have been sent to $USER!"
fi
Q3
#! /bin/sh
# man command for option details
#
NUMBER=10
COUNT=1
a=0
b=1
while [ $COUNT -lt $NUMBER ] ; thena
# f(n)=f(n-1)+f(n-2)
# shift value of vars to the left by 1
#
c=`expr $a + $b`
a=$b
b=$c
COUNT=`expr $COUNT + 1`
done
echo "f($NUMBER) is : $c"
Q4
#! /bin/sh
# man command for option details
#
if [ $# -ne 1 ] ; then
echo "Usage: $0 "
exit 1
fi
if [ ! -d $1 ] ; then
echo "target dir doesn't exsit"
exit 1
fi
for i in ./*.c
do
mv -i $i $1 > /dev/null 2>&1
done
ls -la $1
#! /bin/sh
# man command for option details
#
if [ $# -ne 2 ] ; then
echo "Usage: $0 "
exit 1
fi
if [ ! -d $1 -o ! -r $1 ] ; then
echo "The target dir doesn't exsit or not writable"
exit 1
fi
if [ ! -f $2 -o ! -r $2 ] ; then
echo "The source file doesn't exsit or not readible"
exit 1
fi
while : #infinite loop untill valid input has been given
do
echo "Remove the source file?(yes/no)"
read ANS
case "$ANS" in
"yes")
cp -f -i $2 $1
rm -f $2
break;;
"no")
cp -i $2 $1
break;;
esac
done
Q2
#! /bin/sh
# man command for option details
#
if [ $# -ne 1 ] ; then
echo "Usage: $0 "
exit 1
fi
USER=`who | awk '{print $1}' | grep "^{1}$" | head -n 1`
TTY=`who | awk '{print $2}'`
if [ ! -z "$USER" ] ; then
# send msg to all ttys that are used by user
#
for ttynum in $TTY
do
echo "HELLO! $USER" | write $USER $ttynum > /dev/null 2>&1
done
echo "Msgs have been sent to $USER!"
fi
Q3
#! /bin/sh
# man command for option details
#
NUMBER=10
COUNT=1
a=0
b=1
while [ $COUNT -lt $NUMBER ] ; thena
# f(n)=f(n-1)+f(n-2)
# shift value of vars to the left by 1
#
c=`expr $a + $b`
a=$b
b=$c
COUNT=`expr $COUNT + 1`
done
echo "f($NUMBER) is : $c"
Q4
#! /bin/sh
# man command for option details
#
if [ $# -ne 1 ] ; then
echo "Usage: $0 "
exit 1
fi
if [ ! -d $1 ] ; then
echo "target dir doesn't exsit"
exit 1
fi
for i in ./*.c
do
mv -i $i $1 > /dev/null 2>&1
done
ls -la $1
|
我来写第一个,呵呵,初级练练手
if [ -d $1 -a -f $2] ;then
cp $2 $1
fi
if [ -d $1 -a -f $2] ;then
cp $2 $1
fi