简体   繁体   English

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

[英]Bash Script add, delete, users, directories and files

hi guys I need to create a bash scrip for my class and I haven't taken any classes that teach how to this yet. 嗨,大家好,我需要为我的课堂创建一个bash脚本,而且我还没有参加过任何教该如何做的课程。 The bash script menu has to have the following: 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

I've try to do search on my own but since I've never done it before I don't know where to start. 我尝试自己进行搜索,但由于我从未做过搜索,所以不知道从哪里开始。 I know how to execute it at least and i know how to do all of this with the commands but I have no idea how to create the bash script file. 我至少知道如何执行它,并且我知道如何使用命令来完成所有这些操作,但是我不知道如何创建bash脚本文件。 I would appreciate the help and some explanation so I can at least try to understand this. 我会很感激您的帮助和一些解释,因此至少可以尝试理解这一点。 My class is using the newest version of fedora if that helps. 如果有帮助,我的课使用的是最新版本的fedora。 Also the professor wants us to use nano for it 教授还希望我们使用纳米

What's wrong with a 10-15 page report? 10-15页的报告有什么问题? Research and writing included, your looking at an hour-per-page. 包括研究和写作在内,您每页看一个小时。 Piece of cake... Or, 小菜一碟...

You owe a lotta beer: 您欠了很多啤酒:

#!/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

Note: you make sure you return the favor to someone in need in the future... 注意:请确保将来将您的帮助退给有需要的人...

I wish your teaching assistants or professors provided a little more assistance. 希望您的助教或教授能提供更多帮助。 This is a rudimentary program that might help you. 这是一个基本程序,可能会对您有所帮助。 This is not a means for you to submit as an answer, but for you to learn (note that I know little about bash). 这不是您提交答案的方法,而是您学习的方法(请注意,我对bash知之甚少)。 I do like that you took the opportunity to tackle the bash problem instead of writing a report. 我确实喜欢您借此机会解决了bash问题,而不是撰写报告。

Start with copying this into nano. 首先将其复制到nano中。 Remove the line numbers though. 虽然删除行号。 That'll be a painful process. 那将是一个痛苦的过程。 Save the file as test.sh. 将文件另存为test.sh。 From command line type: chmod 755 test.sh . 从命令行键入: chmod 755 test.sh To run the program, type ./test.sh 要运行该程序,请键入./test.sh

I'll write comments inline. 我将内联发表评论。

 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  }

At this point we have written a function that can be reused by calling it using print_menu . 至此,我们已经编写了一个函数,可以通过使用print_menu调用该函数来重用。 You'll see as you read further. 您会在进一步阅读时看到。

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

We want to show the print menu, then ask the user questions, based on the answer we'll do some work and then repeat the loop of showing print menu and yada yada. 我们想要显示打印菜单,然后向用户提问,根据答案,我们将做一些工作,然后重复显示打印菜单和yada yada的循环。

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

So far we have shown the user the menu and asked to enter a choice. 到目前为止,我们已经向用户显示了菜单并要求输入一个选项。 We should ideally do error checking from user input and such, but we'll cover those concepts some other time. 理想情况下,我们应该从用户输入等中进行错误检查,但我们将在其他时间介绍这些概念。 choice is a variable (or bucket) that will store whatever user typed. choice是一个变量(或存储桶),将存储用户键入的任何内容。 If user typed a , then choice bucket will contain a . 如果用户键入a ,然后choice斗将包含a To retrieve information from that bucket, we use $choice 要从该存储桶中检索信息,我们使用$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              ;;

Check out http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html for more information on how to write case statements in bash. 请查看http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html ,以获取有关如何在bash中编写case语句的更多信息。 We use this case statement to evaluate contents of bucket $choice . 我们使用此case语句评估存储桶$choice内容。

Then we tell bash, if the choice was a , ask another question about file to create. 然后我们告诉bash,如果选择的是a ,请询问有关要创建的文件的另一个问题。 Store the answer in bucket called file. 将答案存储在称为文件的存储桶中。 And use the value contained in bucket called file by using $file 并使用$file使用存储在名为file的存储桶中的值

touch command creates the file. touch命令创建文件。 Then we provide some feedback to the user and then close out that case by typing ;; 然后,我们向用户提供一些反馈,然后通过键入;;结束该案例。

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              ;;

All the above is just the same as choice a . 以上所有与选择a相同。 You can add more code to handle rest of the cases. 您可以添加更多代码来处理其余情况。

47
48          q)  echo Goodbye
49              break;;

If the user typed q , we give feedback and we break out from the while loop. 如果用户键入q ,我们给予反馈,我们break从出来while循环。

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

If none of the above choices were given by the user, we will just provide feedback and go back to the top of the while loop. 如果用户没有上述选择,我们将只提供反馈并返回while循环的顶部。

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

We sleep for 3 seconds and then go back to the top of while loop. 我们睡眠3秒钟,然后返回while循环的顶部。

Hope this helps out. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM