Learn Shell Script Part 1
shell scripting part 1
-----------------------------------------------------------------------------------------------------------------------------
The shell environment helps users to interact with and access core function off the operating system
The term scripting is the more relevant as context of the script.
in this series, we are dealing with bash(bourne again shell)which is the default shell environment for most gnu/Linux system
in the shell environment $ represent the normal user and # represent the root/administrator/superuser
the shell script is a text file that typically begins with a shebang
which is
#!/bin/bash
shebang is a line on which #! is prefixed to the interpreter path /bin/bash is the interpreter command path for the base.
the script can be run with the file name
the text which starts with # is a comment
ex: $ bash script.sh # Assuming script is in the current directory
or
$ bash /home/path/script.sh #using full path of script.sh
to give permission to run the script for all users
> $ chmod a+x script.sh
The kernel will read the first line and see that the shebang is #!/bin/bash. It will identify
/bin/bash and execute the script internally as
$ /bin/bash/script.sh
When a shell is started, it initially executes a set of commands to define various settings such
as prompt text, colors, and much more. This set of commands are read from a shell script at
~/.bashrc (or ~/.bash_profile for login shells) located in the home directory of the user.
The Bash shell also maintains a history of commands run by the user. It is available in the
~/.bash_history file
In Bash, each command or command sequence is delimited by using a semicolon or a new
line. For example:
$ cmd1 ; cmd2
This is equivalent to:
$ cmd1
$ cmd2
1) printing in the terminal
echo is the basic command for printing in the terminal.
echo puts a newline at the end of every echo invocation by default:
$ echo "Welcome to Bash"
Welcome to Bash
Simply, using double-quoted text with the echo command prints the text in the terminal.
Similarly, text without double-quotes also gives the same output:
$ echo Welcome to Bash
Welcome to Bash
Another way to do the same task is by using single quotes:
$ echo 'text in quotes
These methods may look similar, but some of them have a specific purpose and side effects
too. Consider the following command
$ echo "cannot include exclamation - ! within double quotes
This will return the following output:
bash: !: event not found an error
Hence, if you want to print special characters such as ! either do not use them within double
quotes or escape them with a special escape character (\) prefixed with it, like so:
$ echo Hello world !
Or:
$ echo 'Hello world !'
Or:
$ echo "Hello world \!" #Escape character \ prefixed
$ printf "Hello world"
printf takes quoted text or arguments delimited by spaces. We can use formatted strings
with printf. We can specify string width, left or right alignment, and so on. By default,
printf does not have newline as in the echo command. We have to specify a newline
when required, as shown in the following script:
#!/bin/bash
#Filename: printf.sh
printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sara 67.56
printf "%-5s %-10s %-4.2f\n" 2 Jam 99.99
printf "%-5s %-10s %-4.2f\n" 3 Jefry 78.59
We will receive the formatted output:
No Name Mark
1 Sara 67.56
2 Jam 99.99
3 Jefry 78.59
how its work
%s, %c, %d, and %f are format substitution characters for which an argument can be placed
after the quoted format string.
%-5s can be described as a string substitution with left alignment (- represents left
alignment) with width equal to 5.
Escaping newline in echo
By default, echo has a newline appended at the end of its output text. This can be avoided
by using the -n flag. echo can also accept escape sequences in double-quoted strings as an
argument. When using escape sequences, use echo as echo -e "string containing
escape sequences". For example:
echo -e "1\t2\t3"
1 2 3
printing the color output
Colors are represented by color codes, some examples being, reset = 0, black = 30, red = 31,
green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, and white = 37
To print a colored text, enter the following command:
echo -e "\e[1;31m This is red text \e[0m"
Here, \e[1;31m is the escape string that sets the color to red and \e[0m resets the color
back. Replace 31 with the required color code.
For a colored background, reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44,
magenta = 45, cyan = 46, and white=47, are the color codes that are commonly used.
To print a colored background, enter the following command:
Comments
Post a Comment