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?
There two methods 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?
Basic Syntax is shown below
if [ Condition ]
then
command1
command2
…..
else
if [ condition ]
then
command1
command2
….
else
command1
command2
…..
fi
fi
Tell Me The Syntax Of “case Statement” In Linux Shell Scripting?
The basic syntax is shown below
case word in
value1)
command1
command2
…..
last_command
!!
value2)
command1
command2
……
last_command
;;
esac
How To Put Comments In Your Shell Script?
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?
‘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
What Is The Use Of Continue Command In Shell Scripting?
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.
What Is Shell Script And Why It Is Required?
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?
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
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?
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:$ disk usage () { df -h ; }
How To Use Bc (bash Calculator) In A Shell Script?
Use the below Syntax to use bc in shell script.
Variable=`echo “options; expression” | bc`.
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.
Post a Comment