NEW
Font size
WorksheetsScripting Unit 7 and 8 Practical Revision
Total questions: 31
Worksheet time: 2hrs 48mins
How do you print the variable value. (for example: num=5)
@num
$num
&num
num
Rohan wants to accept an input data from user and print it on screen. How does he do it? Choose the correct option.
echo Enter data
read data
echo $data
echo Enter data
input data
echo $data
echo Enter data
Input data
echo @data
echo
read
echo $data
Avery is learning how to write scripts for automating tasks on her computer. She comes across a line at the beginning of her script that seems important. Which one of the statements is true for the first line argument in her script?
First line starts with #! and this tells the compiler which interpreter to use to execute the script. and the line is #!/bin/bash
First line starts with #! and this tells the compiler which interpreter to use to execute the script. and the line is #!/bash/bash
First line starts with !# and this tells the compiler which interpreter to use to execute the script. and the line is #!/bin/bash
First line starts with #! and this tells the compiler which interpreter to use to execute the script. and the line is #!/bash/bin
Grace is writing a script for her project. What should be the first line of her script?
#!/bin/bosh
#!/bin/bash
!#/bin/bash
$#/bin/bash
If you run the script as follows
./scriptName.sh Arjun Isla
What code will you be writing in your script to print these two arguments Arjun and Isla. Select correct code.
Using %1 %2
Using #1 #2
echo $1 $2
Using $0 $1
In a Unix systems class, Luna learned that you should have execute permission on a file to run the script.
True
False
I don't know
We never learned
If you select this option. I will conduct special classes for you.
What permission should you configure after you created the script and before you run the script.
chmod u+x scriptName.sh
chmod +x
chmod +wr scriptName.sh
None of the above
The SHELL environment variable can tell you what shell program you are using on your system?
True
False
Oliver wants to print the numbers from 1 to 5 on the screen. Which of the following commands should he use?
for i in {1..5}
do
echo $i
for i in {1..5}
echo $i
done
for i in [1..5]
do
echo $i
done
for i in {1..5}
do
echo $i
done
In a classroom, Aria is organizing a group project and wants to iterate through a list of tasks. She uses the following for loop:
for i in task_list
do
execute_task
done
What is i called in the given for loop?
loop variable
variable
environment variable
special variable
Jackson is organizing a countdown event where he needs to count down from 10 to 1. Select the correct script that he should use for the countdown.
count=10
while [ $count -gt 0 ]
do
echo $count
((count--))
done
count=10
while [ $count -le 0 ]
do
echo $count
((count==))
done
count=10
while [ $count -eq 10 ]
do
echo $count
((count--))
done
count=10
while [ $count -gt 10 ]
do
echo $count
((count--))
done
Select the script which will print bash file name, first argument and the second argument ?
./scriptName.sh Zoe Ethan
#!/bin/bash
echo $0
echo $1
echo $2
#!/bin/bash
echo #0
echo $1
echo $2
#!/bin/bash
echo @0
echo @1
echo @3
#!/bin/bash
echo $9
echo $4
echo $3
Select the script which will count number of users online.
No_of_Users=$(who)
echo $No_of_Users
No_of_Users=$(who | wc -l)
echo $No_of_Users
No_of_Users=$(who | wc -w)
echo $No_of_Users
No_of_Users=$(who | wc -c)
echo $No_of_Users
Identify the error in the script given below. Select the correct line number.
count=0
while [ count -eq 5 ]
do
echo $count
((count++))
done
Line 1
Line 2
Line 3
Line 4
Line 5
Open rsyslog.conf file on server and select the correct log file path for
#The authpriv file has restricted access.
authpriv.* ______________________
/etc/log/secure
/var/secure
/var/log/secure
All of the above
Open rsyslog.conf on server and select the correct path for
# Save news errors of level crit and higher in a special file.
uucp,news.crit _______________________
/boot/log/spooler
/var/boot/spooler
/var/log/spooler
/var/log/mail
Select correct case statement script which print "Dog" if Dog is select and "Cat" if Cat is selected. Complete the script by selecting correct missing statement,
Complete the script so it prints "No pet selected" for any other pet animal.
echo "Enter your pet animal"
read pet
case $pet in
Dog) echo "Dog";;
Cat) echo "Cat";;
*)
esac
echo "No pet selected"
}
echo "No pet selected"
;;
echo "No pet selected"
None of the above
Select the correct script, which print file exist if imran.txt exist and it print file does not exist if imran.txt does not exist.
if [ -f imran.txt ]
then
echo "imran.txt File exists"
else
fi
if [ -f imran.txt ]
then
echo "imran.txt File exists"
else "imran.txt file does not exist
fi
if [ -f imran.txt ]
then
echo "imran.txt File exists"
else echo "imran.txt file does not exist"
fi
None of the above
Select the correct script and you can use Linux server to verify your answer.
Script should print "File exist and it is a directory" if directory exist else print "File does not exist"
if [ -e file.txt ]
then
echo "File exist and it is a directory"
else "File does not exist
fi
if [ -f file.txt ]
then
echo "File exist and it is a directory"
else "File does not exist
f
if [ -d file.txt ]
then
echo "File exist and it is a directory"
else echo "File does not exist
fi
if [ -s file.txt ]
echo "File exist and it is a directory"
else "File does not exist
fi
Select the script which check the FILE.txt size and print "File is not empty" else "File is empty"
if [ -s file.txt ]
then
echo "File is not empty"
else "File is empty"
fi
if [ -d file.txt ]
then
echo "File is not empty"
else "File is empty"
fi
if [ -e file.txt ]
then
echo "File is not empty"
else "File is empty"
fi
if [ $# file.txt ]
then
echo "File is not empty"
else "File is empty"
Select the script which print the file name (script name)
echo $(basename $0)
echo (basename $0)
echo $(basename $1)
echo (basename 0)
Select the script which print all arguments on screen.
echo $@
echo $!
echo $%
echo &*
Select the script which prints all the arguments as shown below and it uses a for loop to do so.
for loopvar in $@
do
echo $loop
done
for loopvar in $*
do
echo $loopvar
enod
for loopvar in $*
do
echo $loopvar
done
for loopvar in $*
do
echo loopvar
done
Refer to Lab10 and understand the question:
Select the script that will find WARNINGS in dmesg file and send an email to mecorrect@gmail.com with the WARNING messages only.
#!/bin/bash
result=$(grep "WARNING" /var/log/dmesg)
if [ $? -eq 0 ]
then
echo $result >> ~/warning.out
mail -s "Lab 10 : Name, id, Class" mecorrect@gmail.com < ~/warning.out
fi
#!/bin/bash
if [ $? -eq 0 ]
result=$(grep "WARNING" /var/log/dmesg)
then
echo $result >> ~/warning.out
mail -s "Lab 10 : Name, id, Class" mecorrect@gmail.com < ~/warning.out
fi
#!/bin/bash
if [ $? -eq 0 ]
then
echo $result >> ~/warning.out
mail -s "Lab 10 : Name, id, Class"
result=$(grep "WARNING" /var/log/dmesg)mecorrect@gmail.com < ~/warning.out
fi
#!/bin/bash
if [ $? -eq 0 ]
result=$(grep "WARNING" /var/log/dmesg)mecorrect@gmail.com < ~/warning.out
fithen
echo $result >> ~/warning.out
mail -s "Lab 10 : Name, id, Class"
Refer to your lab 8 and the activities and answer the following question.
Select the script which correct to print "I love Linux" 5 times.
#!/bin/bash
for((i=0; i<6; i++))
do
echo "I love Unix!"
done
#!/bin/bash
for((i=0; i<5; i++))
do
echo "I love Unix!"
done
#!bin/bash
for((i=0; i<5; i++))
do
echo "I love Unix!"
done
#!/bin/bash
for((i=0; i<5; i++))
do
echo "I love Unix!"
enod
Select the script that print a "The Number is positive" only if the number stored in the variable is greater than 0. Which line contains an error?
1 #!/bin/bash
2 num=5
3 if [$num -gt 0]
4 then
5 echo "Number is positive"
6 fi
1
2
5
3
During a class project, Abigail needs to count the number of lines in a file named animal.txt. What command should she use to accomplish this task? You can use a Linux server to verify your answer.
cat animal.txt | wc -c
wc -l animal.txt
wc -l < animal.txt
None of the above
Daniel is working on a project in his Linux server and needs to count the number of lines in all text files in his current directory.
Select the script which prints the number of lines in all text files in his current directory.
#!/bin/bash
for i in $(ls *.txt)
do
linecount=$(wc -c < $i)
echo "No of Lines: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(wc -l < $i)
echo "No of Lines: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(wc -u < $i)
echo "No of Lines: $linecount"
done
#!/bin/bash
for i in `textfiles`
do
linecount=$(wc -l < $i)
echo "No of Lines: $linecount"
done
Select the script that will print Number of lines of all text files in your current directory.
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(wc -l < $i)
echo "No of Lines: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(wc -l $i)
echo "No of Lines: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(wc -l $i)
echo "No of Lines: $linecount"
#!/bin/bash
for i in `ls *.txt`
linecount=$(wc -l $i)
echo "No of Lines: $linecount"
done
Below given script print number of lines that contains word "poly" on screen. Identify the lines contain error and select the line number from the given options.
line 1 - #!/bin/bash
line 2 - for $i in `ls *.txt`
line 3 - od
line 4 - linecount=$(grep poly $i | wc -l)
line 5 - echo "No of Lines contain word poly is: $linecount"
line 6 - done
line 2
line 3
line 4
line 4
line 2
line 5
line 2
line 6
Select the correct script that can print number of lines that contains word "poly" in all text files in your current directory, on screen.
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(grep poly $i | wc -l)
echo "No of Lines contain word poly is: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(grep poly $i | wc -c)
echo "No of Lines contain word poly is: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(cat poly $i | wc -l)
echo "No of Lines contain word poly is: $linecount"
done
#!/bin/bash
for i in `ls *.txt`
do
linecount=$(grep poly $i | wc -l)
echo "No of Lines contain word poly is:"
done
