Exercise 21: Writing Scripts - Part 2
I. Prepare the environment
II. Accept input from user, if and case statement
1. Login to the CentOS server with student
2. Write a shell script to create a new user based on the information supplied by user
as the following scenario:
The script will ask user to input the following information:
- User name
- Home directory
- Default shell
After supplied above information, ask user to confirm the user creation, just allow
user to input 1 characters Y for yes or N for no. The message as follows:
“Are you sure to create user with the above information (Y/N)?”
If the answer is yes, show the completed message:
“Your user is created successfully with the following information:
User name: <user name that user supplied>
Home directory: <home dir that user supplied>
Default shell: <shell that user supplied>”
Note: you do not need to create the user actually
If the answer is no, just show “The user creation is canceled by user.”
3. Write a script to do the basic math equations with the scenario as follows:
- The script get 2 number from user
- Compare the 2 number
- Calculate the result of equation: <bigger number>/<smaller number>. Using bc
with scale=2.
4. Write a script to list a number of users as requested. The scenario is as follows:
The script asks user to in put the number of users that he/she want to display
“How many users do you want to display”
Based on the number that user supply, display the first # users appropriate (# is
the number supplied by user).
If the number is 1 then the output as follows:
“The first users of the system is:
<the first line from /etc/passwd>”
If the number is more than 1 then the output as follows:
This document is created by Nguyen Hoang [Link].e4w@[Link] Page 1
“The first <number supplied by user> users of the system are:
<the first # line from /etc/passwd>”
If the number is more than 10 then the output as follows:
“The number that you input is out of range.”
This document is created by Nguyen Hoang [Link].e4w@[Link] Page 2
Exercise Instructions
I. Prepare the environment
II. Accept input from user, if and case statement
1. Login to the CentOS server with student
2. Write a shell script to create a new user based on the information supplied by user
as the following scenario:
The script will ask user to input the following information:
- User name
- Home directory
- Default shell
After supplied above information, ask user to confirm the user creation, just allow
user to input 1 characters Y for yes or N for no. The message as follows:
“Are you sure to create user with the above information (Y/N)?”
If the answer is yes, show the completed message:
“Your user is created successfully with the following information:
User name: <user name that user supplied>
Home directory: <home dir that user supplied>
Default shell: <shell that user supplied>”
Note: you do not need to create the user actually
If the answer is no, just show “The user creation is canceled by user.”
#!/bin/bash
echo "Please supply some information to create the user you want."
read -p "User name:" name
read -p "Home directory:" home
read -p "Default shell:" shell
read -n 1 -p "Are you sure to create user with the above information (Y/N)?"
confirm
if [ $confirm = "Y" ] || [ $confirm = "y" ]
then
This document is created by Nguyen Hoang [Link].e4w@[Link] Page 3
echo -e "\n"
echo "Your user is created successfully with the following information:"
echo "User name: $name"
echo "Home directory: $home"
echo "Default shell: $shell"
else
echo "The user creation is canceled by user."
fi
3. Write a script to do the basic math equations with the scenario as follows:
- The script get 2 number from user
- Compare the 2 number
- Calculate the result of equation: <bigger number>/<smaller number>. Using bc
with scale=2.
#!/bin/bash
read -p "Input the first number: " x
read -p "Input the second number: " y
if [ $x -gt $y ]
then
echo "The first number is greater than the second"
echo "The result of dividing the greater number by the less one is $(echo
"scale=2;$x/$y"|bc)"
elif [ $x -eq $y ]
then
echo "The two numbers are equal"
echo "The result of dividing one number to other is 1"
else
echo "The second number is greater than the first"
This document is created by Nguyen Hoang [Link].e4w@[Link] Page 4
echo "The result of dividing the greater number by the less one is $(echo
"scale=2;$y/$x"|bc)"
fi
4. Write a script to list a number of users as requested. The scenario is as follows:
The script asks user to in put the number of users that he/she want to display
“How many users do you want to display”
Based on the number that user supply, display the first # users appropriate (# is
the number supplied by user).
If the number is 1 then the output as follows:
“The first users of the system is:
<the first line from /etc/passwd>”
If the number is more than 1 then the output as follows:
“The first <number supplied by user> users of the system are:
<the first # line from /etc/passwd>”
If the number is more than 10 then the output as follows:
“The number that you input is out of range.”
#!/bin/bash
read -p "How many users would you like to display " number
case $number in
1) echo -e "The first user in the system is:\n$(head -1 /etc/passwd)" ;;
[2-9]) echo -e "The first $number users in the system is:\n$(head -$number
/etc/passwd)";;
*) echo "The number that you input is out of range";;
esac
This document is created by Nguyen Hoang [Link].e4w@[Link] Page 5