LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 869|回复: 5

问一个关于脚本编写的问题?

[复制链接]
发表于 2005-6-23 18:31:05 | 显示全部楼层 |阅读模式
#!/bin/sh
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
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
$ sh my_ask.sh
Would you like to see the contents of the current directory? [yes/no]yes
': not a valid identifier`answer
my_ask.sh: line 17: syntax error near unexpected token `fi'
my_ask.sh: line 17: `fi'
  
if和fi哪里用错了?

顺便问一个:
  Assign to your shell prompt the string : Way to go YOUR_USER_NAME $
我以前记的,现在记不清了
谁帮个忙啊?
发表于 2005-6-23 19:38:28 | 显示全部楼层
先把代码帖好,再让别人帮你看,行不行?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-23 19:39:40 | 显示全部楼层
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/sh  
  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  
  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 "lease input the name of the file:\n"
read file
if test ! -f $file      # 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      # 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
回复 支持 反对

使用道具 举报

发表于 2005-6-23 21:38:41 | 显示全部楼层
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
q=0
until [ $q -eq 1 ]; do
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
done
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-24 10:41:05 | 显示全部楼层
[beiune@localhost linux]$ sh my_menu.sh
my_menu.sh: line 9: syntax error near unexpected token `newline'
'y_menu.sh: line 9: `case "$answer" in

出现这个错误
我的那个程序显示的也是这个错误

Post by dream3401
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
q=0
until [ $q -eq 1 ]; do
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
done
回复 支持 反对

使用道具 举报

发表于 2005-6-28 16:26:16 | 显示全部楼层
我这边运行都正常的啊  ,


#!/bin/bash
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
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
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表