当前位置: 技术问答>linux和unix
关于脚本编写得几个问题
来源: 互联网 发布时间:2015-10-05
本文导语: 2.Create a shell program called my_ask.sh that will ask the user if he or she would like to see the contents of the current directory. Inform the user that you are looking for a yes or no answer.Issue an error message if the user does not ...
2.Create a shell program called my_ask.sh that will ask the user if he or she would like to see the contents of the current directory. Inform the user that you are looking for a yes or no answer.Issue an error message if the user does not enter yes or no. If the user enters yes display the contents of the current directory. If the user enters no, ask what directory he
or she would like to see the contents of. Get the user's input and display the contents of that directory. Remember to verify that the requested directory exists prior to displaying its contents.
#!/bin/bash
#This script Create a shell program called my_ask.sh that will ask the user if he or she would like
#to see the contents of the current directory. Inform the user that you are looking for a yes or no
#answer.Issue an error message if the user does not enter yes or no. If the user enters yes display
#the contents of the current directory. If the user enters no, ask what directory he
#or she would like to see the contents of. Get the user's input and display the contents of that
#directory. Remember to verify that the requested directory exists prior to displaying its contents.
echo -e "Would you like to see the contents of the current directory? [yes/no]c"
read answer
if [ "$answer" = "yes" -o "$answer" = "y" ] ; then
ls -l $PWD
elif [ "$answer" = "no" -o "$answer" = "n" ] ; then
echo "Which directory would you like to see?(Plz input the standard patch,such as /home/user/)"
read answer2;ls -l $answer2
else
echo "Error input!"
fi
3.Create a shell program called my_menu.sh that will display a simple menu that has three options.
a. The first option will run who;
b. The second option will run pwd;
c. Quit
The menu should be redisplayed after each selection is completed, until the user enters 3 .
#!/bin/sh
echo Please choose your option...
echo "a. The first option will run who;"
echo "b. The second option will run pwd;"
echo "c. Quit"
read answer
case "$answer" in
[aA] ) who ;;
[bB] ) pwd ;;
[cC3] ) exit ;;
* ) echo wrong choose ;;
esac
4.Create a program my_cp.sh which will copy one file to another. The program will accept two command line arguments, a source and a destination. Check for the following
situations:
a) It should make sure that the source and destination do not reference the same file.
b) The program should verify that the destination is a file.
c) The program should verify that the source file exists.
d) The program should check to see if the destination exists. If it does, ask the user if he or she wants to overwrite it.
#!/bin/sh
echo "Please input the name of the file:n"
read file
if test ! -f $file ;then # check if $xx is not existed and is not a valid regular file
echo $file is not existed or is not a regular file
continue # return back to line number 1
fi # if construct finish
echo " Please input the name of the directory you want to copy: n"
read dir
if test ! -d $file ;then # check if $dir is not existed and is not a valid regular directory
echo $dir is not existed or is not a regular directory.
continue # return back to line number 1
fi # if construct finish
echo copy $file
cp -i $file $dir #copy file
break
done # loop finished
echo program finished
exit
or she would like to see the contents of. Get the user's input and display the contents of that directory. Remember to verify that the requested directory exists prior to displaying its contents.
#!/bin/bash
#This script Create a shell program called my_ask.sh that will ask the user if he or she would like
#to see the contents of the current directory. Inform the user that you are looking for a yes or no
#answer.Issue an error message if the user does not enter yes or no. If the user enters yes display
#the contents of the current directory. If the user enters no, ask what directory he
#or she would like to see the contents of. Get the user's input and display the contents of that
#directory. Remember to verify that the requested directory exists prior to displaying its contents.
echo -e "Would you like to see the contents of the current directory? [yes/no]c"
read answer
if [ "$answer" = "yes" -o "$answer" = "y" ] ; then
ls -l $PWD
elif [ "$answer" = "no" -o "$answer" = "n" ] ; then
echo "Which directory would you like to see?(Plz input the standard patch,such as /home/user/)"
read answer2;ls -l $answer2
else
echo "Error input!"
fi
3.Create a shell program called my_menu.sh that will display a simple menu that has three options.
a. The first option will run who;
b. The second option will run pwd;
c. Quit
The menu should be redisplayed after each selection is completed, until the user enters 3 .
#!/bin/sh
echo Please choose your option...
echo "a. The first option will run who;"
echo "b. The second option will run pwd;"
echo "c. Quit"
read answer
case "$answer" in
[aA] ) who ;;
[bB] ) pwd ;;
[cC3] ) exit ;;
* ) echo wrong choose ;;
esac
4.Create a program my_cp.sh which will copy one file to another. The program will accept two command line arguments, a source and a destination. Check for the following
situations:
a) It should make sure that the source and destination do not reference the same file.
b) The program should verify that the destination is a file.
c) The program should verify that the source file exists.
d) The program should check to see if the destination exists. If it does, ask the user if he or she wants to overwrite it.
#!/bin/sh
echo "Please input the name of the file:n"
read file
if test ! -f $file ;then # check if $xx is not existed and is not a valid regular file
echo $file is not existed or is not a regular file
continue # return back to line number 1
fi # if construct finish
echo " Please input the name of the directory you want to copy: n"
read dir
if test ! -d $file ;then # check if $dir is not existed and is not a valid regular directory
echo $dir is not existed or is not a regular directory.
continue # return back to line number 1
fi # if construct finish
echo copy $file
cp -i $file $dir #copy file
break
done # loop finished
echo program finished
exit
|
不知道你想说什么问题?