Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
='''Chapter 11: Bash Scripting'''= =='''Hack 84. Execution Sequence of .bash_* files'''== What is the sequence in which the following files are executed? o /etc/profile o ~/.bash_profile o ~/.bashrc o ~/.bash_login o ~/.profile o ~/.bash_logout '''Execution sequence for interactive login shell''' Following pseudo code explains the sequence of execution of these files. execute /etc/profile IF ~/.bash_profile exists THEN execute ~/.bash_profile ELSE IF ~/.bash_login exist THEN execute ~/.bash_login ELSE IF ~/.profile exist THEN execute ~/.profile END IF END IF END IF When you logout of the interactive shell, following is the sequence of execution: IF ~/.bash_logout exists THEN execute ~/.bash_logout END IF Please note that /etc/bashrc is executed by ~/.bashrc as shown below: <nowiki># </nowiki>cat ~/.bashrc if <nowiki>[ </nowiki>-f /etc/bashrc ]; then . /etc/bashrc Fi '''Execution sequence for interactive non-login shell''' While launching a non-login interactive shell, following is the sequence of execution: IF ~/.bashrc exists THEN execute ~/.bashrc END IF '''Note: '''When a non-interactive shell starts up, it looks for ENV environment''' '''variable, and executes the file-name value mentioned in the ENV variable. =='''Hack 85. How to generate random number in bash shell'''== Use the $RANDOM bash built-in function to generate random number between 0 – 32767 as shown below. $ echo $RANDOM 22543 $ echo $RANDOM 25387 $ echo $RANDOM 647 =='''Hack 86. Debug a shell script'''== To debug a shell script use set –xv inside the shell script at the top. '''Shell script with no debug command:''' $ cat filesize.sh #!/bin/bash for filesize in $(ls -l . | grep "^-" | awk '{print $5}') do let totalsize=$totalsize+$filesize done echo "Total file size in current directory: $totalsize" '''Output of Shell script with no debug command:''' $ ./filesize.sh Total file size in current directory: 652 '''Shell script with Debug command inside:''' Add set –xv inside the shell script now to debug the output as shown below. $ cat filesize.sh #!/bin/bash set -xv for filesize in $(ls -l . | grep "^-" | awk '{print $5}') do let totalsize=$totalsize+$filesize done echo "Total file size in current directory: $totalsize" '''Output of Shell script with Debug command inside:''' $ ./fs.sh # ls -l . # grep '^-' # awk '{print $5}' # for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')' # let totalsize=+178 # for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')' # let totalsize=178+285 # for filesize in '$(ls -l . | grep "^-" | awk '\''{print $5}'\'')' # let totalsize=463+189 # echo 'Total file size in current directory: 652' Total file size in current directory: 652 '''Execute Shell script with debug option:''' Instead of giving the set –xv inside the shell script, you can also provide that while executing the shell script as shown below. $ bash -xv filesize.sh =='''Hack 87. Quoting'''== echo statement without any special character. $ echo The Geek Stuff The Geek Stuff Echo statement with a special character ; . semi-colon is a command terminator in bash. In the following example, “The Geek” works for the echo and “Stuff” is treated as a separate Linux command and gives command not found. $ echo The Geek; Stuff The Geek -bash: Stuff: command not found To avoid this you can add a \ in front of semi-colon, which will remove the special meaning of semi-colon and just print it as shown below. $ echo The Geek\; Stuff The Geek; Stuff '''Single Quote''' Use single quote when you want to literally print everything inside the single quote. Even the special variables such as $HOSTNAME will be print as $HOSTNAME instead of printing the name of the Linux host. $ echo 'Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD' Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD '''Double Quote''' Use double quotes when you want to display the real meaning of special variables. $ echo "Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD" Hostname=dev-db <nowiki>; </nowiki>Current User=ramesh <nowiki>; </nowiki>Message=$ is USD Double quotes will remove the special meaning of all characters except the following: o $ Parameter Substitution. o ` Backquotes o \$ Literal Dollar Sign. o \´ Literal Backquote. o \" Embedded Doublequote. o \\ Embedded Backslashes. =='''Hack 88. Read data file fields inside a shell script'''== This example shows how to read a particular field from a data-file and manipulate it inside a shell-script. For example, let us assume the employees.txt file is in the format of {employee-name}:{employee-id}:{department-name}, with colon delimited file as shown below. $ cat employees.txt Emma Thomas:100:Marketing Alex Jason:200:Sales Madison Randy:300:Product Development Sanjay Gupta:400:Support Nisha Singh:500:Sales The following shell script explains how to read specific fields from this employee.txt file. $ vi read-employees.sh #!/bin/bash IFS=: echo "Employee Names:" echo "---------------" while read name empid dept do <nowiki>echo "$name is part of $dept department" done < ~/employees.txt</nowiki> Assign execute privilege to the shell script and execute it. $ chmod u+x read-employees.sh $ ./read-employees.sh Employee Names: <nowiki>---------------</nowiki> Emma Thomas is part of Marketing department Alex Jason is part of Sales department Madison Randy is part of Product Development department Sanjay Gupta is part of Support department Nisha Singh is part of Sales department
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)