Bash脚本添加,删除,用户,目录和文件

时间:2015-10-16 03:29:18

标签: linux bash

嗨,大家好我需要为我的班级创建一个bash脚本,但我还没有学过任何教授如何实现这一目标的课程。 bash脚本菜单必须具有以下内容:

Please choose one of the following option:
a- Create a File
b- Create a Directory
c- Delete a File
d- Delete a Directory
e- Create a User
f- Delete a User
q- Quit 


Enter your choice: a
     What would you like to name your file (including the path to the location to where you want the file to be): ~/Data
     ~/Data: File Created successfully.
Enter your choice: q

我尝试自己进行搜索,但因为在我不知道从哪里开始之前我从未这样做过。我知道如何至少执行它,我知道如何使用命令完成所有这些,但我不知道如何创建bash脚本文件。我将非常感谢帮助和一些解释,所以我至少可以尝试理解这一点。如果有帮助,我的班级正在使用最新版本的fedora。教授也希望我们使用纳米

2 个答案:

答案 0 :(得分:2)

10-15页的报告有什么问题?包括研究和写作,你每小时看一个小时。一块蛋糕......或者,

你欠了很多啤酒:

#!/bin/bash

while [ "$SEL" != q ]; do
cat >&2 << MENU

  Enter a number to launch:
    a - Create a File
    b - Create a Directory
    c - Delete a File
    d - Delete a Directory
    e - Create a User
    f - Delete a User
    q - Exit
MENU
    printf "\n  Enter your choice: "
    read SEL
    SEL=${SEL,,}
    case $SEL in
        a ) printf "\nWhat would you like to name your File?\n"
            printf "(including the path to the file location): "
            read -r fname
            [ -n $fname ] && touch "$fname" || \
            printf "error: invalid filename.\n"
            unset fname
            ;;
        b ) printf "\nWhat would you like to name your Directory?\n"
            printf "(including the path to the directory location): "
            read -r dname
            [ -n $dname ] && mkdir -p "$dname" || \
            printf "error: invalid directory name.\n"
            unset dname
            ;;
        c ) printf "\nWhat File would you lime to delete?\n"
            printf "(including path): "
            read -r fname
            [ -w "$fname" ] && rm "$fname" || \
            printf "error: invalid filename or insufficent permission.\n"
            unset fname
            ;;
        d ) printf "\nWhat Directory would you lime to delete?\n"
            printf "(including path): "
            read -r dname
            [ -d "$dname" -a -w "$dname" ] && rm -r "$dname" || \
            printf "error: invalid directory or insufficent permission.\n"
            unset dname
            ;;
        e ) printf "\nEnter user name to add: "
            read -r uname
            [ -n $uname ] && useradd $uname
            unset uname
            ;;
        f ) printf "\nEnter user name to delete: "
            read -r uname
            [ -n $uname ] && userdel $uname
            unset uname
            ;;
        q ) exit 0
            ;;
        * ) printf "\nError. Please enter a valid selection.\n"
            ;;
    esac
done

注意:您确保将来有利于有需要的人...

答案 1 :(得分:0)

我希望你的助教或教授提供更多的帮助。这是一个可能对您有所帮助的基本计划。这不是你提交答案的手段,而是你学习的方法(注意我对bash知之甚少)。我确实希望您借此机会解决bash问题而不是撰写报告。

首先将其复制到nano中。删除行号。那将是一个痛苦的过程。将文件另存为test.sh.从命令行类型:chmod 755 test.sh。要运行该程序,请键入./test.sh

我会在线写评论。

 1  #!/bin/bash
 2
 3  # create a function that will have echo statements
 4  # to print instructions on the screen
 5  function print_menu() {
 6  echo Please choose on of the following option:
 7  echo a - Create a file
 8  echo b - Create a directory
 9  echo c - Delete a file
10  echo d - Delete a directory
11  echo e - Create a user
12  echo f - Delete a user
13  echo q - quit
14  }

此时我们编写了一个可以通过使用print_menu调用它来重用的函数。当你进一步阅读时,你会看到。

15
16  # keep on looping until user presses q
17  while true; do
18

我们想要显示打印菜单,然后询问用户问题,根据答案我们会做一些工作,然后重复显示打印菜单和yada yada的循环。

19      # print the menu
20      print_menu
21      # ask user to choose an option
22      read -p "Enter your choice: " choice

到目前为止,我们已向用户显示菜单并要求输入选项。理想情况下,我们应该从用户输入等方面进行错误检查,但我们会在其他时间覆盖这些概念。 choice是一个变量(或存储桶),用于存储输入的任何用户。如果用户键入a,则choice存储区将包含a。要从该存储桶中检索信息,我们使用$choice

23      # based on user's choice, do variety of things
24      case $choice in
25          a)  read -p "Name of file to create: " file
26              touch $file
27              echo Created file $file
28              echo ----
29              ;;

查看http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html以获取有关如何在bash中编写case语句的更多信息。我们使用此case语句来评估存储桶$choice的内容。

然后我们告诉bash,如果选择是a,请询问另一个关于要创建的文件的问题。将答案存储在名为file的存储桶中。并使用$file

使用称为文件的存储桶中包含的值

touch命令创建文件。然后我们向用户提供一些反馈,然后键入;;

来关闭该案例
30
31          b)  read -p "Name of directory to create: " dir
32              mkdir -p $dir
33              echo Created directory $dir
34              echo ---
35              ;;
36
37          c)  read -p "Name of file to delete: " file
38              rm $file
39              echo Deleted file $file
40              echo ---
41              ;;
42
43          d)  read -p "Name of directory to delete: " dir
44              rm -rf $dir
45              echo Deleted directory $dir
46              ;;

以上所有内容与选择a相同。您可以添加更多代码来处理其他情况。

47
48          q)  echo Goodbye
49              break;;

如果用户输入q,我们会在break循环中提供反馈并while

50
51          *)  echo Nothing selected. Try again
52              ;;

如果用户没有给出上述选择,我们只会提供反馈并返回到while循环的顶部。

53      esac
54      # sleep 3 seconds to give user time to digest the output
55      sleep 3
56  done
57

我们睡了3秒然后回到while循环的顶部。

希望这会有所帮助。