Shell Scripting (bash)

Shell Scripting :

Logical And operator :

echo -e "enter your age /c"
read age
if [ "$age" -gt 18 ] && [ "$age" -lt 60 ]
then
echo "eligible for driving license"
else
echo " not eligible for driving license"
fi


Logical OR operator :

echo -e "enter your age /c"
read age
if [ "&age" -eq 18 ] || [ "$age" -eq 25 ]
then
echo "valid age"
else
echo "not the valid age"
fi


Performing arithmetic operations :

#!/bin/bash
# we should not use echo 1+1 .. it will take it as a string.
num1=20
num2=40
echo $(( num1 + num2 ))
echo $(( num1 - num2 ))
echo $(( num1 * num2 ))
echo $(( num1 / num2 ))
echo $(( num1 % num2 ))



The case statement :

case expression in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
    ...
esac

#!/bin/bash
vehicle=$1
case $vehicle in
    "car" )
        echo "Rent of car is 100 dolloars" ;;
    "van" )
        echo "Rent of van is 80 dollars" ;;
    "bicycle" )
        echo "Rent of bicycle is 5 dollars" ;;
    "truck" )
        echo "Rent of Truck is 150 Dollars" ;;
    * )
        echo "not an authorized vehicle" ;;
esac

# if we provide * it meets all other arguments which we have given apart from the above.
# * is matches remaining all arguments.


#!/bin/bash
echo -e "Enter some character : /c"
read char
case $char in
    [a-z] )
        echo "user provided $chara to z smaller case" ;;
    [A-Z] )
        echo "user provided $char A to Z upper case" ;;
    [0-9] )
        echo "user provided $char 0 - 9 numbers" ;;
    ? )
        echo "user provided $char special character" ;;
    * )
        echo "unknown input" ;;
esac

# if the user enter any character from a to z or A to Z or 0 - 9 numbers


While Loops :

#!/bin/bash
while [ condition ]
do
    command1
    command2
    command3
done
 
What is Shell Script and why it is required ?
Ans: A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish the task, we can add these all commands together in a text file (Shell Script) to complete daily routine task.

What is the default login shell and how to change default login shell for a specific user ?
Ans: In Linux like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change default shell using the “chsh” command . Example is shown below :
# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh

What are the different type of variables used in a shell Script ?
Ans: In a shell script we can use two types of variables :
  • System defined variables
  • User defined variables
System defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command.
User defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo $<Name_of_Variable>


How to redirect both standard output and standard error to the same location ?
Ans: There two method to redirect std output and std error to the same location:
Method:1 2>&1 (# ls /usr/share/doc > out.txt 2>&1 )
Method:2 &> (# ls /usr/share/doc &> out.txt )



What is the Syntax of “nested if statement” in shell scripting ?
Ans : Basic Syntax is shown below :
if [ Condition ]
then
command1
command2
…..
else
if [ condition ]
then
command1
command2
….
else
command1
command2
…..
fi
fi



What is the use of “$?” sign in shell script ?
Ans:While writing a shell script , if you want to check whether previous command is executed successfully or not , then we can use “$?” with if statement to check the exit status of previous command. Basic example is shown below :
root@localhost:~# ls /usr/bin/shar
/usr/bin/shar
root@localhost:~# echo $?
0
If exit status is 0 , then command is executed successfully
root@localhost:~# ls /usr/bin/share
ls: cannot access /usr/bin/share: No such file or directory
root@localhost:~# echo $?
2
If the exit status is other than 0, then we can say command is not executed successfully.



How to compare numbers in Linux shell Scripting ?
Ans: test command is used to compare numbers in if-then statement. Example is shown below :
#!/bin/bash
x=10
y=20
if [ $x -gt $y ]
then
echo “x is greater than y”
else
echo “y is greater than x”
fi




What is the use of break command ?
Ans: The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops.


What is the use of continue command in shell scripting ?
Ans The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where error has occurred but we still want to execute the next commands of the loop.


Tell me the Syntax of “Case statement” in Linux shell scripting ?
Ans: The basic syntax is shown below :
case word in
value1)
command1
command2
…..
last_command
!!
value2)
command1
command2
……
last_command
;;
esac


What is the basic syntax of while loop in shell scripting ?
Ans: Like the for loop, the while loop repeats its block of commands a number of times. Unlike the for loop, however, the while loop iterates until its while condition is no longer true. The basic syntax is :
while [ test_condition ]
do
commands…
done


How to make a shell script executable ?
Ans: Using the chmod command we can make a shell script executable. Example is shown below :
# chmod a+x myscript.sh



What is the use of “#!/bin/bash” ?
Ans: #!/bin/bash is the first of a shell script , known as shebang , where # symbol is called hash and ‘!’ is called as bang. It shows that command to be executed via /bin/bash.



What is the syntax of for loop in shell script ?
Ans: Basic Syntax of for loop is given below :
How compare the strings in shell script ?
Ans: test command is used to compare the text strings. The test command compares text strings by comparing each character in each string.
for variables in list_of_items
do
command1
command2
….
last_command
done


How to debug a shell script ?
Ans: A shell script can be debug if we execute the script with ‘-x’ option ( sh -x myscript.sh). Another way to debug a shell script is by using ‘-nv’ option ( sh -nv myscript.sh).




What are the Special Variables set by Bourne shell for command line arguments ?
Ans: The following table lists the special variables set by the Bourne shell for command line arguments .
Special Variables
Holds
$0
Name of the Script from the command line
$1
First Command-line argument
$2
Second Command-line argument
…..
…….
$9
Ninth Command line argument
$#
Number of Command line arguments
$*
All Command-line arguments, separated with spaces



How to test files in a shell script ?
Ans: test command is used to perform different test on the files. Basic test are listed below :
Test
Usage
-d file_name
Returns true if the file exists and is a directory
-e file_name
Returns true if the file exists
-f file_name
Returns true if the file exists and is a regular file
-r file_name
Returns true if the file exists and have read permissions
-s file_name
Returns true if the file exists and is not empty
-w file_name
Returns true if the file exists and have write permissions
-x file_name
Returns true if the file exists and have execute permissions



How to put comments in your shell script ?
Ans: Comments are the messages to yourself and for other users that describe what a script is supposed to do and how its works.To put comments in your script, start each comment line with a hash sign (#) . Example is shown below :
#!/bin/bash
# This is a command
echo “I am logged in as $USER”



How to get input from the terminal for shell script ?
Ans: ‘read’ command reads in data from the terminal (using keyboard). The read command takes in whatever the user types and places the text into the variable you name. Example is shown below :
# vi /tmp/test.sh
#!/bin/bash
echo ‘Please enter your name’
read name
echo “My Name is $name”
# ./test.sh
Please enter your name
LinuxTechi
My Name is LinuxTechi


How to unset or de-assign variables ?
Ans: ‘unset’ command is used to de-assign or unset a variable. Syntax is shown below :
# unset <Name_of_Variable>



How to perform arithmetic operation ?
Ans: There are two ways to perform arithmetic operations :
1. Using expr command (# expr 5 + 2 )
2. using a dollar sign and square brackets ( $[ operation ] ) Example : test=$[16 + 4] ; test=$[16 + 4]



Basic Syntax of do-while statement ?
Ans: The do-while statement is similar to the while statement but performs the statements before checking the condition statement. The following is the format for the do-while statement:
do
{
statements
} while (condition)



How to define functions in shell scripting ?
Ans: A function is simply a block of of code with a name. When we give a name to a block of code, we can then call that name in our script, and that block will be executed. Example is shown below :
$ diskusage () { df -h ; }



How to use bc (bash calculator) in a shell script ?
Ans: Use the below Syntax to use bc in shell script.
variable=`echo “options; expression” | bc`



















Comments

Popular posts from this blog

Python reference Interview questions

[SOLVED]* Please wait for the system Event Notification service

Rebuild the initial ramdisk image in Red Hat Enterprise Linux