function of each line of following script :-- 1) The following shell script illu
ID: 3702452 • Letter: F
Question
function of each line of following script :--
1) The following shell script illustrates the use of the read and echo commands. Enter it using vi the file should be named mycat. Set the appropriate permissions and test it. Using man pages, Google, your textbook, and class handouts comment each line of the script provided below to explain how the script functions.
! /bin/sh -f
read s
while [ -n "$s" ]
do
echo $s
read s
done
2) The following shell script is used to demonstrate how a script can be written that is able to interact with a user. Enter it using vi the file should be named mydialog. Set the appropriate permissions and test it. Using man pages, Google, your textbook, and class handouts comment each line of the script provided below to explain how the script functions.
#! /bin/sh
echo -n "Enter you name (first last): "
read first last
echo "Data read:" $first $last
cat << ENDDATA
Hi, $first ${last}.
Mr. $last, bye-bye!
ENDDATA
3) The following script can be used to create users. Using man pages, Google, your textbook, and class handouts comment each line of the script provided below to explain how the script functions. There is no need to run this script.
#! /bin/sh -fx
read username
while [ -n "$username" ]
do
name=`echo $username | awk '{print $1}'`
useradd -m -g users -d /home/users/$name -s /bin/bash $name
passwd $name
read username
done
4) The following script can be used to delete users. Set the appropriate permissions and test it. Using man pages, Google, your textbook, and in class handouts comment
each line of the script provided below to explain how the script functions. There is no need to run this script.
#!/bin/bash
read username
while [ -n "$username" ]
do
name=`echo $username | awk '{print $1}'`
userdel -r $name
read username
done
5) This shell script demonstrates the use of the shift instruction. Enter it using vi the file should be named myargs. Set the appropriate permissions and test it. . Using man pages, Google, your textbook, and class handouts comment each line of the script provided below to explain how the script functions.
! /bin/sh -x
echo $#: $@
set abc def ghi
while [ $# -gt 0 ]
do
echo $#: $@
shift
done
6) The following shell script is used to demonstrate how a script can be written that uses procedures called by a driver section of the script. Enter it using vi the file should be named myproc. Set the appropriate permissions and test it. Using man pages, Google, your textbook, and class handouts comment each line of the script provided below to explain how the script functions.
main()
{
proc1()
{
echo ... In proc
return 0
}
echo Calling proc
proc1
echo Back to caller
return 0
}
main
Explanation / Answer
NOTE: I have completed your assignment. Please check and let me know if you find any issues. I will revert back within 24 hours. thanks for your patience.
Code-1:
Unix Terminal> cat first.sh
#!/bin/sh -f
# reads a line
read s
# iterates the loop until user enters a empty line or just hit enter without typing anything
while [ -n "$s" ]
do
# displays the given line
echo $s
# reads next line
read s
done
Code output snapshot:
Unix Terminal> bash first.sh
this is first line
this is first line
this is second line
this is second line
Unix Terminal>
Code-2:
Unix Terminal> cat second.sh
#!/bin/sh
# displays a message asking user to enter first and last name
echo -n "Enter you name (first last): "
# reads first and last name
read first last
# displays message that first and last names are read
echo "Data read:" $first $last
# using hereby document command greets user
cat << ENDDATA
Hi, $first ${last}.
Mr. $last, bye-bye!
ENDDATA
Code outptu snapshot:
Unix Terminal> bash second.sh
Enter you name (first last): vishal rathore
Data read: vishal rathore
Hi, vishal rathore.
Mr. rathore, bye-bye!
Unix Terminal>
Code-3:
NOTE: I have not run the script in my system. The reason being it creates an user in my computer. But i have written the appropriate comments required for the assignment.
#!/bin/sh -fx
# reads username from the user
read username
# until user provides an empty line keep reading the username
while [ -n "$username" ]
do
# extracts the first name part from given username
name=`echo $username | awk '{print $1}'`
# creates an user in the operating system
useradd -m -g users -d /home/users/$name -s /bin/bash $name
# sets the password
passwd $name
# reads the next username
read username
done
Code-4:
NOTE: I have not run the script in my system. The reason being it deletes users in my computer. But i have written the appropriate comments required for the assignment.
#!/bin/bash
# reads username from the user
read username
# until user provides an empty line keep reading the username
while [ -n "$username" ]
do
# extracts the first name part from given username
name=`echo $username | awk '{print $1}'`
# deletes the user
userdel -r $name
# reads the next username
read username
done
Code-5:
Unix Terminal> cat five.sh
#!/bin/sh -x
# displays if there are any command line arguments passed
echo $#: $@
# sets the command line parameter variable i.e. $@ with values abc, def and ghi
set abc def ghi
# repeats the loop until command line arguments are zero
while [ $# -gt 0 ]
do
# displays the command line arguments
echo $#: $@
# removes or extracts one command line argument each time
shift
done
Code output snapshot:
Unix Terminal> bash five.sh arg1 arg2 arg3
3: arg1 arg2 arg3
3: abc def ghi
2: def ghi
1: ghi
Unix Terminal>
Code-6:
Unix Terminal> cat six.sh
# function main defined here
main()
{
# nested function proc1 defined here
proc1()
{
# displays ... In proc when this function called
echo ... In proc
return 0
}
# displays message calling proc
echo Calling proc
# calling proc1 nested function
proc1
# displaying back to caller message once proc1 function is completed
echo Back to caller
# returning the 0 status to caller which is main
return 0
}
# calling main function
main
Code output snapshot:
Unix Terminal> bash six.sh
Calling proc
... In proc
Back to caller
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.