As we probably all know, shell script is badly required, when you thought to automate something in your operating system. We are here using Born shell(sh) and bash in linux ubuntu.
cat /etc/shells #it will give us following shells
/bin/sh # Bourne Shell
/bin/bash # Bourne again shell unix and windows
/bin/rbash # Restricted Shell for unix
/bin/dash # Debian Almquist shell
/usr/bin/tmux #Terminal Multiplexing
/usr/bin/screen
Shell Comments
We are making comments placing #
symbol at the beginning of line which we need to comment out, or write comment should start from #.
#! /bin/bash
#
➜ For comments!
➜ It is called bang/bin/bash
➜ We are using Bourne again shell
Special shell variables
$#
= No. of parameters given to script-
$@
= List of parameters given to script $0
= Name of current program (script)-
$1, $2
.. = Parameter 1, 2 and so on.. -
$?
= Exit value of last command run $$
,$!
= Both are process numbers- These variables are shell variables and only valid to the current shell.
#!/bin/sh
echo "I am calling with $# parameters"
echo "My file is $0"
echo "First parameter is $1"
echo "Second parameter is $2"
echo "All parameters are $@"
After execution it will return
bash index.sh
--------------output----------
I am calling with 0 parameters
My file is /home/kaushik/index.sh
First parameter is
Second parameter is
All parameters are
------------------------------
If we run it like this
bash index.sh pm1 pm2 pm3
I am calling with 3 parameters
My file is index.sh
First parameter is
pm1
Second parameter is pm2
All parameters are pm1 pm2 pm3
Special Shell characters
- * matches every character, just as in regular expressions.
- So, ls *txt in a script will list all files whose name ends in txt.
- \ is an escape character which tells the shell to not interpret the character after it.
- \ is commonly used to escape the special characters such as *, $ etc.
echo “Executing script : \”$0\” with $# parameters”
Key Words and Scope
Simple Variable | #!/bin/bash |
expr ( External program expr only expects numbers) | $x = “String” expr $x + 1 expr: non-numeric argument —————————– a=2; expr $a + 4 6 |
read (interactively set variable names using this) | echo What is your name? read MY_NAME echo “Hello $MY_NAME – What’s up.” |
export (Effect on the scope of variables) | !/bin/sh echo “The wall is : ${abc}” abc=’test’ echo “The wall is : ${abc}” —————————— The wall is : The wall is : test —————————— export abc=’exported’ —————————— The wall is : exported The wall is : test —————————— $abc // will output ‘exported’ |
touch (create any field) | echo “Create File ${abc}_file.sh” touch “${abc}_file.sh” Will create exported_file.sh |
Wildcards
How to copy all the files from /folder/a
into /folder/b
. Only the .php files or only the .html files
cp /folder/a/* /folder/b/
cp /folder/a/*.php /folder/b/
cp /folder/a/*.html /folder/b/
Linux list all environment variables
For printing env variables You can use following command with options
printenv | | less or more |
env | | less or more |
set | | less or more |
printenv > env.txt | cat env.txt |
printenv | grep HOME | For searching |
Few system variables I listed here for reference, this variables can be printed like this echo $HOME
HOME | Home Directory |
PWD | Present Working Directory |
USER | Current login user |
PATH | It specifies the directories in which executable programs* are located on the machine that can be started without knowing and typing the whole path to the file on the command line. |
SSH_CLIENT | SSH client info |
LOGNAME | Username of current session |
HOSTNAME | Print the host name you specified in host file |
EDITOR | export EDITOR=/bin/nano export VISUAL=nano Making nano is the default text editor |
SHELL | Shell path |
HISTSIZE | Number of commands to be remember |
BASH_VERSION | Holds the version of this instance of bash. |
LANG | Default language |
abc | This variable we exported with export $abc command |
Suggestions