Simple script writing

Source: Internet
Author: User

Simple script writing 1. Exercise: write a script www.2cto.com to determine whether there are users in the current system. The default shell is bash. If so, the number of such users is displayed. Otherwise, no such user is displayed; grep "bash $"/etc/passwd &>/dev/nullRETVAL =$? If [$ RETVAL-eq 0]; then if grep "bash $"/etc/passwd &>/dev/null; then Method 2: www.2cto.com #! /Bin/bash # GESHU = 'grep "bash"/etc/passwd | wc-l' if [$ GESHU-eq 0]; then echo "mei you zhe lei yong hu" else echo "you {$ GESHU}" fi prompt: "Reference" the execution result of a command. Use command reference. For example: RESAULTS = 'wc-l/etc/passwd | cut-d:-f1 '; the execution status result of a command must be directly executed and cannot be referenced. For example: the id command in the if id user1 sentence must not be enclosed by quotation marks. To assign the execution result of a command to a variable, use the command reference, for example, USERID = 'id-u user1'; if you want to save the execution status result of a command and use it as a condition for determining whether the command is successfully executed, you must first execute this command, then reference its status result, such as id-u user1RETVAL =$? This statement cannot be written as RETVAL = 'id-u user1'. 2. Exercise: write a script to determine whether the current system has a user. The default shell is bash. If yes, the user name is displayed. Otherwise, no such user is displayed. Method 1 :#! /Bin/bash # GESHU = 'grep "bash"/etc/passwd | wc-l 'yonghu = 'grep "bash"/etc/passwd | cut-d: -f1 | tail-1 'if [$ GESHU-eq 0]; then echo "mei you zhe lei yong hu" else echo "you zhe yang de yong hu li ru: {$ YONGHU} "fi 3. Exercise: write a script to specify a file, for example,/etc/inittab to check whether there are blank lines in the file. If yes, the number of blank lines is displayed; otherwise, no blank lines are displayed. Method 1 :#! /Bin/bashHANGSHU = 'grep' ^ $ '/etc/inittab | wc-l' if [$ HANGSHU-eq 0]; then echo "mei you kong bai hang" else echo "you {$ HANGSHU} hang kong bai hang" fi 4. Exercise: write a script to give a user, if the UID is the same as the GID, the user is displayed as "good guy"; otherwise, the user is displayed as "bad guy ". If! Grep "" #! /Bin/bashUSERNAME = user1USERID = 'id-u $ username' GROUPID = 'id-g $ username' if [$ USERID-eq $ GROUPID]; thenecho "Good guy. "elseecho" Bad guy. "fi Method 1 :#! /Bin/bashUSERNAME = useryangyaruUSERUID = 'grep "^ USERNAME \ B"/etc/passwd | cut-d: -f3 'usergid = 'grep "^ USERNAME \ B"/etc/passwd | cut-d:-f4 'if [$ USERUID-eq $ USERGID]; thenecho "Good guy. "elseecho" Bad guy. "fi further requirement: Do not use the id command to obtain its id number; 5. Exercise: round: write a script to give a user and get the password warning period; then, judge whether the last time the user modified the password was earlier than the warning period. The prompt is: arithmetic operation method $ [$ A-$ B]: result of subtracting the value of variable A from the value of variable B. If the value is smaller than, "Warning" is displayed. Otherwise, "OK" is displayed ". Method 1 :#! /Bin/bashJINGGAO = 'grep "^ yyr"/etc/shadow | cut-d: -f6 'miaoshu = 'date + % s' TIANSHU = $ [$ MIAOSHU/86400] XIUGAI = 'grep "^ yyr"/etc/shadow | cut-d: -f3 'shiyong = $ [$ TIANSHU-$ XIUGAI] ZUICHA = 'grep "^ yyr"/etc/shadow | cut-d: -f5 'shengyu = $ [$ ZUICHA-$ SHIYONG] if [$ SHENGYU-gt $ JINGGAO]; thenecho "OK" elseecho "Warning" fi 6. exercises: write a script to determine whether the total number of historical commands in the command history is greater than 1000. If the number is greater than, "Some command will gone. "; otherwise," OK "is displayed ". Method 1 :#! /Bin/bashHIS = 'History | tail-l | cut-d ''-f2' if [$ HIS-lt 1000]; thenecho" OK. "elseecho" Some command will gone. "fi

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.