1)
A shell script "var1.sh" contains the following lines:
echo $MY_NAME
MY_NAME="Tyson Fury"
echo $MY_NAME
On the command line we type :
[amittal@hills Reviews]$ MY_NAME="Mike Tyson"
[amittal@hills Reviews]$ ./var1.sh
Tyson Fury
[amittal@hills Reviews]$
[amittal@hills Reviews]$ echo $MY_NAME
Mike Tyson
If we run the script file as using the "source" we get the below behavior.
[amittal@hills Reviews]$ MY_NAME="Mike Tyson"
[amittal@hills Reviews]$ source ./var1.sh
Mike Tyson
Tyson Fury
[amittal@hills Reviews]$ echo $MY_NAME
Tyson Fury
Explain why we get the output in the above 2 cases.
2)
Assume the following is typed on the unix console.
echo "This is a sentence" > data.txt
echo "This is a sentence" >> data.txt
echo "This is a sentence" | grep "These" >> data.txt
cat data.txt
What will be the output ? Explain the output .
3)
Assume we have the 4 following files in a folder :
"f" , "fa" , "faa" , "faab"
What is the output of the following command:
ls f*b
What is the output of the following command:
ls fa*
What is the output of the following command:
ls f?a
4)
What is the output of the following ?
[amittal@hills Reviews]$ echo "This is a sentence" > data.txt
[amittal@hills Reviews]$ cat data.txt | grep "THIS" | grep "This"
5)
What is the output of the following commands typed on the console.
[amittal@hills awk]$ var1="Learn sed and"
[amittal@hills awk]$ echo var1
[amittal@hills awk]$ echo $var1
[amittal@hills awk]$ echo $var1awk
[amittal@hills awk]$ echo ${var1} awk
6)
What is the output of the following ?
var1="This is a sentence"
echo $var1
echo "$var1"
echo '$var1'
7)
What do the below statements print ? There is a space between ")" and the period in the string ") ." .
echo "123 abc" | sed -r 's/^([0-9]+) .*/\1/'
echo "123 abc" | sed -r 's/([^0-9]+) .*/\1/'
8)
What does the following print ?
echo "Teest" | egrep "Teee?st"
9)
echo "first second third" | awk '{print $2, " " , $1}'
10)
A shell script contains the below:
echo "1234" | awk 'BEGIN { print "Testing for division by 2." }
{
if ( $1 % 2 == 0 )
{
print "Number is even."
}
else
{
print "Number is odd."
}
}
END { print "End of Test" }'
What is the output ?
1)
When we run a script with the command "./var1.sh" the main shell creates a subshell and the script runs in that subshell. The subshell does not have access to variables in the main shell ( except environment variables ) and conversely the main shell cannot see the subshell's variables when the script ends. If we run a script with the source command "source ./var1.sh" then a subshell is not created but rather all the commands are executed in the main shell. The script can see all the variables that were defined in the main shell and the main shell can see the modified variables from the shell script.
echo $MY_NAME
MY_NAME="Tyson Fury"
echo $MY_NAME
On the command line we type :
[amittal@hills Reviews]$ MY_NAME="Mike Tyson"
[amittal@hills Reviews]$ ./var1.sh
Tyson Fury
[amittal@hills Reviews]$
[amittal@hills Reviews]$ echo $MY_NAME
Mike Tyson
When the shell runs "./var1.sh" . the script executes the line:
echo $MY_NAME
However because the script is run in a subshell it does not see the value that the main shell assigned:
MY_NAME="Mike Tyson"
So the "echo $MY_NAME" prints blank line. The the script assigns "Tyson Fury" to the variable and that gets printed out. When we come back to the main shell and print:
echo $MY_NAME
then "Mike Tyson" . When we come back to the main shell the sub shell modifications to the variable are lost.
Now the second case:
[amittal@hills Reviews]$ MY_NAME="Mike Tyson"
[amittal@hills Reviews]$ source ./var1.sh
Mike Tyson
Tyson Fury
[amittal@hills Reviews]$ echo $MY_NAME
Tyson Fury
When we use "source" we are not running the shell script in a sub shell but in the main shell. It's as if we took the commands from the shell script and typed them at the command line.
[amittal@hills Reviews]$ MY_NAME="Mike Tyson"
amittal@hills Reviews]$ source ./var1.sh
Mike Tyson
Tyson Fury
[amittal@hills Reviews]$ echo $MY_NAME
Tyson Fury
Now when the script does "echo $MY_NAME" the value that was assigned "Mike Tyson" is printed out. Then "Tyson Fury" is assigned to the variable and that gets printed out. Finally when we do another "echo $MY_NAME" from the command line the value changed by the script is retained and we get "Tyson Fury" printed out.
2)
echo "This is a sentence" > data.txt
echo "This is a sentence" >> data.txt
echo "This is a sentence" | grep "These" >> data.txt
cat data.txt
The first command with ">" writes "This is a sentence" to the file. If the file "data.txt" existed before then it will be overwritten. Next the ">>" causes echo to append the same phrase to the file. In the next line the we have another echo but the grep in the pipeline does not find a match so a blank line is appended to "data.txt" with the output:
This is a sentence
This is a sentence
3)
Assume we have the 4 following files in a folder :
"f" , "fa" , "faa" , "faab"
What is the output of the following command:
ls f*b
faab
What is the output of the following command:
ls fa*
fa faa faab
What is the output of the following command:
ls f?a
faa
4)
What is the output of the following ?
[amittal@hills Reviews]$ echo "This is a sentence" > data.txt
[amittal@hills Reviews]$ cat data.txt | grep "THIS" | grep "This"
Since the second command (grep "THIS") produces nothing and no input is given to the third command in the pipeline we don't get any output.
5)
What is the output of the following commands typed on the console.
[amittal@hills awk]$ var1="Learn sed and"
[amittal@hills awk]$ echo var1
[amittal@hills awk]$ echo $var1
[amittal@hills awk]$ echo $var1awk
[amittal@hills awk]$ echo ${var1} awk
echo var1
A variable without the "$" sign is just a string literal
var1
echo $var1
Learn sed and
echo $var1awk
This is interpreted as the value of the variable "var1awk" that has not been assigned any value so nothing is printed.
echo ${var1}awk
The curly bracket around the "var1" lets the shell know that we want the value of the variable and "awk" is printed as a string literal.
Learn sed andawk
6)
What is the output of the following ?
var1="This is a sentence"
echo $var1
echo "$var1"
echo '$var1'
In the first the value of "$var1" is obtained but then the shell gobbles up the spaces printing.
This is a sentence
In the second echo statement we are using double quotes so the value of the variable is printed out but now
the spaces are preserved.
[amittal@hills files]$ echo "$var1"
This is a sentence
The single quote prints the string as it is without any modification.
[amittal@hills files]$ echo '$var1'
$var1
7)
What do the below statements print ?
echo "123 abc" | sed -r 's/^([0-9]+) .*/\1/'
echo "123 abc" | sed -r 's/([^0-9]+) .*/\1/'
echo "123 abc" | sed -r 's/^([0-9]+) .*/\1/'
The carat "^" means start from the beginning of the sentence and the parentheses will do a pattern match. It will find a pattern match for the number "123" and the space after that will match the space after the ")" parentheses. So the pattern matches the whole line and that whole line will be replaced by what's in parentheses.
Output:
123
The second sed statement has the carat inside the square brackets.
echo "123 abc" | sed -r 's/([^0-9]+) .*/\1/'
Carat inside the brackets means the any other character than the digits will be a match. So the number does not match. the literal "abc" might have matched but there is a space in our expression and no space after "abc" so we don't have a match. Since there is no match and sed is not going to modify our original string. We have the output:
123 abc
In order to understand the above let's look at the following examples:
echo "123 abc" | sed -r 's/([^0-9]+).*/\1/'
In the above there is no space between the parathenses ")" and the dot. So we do have a match for the "abc" and not for the number so we replace "abc" with the match "abc" and thus there is no change in the output.
[student@milhouse ~]$ echo "123 abc" | sed -r 's/([^0-9]+).*/\1/'
123 abc
Let us put "number" as the replacement pattern
[student@milhouse ~]$ echo "123 abc" | sed -r 's/^([0-9]+) .*/Number/'
Number
[student@milhouse ~]$ echo "123 abc" | sed -r 's/([^0-9]+) .*/Number/'
123 abc
[student@milhouse ~]$ echo "123 abc" | sed -r 's/([^0-9]+).*/ Number/'
123 Number
8)
What does the following print ?
echo "Teest" | egrep "Teee?st"
The "?" question mark means the preceding character can occur 1 or 0 times. So the second egrep finds a pattern and outputs the original string "Teest" to the last command. Pattern matching says that the e preceding the question mark is repeated 0 times.
9)
echo "first second third" | awk '{print $2, " " , $1}'
The "$2" represents the second field. In awk the default separator for fields is the space. The "$1" represents the first field. So the output is :
second first
10)
echo "1234" | awk 'BEGIN { print "Testing for division by 2." }
{
if ( $1 % 2 == 0 )
{
print "Number is even."
}
else
{
print "Number is odd."
}
}
END { print "End of Test" }'
Testing for division by 2.
Number is even.
End of Test
Awk will first execute the "BEGIN" command and then run the input line by line with the commands in the middle and then finally execute the "END" statement. We only have 1 line that is the input "1234" . That is tested by the line
if ( $1 % 2 == 0 )
to check if the modulus is 0 and if it is we print the line "Number is even." . So the final result is:
1)
The "who" command lists the users who are logged in. Assume for the following exercise "who" returns the following.
[amittal@hills final]$ who
rseth pts/0 2018-12-02 09:12 (171.68.250.206)
amittal pts/1 2018-12-03 07:39 (64.125.46.186)
amittal pts/2 2018-11-16 08:19 (64.125.46.186)
abrick pts/4 2018-12-02 16:57 (70.36.145.75)
amittal pts/5 2018-12-02 13:05 (75.18.249.132)
The file "args.sh" contains the following code:
who | grep $1
The following command is run from the command line.
./args.sh "amittal pts"
This results in:
[amittal@hills final]$ ./arg2.sh "amittal pts"
grep: pts: No such file or directory
Explain the error and how it could be corrected.
2)
File args.sh contains the following.
echo $#
echo $0
echo $1
echo $2
echo "$1"
echo '$1'
What is the output when the following command is run:
[amittal@hills final]$ ./args.sh first second third
3)
File "args.sh" contains the following
echo "Number of arguments:" $#
cat $1 $2 > fourth
paste $1 $2 > $3
File "first" contains the following line
first
And the file "second" contains the following line:
second
What is the out output when the command is run as:
./arg2.sh first second third
What are the contents of the files "third" and "fourth" ?
4)
File "loop1.sh" contains the following:
for var1 in $*
do
echo $var1
done
Explain the output when the following command is run:
./loop1.sh first second third
5)
File "loop1.sh" contains the following text:
for (( i1=1; i1<=5; i1++ ))
do
for (( i2=3; (i1+i2)<=5; i2++ ))
do
echo -n " " $i2
done
done
What is the output when the shell file is run ?
6)
File "decision1.sh" has the following text:
str1="Fred and ed go to school."
echo $str1 | grep "ed"
echo $?
echo $str1 | grep "Ed"
echo $?
echo $str1 | grep "Mary"
echo $?
What is the output ?
7)
The file "decision.sh" contains the following:
var1=10
var2="class"
echo "Step 1"
if [ $var1 -gt 9 ]
then
echo "Step 2"
if [ $var1 -ne 9 ]
then
echo "Step 3"
else
if [ $var2 = "class" ]
then
echo "Step 4"
else
echo "Step 5"
fi
fi
echo "Step 6"
else
echo "Step 7"
fi
echo "Step 8"
What is the output ?
8)
What does the below print ?
echo "Wed Aug 28 19:34:01" | sed -r 's/ ([0-9]+):([0-9]+):([0-9]+)/ \1 Hours \2 Minutes \3 Seconds/'
9)
What does the below print ?
echo "Wed Aug 28 19:34:01" | awk 'BEGIN { FS=":" }{ print $1, "Hours" ,$2, "Minutes", $3, "Seconds" }'
10)
The file "shell1.sh" contains the following. What is the output ?
var1="This"
var2="fun"
var3="$var1 class was"
var4="so"
var5="$var3 $var4 much $var2"
echo $var5
1)
who | grep $1
The following command is run from the command line.
./args.sh "amittal pts"
The error happens because "amittal pts" is passed to the shell script as a single field . The $1 is replaced with this field.
who | grep amittal pts
grep searches for the word "amittal" in the file "pts". It does not find the file "pts" and gives an error. What we need is to tell grep that it is a single field we need to search by enclosing the $1 in quotes.
who | grep "$1"
We can understand this better with another example.
var1="This is a sentence."
if we do
echo $var1
we get the following:
This is a sentence.
Shell replaced the value of $var1 and then worked on it some more. It ate up the spaces and 4 fields are printed out. What we need is to tell Shell not to work on it any more.
We can do that with double quotes.
echo "$var1"
2)
echo $# //Represents the number of arguments
echo $0 //Represents the name of the file being run
echo $1 //First field
echo $2 //Second Field
echo "$1" //First field Quotes preserves the spaces but our argument "first" is a simple word
echo '$1' //Single quotes raw string
What is the output when the following command is run:
[amittal@hills final]$ ./args.sh first second third
3
./args.sh
first
second
first
$1
3)
File "args.sh" contains the following
echo "Number of arguments:" $#
cat $1 $2 > fourth
paste $1 $2 > $3
File "first" contains the following line
first
And the file "second" contains the following line:
second
What is the out output when the command is run as:
./args.sh first second third
What are the contents of the files "third" and "fourth" ?
The first command is:
cat $1 $2 > fourth
This will concatenate the contents of the files "first" and "second" as the $1 refers to the first field and $2 refers to the second field. So the file named "fourth" will contain:
first
second
The paste command is:
paste $1 $2 > $3
This will take the lines from "first" and "second" and join them into a single file and store that in a file named "third" .
The contents of the file named "third" will be:
first second
4)
File "loop1.sh" contains the following:
for var1 in $*
do
echo $var1
done
Explain the output when the following command is run:
./loop1.sh first second third
The "$*" means all the arguments. So the for loop evaluates to:
for var1 in first second third
do
echo $var1
done
Output:
first
second
third
5)
File "loop1.sh" contains the following text:
for (( i1=1; i1<=5; i1++ ))
do
for (( i2=3; (i1+i2)<=5; i2++ ))
do
echo -n " " $i2
done
done
What is the output when the shell file is run ?
The outer loop runs from i1 to
1 2 3 4 5
and for each of those i1 values we run an inner loop for i2 . The i2 runs from 3 to to 5 .
For i1 = 1
(3+1) <= 5
i2 which is 3 gets printed out.
( 4+1 ) <=5
value of i2 is 4 and that gets printed out:
now i2 is 5 and since ( 5+1) >= 5 we do not enter the inner for loop.
Now i1 = 2 in the outer for loop
i2 is 3 and (2 + 3 ) <= 5 and
i2 the 3 is printed out
Now i2is 4 and ( 2+4) is not less than 5
For rest of the iterations the sum of i1 and i2 is going to be greater than 5 so nothing will be printed out.
Answer is
3 4 3
6)
File "decision1.sh" has the following text:
str1="Fred and ed go to school."
echo $str1 | grep "ed"
echo $?
echo $str1 | grep "Ed"
echo $?
echo $str1 | grep "Mary"
echo $?
What is the output ?
The "$?" prints the exit status . For grep if there is a match then the exit status is 0 otherwise the status is 1 . Grep only matches the first grep of the word "ed"
So we get
0
1
1
7)
var1=10
var2="class"
echo "Step 1" --> PRINTED
if [ $var1 -gt 9 ]
then
echo "Step 2" --> PRINTED
if [ $var1 -ne 9 ]
then
echo "Step 3" --> PRINTED
else
if [ $var2 = "class" ]
then
echo "Step 4"
else
echo "Step 5"
fi
fi
echo "Step 6" --> PRINTED
else
echo "Step 7"
fi
echo "Step 8" --> PRINTED
10 is greater than 9 so the if condition is satisfied and we enter the "if" condition. and "Step 2" is printed out. The next if condition is "not equal to 9" so we enter the if condition and "Step 3" gets printed out. Then if statement ends and "Step 6" is printed out ( if [$var1 -ne 9 ] ends ) .
Output:
Step 1
Step 2
Step 3
Step 6
Step 8
8)
What does the below print ?
echo "Wed Aug 28 19:34:01" | sed -r 's/ ([0-9]+):([0-9]+):([0-9]+)/ \1 Hours \2 Minutes \3 Seconds/'
Wed Aug 28 19 Hours 34 Minutes 01 Seconds
9)
What does the below print ?
echo "Wed Aug 28 19:34:01" | awk 'BEGIN { FS=":" }{ print $1, "Hours" ,$2, "Minutes", $3, "Seconds" }'
FS stands for Field Separator and there are 3 fields
$1 Wed Aug 28 19
$2 34
$3 01
Wed Aug 28 19 Hours 34 Minutes 01 Seconds
10)
The file "shell1.sh" contains the following. What is the output ?
var1="This"
var2="fun"
var3="$var1 class was" -----------var3 now contains "This class was"
var4="so"
var5="$var3 $var4 much $var2" var5 contains This class was so much fun
echo $var5
This class was so much fun
is printed out.