In: Computer Science
Write bash shell scripts for the following problems and submit your answers a single.
A ) Write a linux bash shell script that reads your first and lastname as arguments and print them as shown below. In my example, I name the script as printname.sh. In the example, the two arguments follow the shell scriptname. Submit the script and output print screen.
# sh ./printname.sh Abdullah Konak My first name is Abdullah My surname is Konak
B ) Write a linux bash script that will return the largest of the two numbers that are entered as the argument of the script as shown in the example given below. Return your script and a printscreen from your computer.
# sh ./larger.sh 7 5 7 is larger of 5 and 7.
C) Extend the script in part B to any number of numbers entered as arguments as the example given below.
# sh ./largest.sh 7 5 3 8 9 10 10 is the largest of numbers entered.
BASH SHELL SCRIPT FOR A: (printname.sh)
#!/bin/bash
### Command arguments can be accessed as
echo "My first name is" $1
echo "My surname is" $2
SCREENSHOT FOR OUTPUT:
BASH SHELL SCRIPT FOR B:(larger.sh)
#!/bin/bash
# comparing two numbers
if [ $1 -gt $2 ]
then
# printing $1 as largest number
echo "$1 is larger of $1 and $2"
else
# printing $2 as largest number
echo "$2 is larger of $1 and $2"
fi
SCREENSHOT FOR OUTPUT:
BASH SHELL SCRIPT FOR C:(largest.sh)
#!/bin/bash
# getting the array of comman line arguments
args=("$@")
# variable for counter
i=0
# variable to store the maximum number
max=0
# looping to find the largest number in the array of command
line arguments
while [ $i -lt $# ]
do
# setting the first number as the largest number
if [ $i -eq 0 ] #set first number as max
then
max=${args[i]}
else
# comparing other numbers in the
array, updating the max variable
if [ ${args[i]} -gt $max ]
then
max=${args[i]}
fi
fi
#increment the counter i by 1
i=$((i + 1))
done
echo "$max is the largest of numbers entered."
SCREENSHOT FOR OUTPUT: