Some shell script exercises write a script: 1. set the FILE value of the variable to etcpasswd2, send Hello to each user in etcpasswd, and display the shell of the other user, such as Hello, root, yourshell: binbash3, count the total number of users! Binbash some shell script exercises to write a script: 1. set the FILE value of the variable to/etc/passwd2, send hello to each user in/etc/passwd, and display the shell of the other user, for example: Hello, root, your shell:/bin/bash3, count the total number of users #! /Bin/bashFILE = "/etc/passwd" num = 'WC-l <$ FILE 'echo "User count: $ num" for I in 'seq 1 $ num '; do Username = 'head-$ I $ FILE | tail-1 | cut-d:-f1 'Shell = 'head-$ I $ FILE | tail-1 | cut-d: -f7 'echo "Hello, $ Username, your shell: $ Shell" done: 1. add 10 users, user1 to user10, and the password is the same as the user name; count the total idnumbers of the 10 users ;#! /Bin/bashfor I in 'seq 1 10 '; do useradd user $ I echo "user $ I" | passwd -- stdin user $ I userid = 'grep "user $ I"/etc/passwd | cut-d: -f3 'sumid = $ [$ sumid + $ userid] doneecho "id count: $ sumid" write a script, show all users with bash by default and/sbin/nologin by default respectively on the current system, and count the total number of users under various shell types. The displayed result is like BASH, 3 users, they are: root, redhat, gentoo NOLOGIN, 2 users, they are: bin, ftp #! /Bin/bashBASH_C = 'grep'/bin/Bash'/etc/passwd | wc-l 'nologin _ C = 'grep'/sbin/nologin'/etc/passwd | wc -l 'echo "BASH, $ BASH_C 'users', they are: "for I in 'seq 1 $ BASH_C '; do BASH_N = "$ BASH_N 'grep'/bin/Bash'/etc/passwd | head-$ I | tail-1 | cut-d:-F1 ', "doneecho $ BASH_Necho" NOLOGIN, $ NOLOGIN_C 'users', they are: "for I in 'seq 1 $ NOLOGIN_C '; do NOLOGIN_N = "$ NOLOGIN_N 'grep'/sbin/nologin'/etc/passwd | he Ad-$ I | tail-1 | cut-d:-f1 ', "doneecho $ NOLOGIN_N write a script: 1) display a menu to the User: d | D) show disk usages. m | M) show memory usages. s | S) show swap usages. *) quit.2) displays the corresponding content after the user gives the option. 3) after the user selects the option, the user does not exit after the information is displayed. Instead, the user selects the option and displays the corresponding content again; in addition to using quit ;#! /Bin/bash # create function ss () {echo '----------------------------- Options ---------------------------- 'echo 'd | d) show disk usages. 'echo'm | M) show memory usages. 'echo's | s) show swap usages. '*) quit. # enter an option parameter Sread-p 'Please input your select:'s} # Call the function ss # Create a while loop. if the option is dmsDMS, enter the case. if not, then the while loop ends directly while [[$ S = [dmsDMS] | $ S = 'Quit' | $ S = *]; docase $ S in [dD]) df-lh; [mM]) free-m-l | grep-v '^ Swap '; [sS]) free-m-l | grep' ^ Swap '; quit) exit; *) echo' ------------------------- Warning! ---------------------------- 'Echo 'Please input your correct select, eg: [dDmMsS | quit] '; use of esacsdone option #! /Bin/bashhelp () {echo "-m memory" echo "-s swap" echo "-d disk space" echo "-q quit"} helpwhile getopts msdqh selectdo case $ select in m) free-m-l | grep-v '^ Swap'; s) free-m-l | grep '^ Swap'; d) df-lh; q) exit; h) help; *) echo "Please select help options-h"; esacdone
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.