Question

In: Computer Science

Linux Scripting Exercises Step 1: Using the nano text editor, enter the code below and save it as welcome.sh, change its permission to executable, run the script, and answer the questions below ( 1.1-1.5):

 

Linux Scripting Exercises

Step 1: Using the nano text editor, enter the code below and save it as welcome.sh, change its permission to executable, run the script, and answer the questions below ( 1.1-1.5):

#!/bin/bash

#

echo ”Hello $user”

echo ”Today is `date` ”

echo ”I feel $argv[1]” today

echo ”The following users are logged in: `users`”

Explain and number your answers for 1.1-1.5 below:

1.1. $user does in line 3

1.2. `date` does in line 4

1.3. $argv[1] does in line 5

1.4. `users` does in line 6

1.5. What would happen if you tried to execute the script as follows?

./welcome.sh

Step 2: Using the nano text editor, enter the code shown below, save it as word.sh, change its permission to executable, run the script, and answer the questions below ( 2.1-2.4):

#!/bin/bash

#

set a1 = One two three

set a2 =”One two three”

set a3 = ’One two three’

set a4 = One two three)

set a5 = $a2

set a6 = $a2 $a3

set a7 = $a2 $a3

#

echo $a1 $#a1

echo $a2 $#a2

echo $a3 $#a3

echo $a4 $#a4

echo $a5 $#a5

echo $a6 $#a6

echo $a7 $#a7

echo $a7[2]

Explain and answer 2.1-2.4 below:

2.1. What does echo $a1 $#a1 do?

2.2. Which shell variable has the greatest number of words?

2.3. Which shell variables have one word and include spaces in the word?

2.4. What would happen if you tried to execute the script as follows?

./word.sh

Solutions

Expert Solution

Solutions for Step 1 :

Ouputs after running the first script:

”Hello ”
”Today is Sat Oct 3 08:45:59 IST 2020 ”
”I feel [1]” today
”The following users are logged in: amity amity”

1.1 Only prints Hello as in the sh file no value is initialized for user varriable.Hence $user prints nothing

1.2 date prints the current system time

1.3 Prints I feel [1] today as argv prints the command line arguements and as no arguements are provided nothing gets printed

1.4 The logged in user gets printed

1.5 The shell script gets executed

Solutions for Step 2:

Outputs after the running the script:

2a1
2a2
2a3
2a4
2a5
2a6
2a7
[2]

2.1 $a1 $#a1 prints 2a1 as $# prints the number of arguments passed and a1 gets printed as a normal string hence the output 2a1

2.2 a3

2.3 a3

2.4 The script gets executed and gives the output as mentioned above.


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT