Shell Script Example Collection (exercise summary)

Source: Internet
Author: User
Tags create directory

Practice one: Write a script
1. Set the value of the variable file to/etc/passwd
2. Say hello to each user in/etc/passwd and say what the ID is
Shape: (Hint: line= ' wc-l/etc/passwd | Cut-d ""-f1 ")
Hello,root,your UID is 0.
3. Count the number of users
Answer one: #!/bin/bash
File= "/etc/passwd"
lines= ' Wc-l $file | Cut-d ""-f1 "
For I in ' seq 1 $LINES ';d o
Userid= ' head-$I $file | Tail-1 |cut-d:-f3 '
Username= ' head-$I $file | Tail-1 |cut-d:-f1 '
echo "Hello $username, your UID is $userid"
Done
echo "There are $LINES users"
Answer two: #!/bin/bash
file=/etc/passwd
Let num=0
For I in ' cat $file ';d o
Username= ' echo ' $I | cut-d:-f1 '
Userid= ' echo ' $I | cut-d:-f3 '
echo "Hello, $username, your UID is $userid"
num=$[$num +1]
Done
echo "There are $num users"
Exercise two: Write a script
1. Switch working directory to/var
2. Greet each file or subdirectory in the/var directory in turn, as follows:
(Hint: for file in/var/*; or for file in ' Ls/var ';)
Hello,log
3. Statistics of multiple files in/var directory, and displayed
Answer: #!/bin/bash
Cd/var
Let num=0
For I in ' ls/var/* ';d o
echo "Hello $I"
num=$[$num +1]
Done
echo "The number of files is $num"
Exercise three: Write a script
1. Set the value of the variable file to/etc/passwd
2. Use the loop to read the 2,4,6,10,13,15 line of the file/etc/passwd and display its contents
3. Save these lines to the/tmp/mypasswd file
Answer: #!/bin/bash
File= "/etc/passwd"
For I in 2 4 6 15;do
EXEC 3>/tmp/mypasswd
Line= ' head-$I $file | Tail-1 '
echo "$line"
echo "$line" >&3
EXEC 3>&-
Done Exercise four: Write a script to pass two integers to the script, let the script calculate and display the two integers separately and, difference, product, quotient
The answer is as follows: Vim test.sh
#!/bin/bash
echo "First Number $" (indicates the output of the second digit) echo "second number $" (indicates the output of the third digit)
echo "$ (($1+$2))" (sum of outputs two)
echo "$[$1-$2" (output two number difference)
echo "$[$1*$2]" (output two number of product) echo "$[$1/$2" (output two number of quotient)
: Wq (means save and Exit VI Editor)
chmod +x test.sh (Permissions for test.sh execution)
./test.sh 2 3 (pass two parameters and execute script job one: Write a script: 1. Create a directory/tmp/scripts 2. Switch the working directory to this directory 3. Copy the/ETC/PAM.D directory to the current target and rename it to test 4. Change the current directory's test and the contents of its files and subdirectories to Redhat 5. Change the permissions of other users of the files in test and its subdirectories to no permission answer:
#!/bin/bash
Mkdir-v/tmp/scripts
Cd/tmp/scripts
Cp-r/ETC/PAM.D./test
Chown-r Redhat./test
Chmod-r o=---./test Job Two: Write a script 1. Displays the current system date and time, and then creates the directory/tmp/lstest 2. Switch working directory to/tmp/lstest 3. Create Directory A1d,b56e,6test 4. Create an empty file xy,x2y,732 5. Lists the files or directories in the current directory that begin with A,x or 6. Lists the file or directory answer that starts with a letter, followed by an arbitrary number, and followed by any length character in the current directory:
#!/bin/bash
Date
Mkdir-pv/tmp/lstest
Cd/tmp/lstest
mkdir a1d b56e 6test
Touch XY x2y 732
LS [ax6]*
LS [[: alpha:]][[:d igit:]]*
Job Three: Write a script
Add 10 users user1 to User10, but require that only the user does not exist to add
Answer:
#!/bin/bash
For I in ' seq 1 ';d o
cut-d:-f1/etc/passwd |grep "user$i" 2>>/tmp/etc.err | | Useradd user$i
Done
Job Four: Write a script
Test whether all hosts between 192.168.0.151 to 192.168.0.254 are online through the ping command
If online, "IP is Up" appears
If the line is not present, "IP is Down"
Answer: #!/bin/bash
For I in ' SEQ 151 254 ';d O
Ping-c1-w1 192.168.0. $I &>/dev/null && echo "192.168.0. $I is up" | | echo "192.168.0. $I is Down"
Done

Shell Script Example Collection (exercise summary)

Related Article

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.