Shell Exercises Explained

Source: Internet
Author: User

Write a script that calculates all the odd and all the even numbers within 100 and displays them separately
#! /bin/bash
Sum1=0
For i in ' SEQ 1 2 100 '
Do
sum1=$[$sum 1+ $i]
Done
echo $sum 1

Sum2=0
For j in ' SEQ 2 2 100 '
Do
sum2=$[$sum $j]
Done
Echo $sum 2


Write a script that generates 10 random numbers using random and finds the maximum and minimum values

For i in ' SEQ 1 10 '
Do
n[$i]= $RANDOM
echo ${n[$i]} >> tmp.txt
Done
A= ' Sort-n tmp.txt|head-n1 '
b= ' Sort-n tmp.txt|tail-n1 '
echo "A=" $a
echo "b=" $b


======
If [-Z $hdir] Determines whether it is empty
If [-N $hdir] Determines whether it is not empty
======
Write a script to check if a normal user's home directory exists, such as Apache user presence, output exit
, if not present, output not exist


read-p "Please input a user name;" Username
hdir1= ' awk-f: ' $1== ' ' $username ' ' '/etc/passwd '
if [-Z $hdir 1]
Then
echo "The user you are not exist"
Exit
fi
hdir= ' echo $hdir 1| awk-f: ' {print $6} '
if [-Z $hdir]
Then
echo "The user you are not exist"
Exit
fi
if [-Z $hdir]
Then
echo "$username ' s homedir is $hdir and exist"
Else
echo "$username ' s homedir is $hdirnot exist"
fi


Write a script to complete the following tasks
1, add 5 users, Usr1,,,,user5
2, each user's password is the same as the user name, and requires that the add password does not display the passwd command execution result information
3, each user added after the completion, to show that the user has added a successful

For i in {1..5}
Do
if! Grep-q "^user$i:"/etc/passwd
Then
Useradd usr "$i"
Echo-e "user$i\nuser$i\n" |passwd user "$i" >/dev/null 2>&1
echo "Add user Done"
Else
echo "The user user$i exist"
Continue
Fi
Done

The number range is added to 100-999.
If the value exceeds plus or minus 50 will prompt: Too big, too small
If the value is a hint between plus and minus 50, it's close.

m=$[$RANDOM%900+100]
While :
Do
read-p "Input a number:" N
#n1 =$[$m +50]
#n2 =$[$m -50]

if ["$n"-GT "$m"]
Then
echo "Bingger"
fi
if ["$n"-lt "$m"]
Then
echo "smaller"
fi
if ["$n"-eq "$m"]
Then
echo "Right"
Exit
fi
Done

Write a script that calculates all positive integers within 100 that can be divisible by 3
Sum=0
For i in {1..100}
Do
n=$[$i%3]
If ["$n"-eq 0]
Then
sum=$[$sum + $i]
Fi
Done
Echo $sum

Write a script, after the execution, print a line prompt "Please input a number", ask the user to enter a value, then print out the value, and then ask the user to enter a value again, know user input end stop

While:
Do
Read-p "Please input a number:" N
If ["$n" = = "End"]
Then
Exit
Fi
M= ' echo $n |sed ' s/[0-9]//g '
If [-N ' $m '] is not empty
Then
echo "It ' s not number,please input a number."
Else
Echo $n
Fi
Done

Output warning when the root directory is on a hard drive with a usage greater than 85%

DF |awk ' $6== '/' '

DF |awk ' $6== '/' ' |awk-f ' [% ' "]+ ' ' {print $} '
AWK supports multiple separators, here is the% and space, + indicates that percent spaces can have multiple

echo "a b%c%d E" |awk-f "[%" "]" ' {Print $3,$5} '

DF |awk ' $6== '/' ' |awk-f ' [% ' "]+ ' ' {print $6} '

Shell Exercises Explained

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.