Bash Shell
Bash is used to run commands and write scripts on Unix/Linux systems. Bash is the default shell on most Linux and macOS systems. Bash added features from other shells, such as the Korn shell (ksh) and C shell (csh),
[root@localhost ~]# echo $SHELL
/bin/bash
[root@localhost ~]# cat /etc/shells --available shells
/bin/sh --Ã cannot find many function, command
/bin/bash
/usr/bin/sh
/usr/bin/bash
[root@localhost ~]# echo "Hello World" Ã in terminal
Hello World
[root@localhost ~]# vi shell1.sh
[root@localhost ~]# ls -l
-rw-r--r-- 1 root root 32 Jun 6 13:05 shell1.sh
[root@localhost ~]# bash shell1.sh
Hello World
[root@localhost ~]# env
[root@localhost ~]# echo $PWD
/root
[root@localhost ~]# echo $PATH
/usr/share/Modules/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo $SHELL
/bin/bash
[root@localhost ~]# su – santosh -----Ã To switch to other user
santosh@localhost ~]$ echo $USER
santosh
env----------------------------------> env---user wise will change, only last for session only
[santosh@localhost ~]$ env
[santosh@localhost ~]$ $PATH
-bash: /home/santosh/.local/bin:/home/santosh/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin: No such file or directory
[santosh@localhost ~]$ export NAME=centos here, NAME=variabler, centos=value
[santosh@localhost ~]$ env | grep NAME
HOSTNAME=localhost.localdomain
NAME=centos
LOGNAME=santosh
[santosh@localhost ~]$ var2=NEPAL
[santosh@localhost ~]$ env | grep NEPAL
[santosh@localhost ~]$ echo $var2
NEPAL
[santosh@localhost ~]$ exit logout from user
logout
[root@localhost ~]# echo $NAME
-----------Nothing will show
[root@localhost ~]# su - santosh
[santosh@localhost ~]$ echo $var2
[santosh@localhost ~]$ echo $NAME ---doesn't show any things ......only for session
[santosh@localhost ~]$ export NAME=san
[santosh@localhost ~]$ echo $NAME
san
[santosh@localhost ~]$ export NAME=karma
[santosh@localhost ~]$ echo $NAME
karma
[santosh@localhost ~]$ unset NAME Ã To remove
[santosh@localhost ~]$ echo $NAME
[santosh@localhost ~]$ vi exercise2.sh
[santosh@localhost ~]$ ./exercise2.sh
-bash: ./exercise2.sh: Permission denied
[santosh@localhost ~]$ chmod +x exercise2.sh -Ã To make executable file
[santosh@localhost ~]$ ./exercise2.sh
My Name is San
[santosh@localhost ~]$ cd /tmp/
[santosh@localhost tmp]$ ./exercise2.sh
-bash: ./exercise2.sh: No such file or directory à file must run where is made
[santosh@localhost tmp]$ cd
[santosh@localhost ~]$ ls
Desktop Downloads Music Public Videos
Documents exercise2.sh Pictures Templates
[santosh@localhost ~]$ mkdir -p .local/bin
[santosh@localhost ~]$ mv exercise2.sh .local/bin
[santosh@localhost ~]$ echo $PATH
/home/santosh/.local/bin:/home/santosh/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
[santosh@localhost ~]$ cd /tmp/ ---->run from anywhere, it works
[santosh@localhost tmp]$ exercise2.sh
My Name is San
[santosh@localhost tmp]$ cd /var/log
[santosh@localhost log]$ exercise2.sh
My Name is San
[santosh@localhost log]$ cd
[santosh@localhost ~]$ cat .local/bin/exercise2.sh
#!/bin/bash
NAME=San
echo "My Name is $NAME"
[santosh@localhost ~]$ mkdir $HOME/.local/bin here, $HOME=variable or use [santosh@localhost ~]$ mkdir -p ~/.local/bin where, ~/ any user home directory
mkdir: cannot create directory ‘/home/santosh/.local/bin’: File exists
[santosh@localhost ~]$ mkdir -p $HOME/.local/bin
[santosh@localhost ~]$ echo $HOME
/home/santosh
[santosh@localhost ~]$ vi exercise2.sh
[santosh@localhost ~]$ chmod +x exercise2.sh
[santosh@localhost ~]$ ./exercise2.sh
Hello testing
[santosh@localhost ~]$ cd /tmp/
[santosh@localhost tmp]$ ls
[santosh@localhost tmp]$ ./exercise2.sh
-bash: ./exercise2.sh: No such file or directory
[santosh@localhost tmp]$ $HOME/exercise2.sh à To execute, give full path
Hello testing
OR,
[santosh@localhost tmp]$ cd
[santosh@localhost ~]$ mkdir bin
[santosh@localhost ~]$ mv exercise2.sh /bin/
mv: cannot move 'exercise2.sh' to '/bin/exercise2.sh': Permission denied
[santosh@localhost ~]$ sudo mv exercise2.sh /bin/
[sudo] password for santosh:
[santosh@localhost ~]$ cd /bin/
[santosh@localhost bin]$ ls
[santosh@localhost bin]$ cd /tmp/
[santosh@localhost tmp]$ exercise2.sh
My Name is San here output is My Name is San not Hello tesing because of precedienty of ./local/bin
[santosh@localhost tmp]$ cat ~/.local/bin/exercise2.sh
#!/bin/bash
NAME=San
echo "My Name is $NAME"
[santosh@localhost tmp]$ cat ~/bin/exericise2.sh
cat: /home/santosh/bin/exericise2.sh: No such file or directory
[santosh@localhost tmp]$ echo $PATH
/home/santosh/.local/bin:/home/santosh/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin --Ã shows on my name is because of precedencity /.local/bin
[santosh@localhost tmp]$ cd /bin/
[santosh@localhost bin]$ mv exercise2.sh exercise3.sh
mv: cannot move 'exercise2.sh' to 'exercise3.sh': Permission denied
[santosh@localhost bin]$ sudo mv exercise2.sh exercise3.sh
[sudo] password for santosh:
[santosh@localhost bin]$ cd /tmp/
[santosh@localhost tmp]$ ls
[santosh@localhost tmp]$ exercise3.sh
Hello testing
[root@localhost ~]# cd /bin
[root@localhost bin]# ls
[root@localhost bin]# vim exercise4.sh
root@localhost bin]# chmod +x exercise4.sh
[root@localhost bin]# vim exercise4.sh
[root@localhost bin]# exercise4.sh
value1 my first variable
Enter your name
san
Your name is san
:Testing
[root@localhost ~]# cd /
[root@localhost /]# cd bin
[root@localhost bin]# vi exercise5.sh
[root@localhost bin]# chmod +x exercise5.sh
[root@localhost bin]# ./exercise5.sh ram and hari -->parameter passed
You have passed ram and hari
[root@localhost bin]# man chpasswd --for password
[root@localhost bin]# vi user.sh
[root@localhost bin]# chmod +x user.sh
[root@localhost bin]# ./user.sh karma Chelsea
chpasswd: line 1: user 'karma' does not exist
chpasswd: error detected, changes ignored
Username is karma and password is chelsea
Solution:
[root@localhost bin]# vi user.sh
[root@localhost bin]# chmod +x user.sh
[root@localhost bin]# ./user.sh karma chelsea
Username is karma and password is Chelsea
Note: if from user the add sudo
To check username and password
[root@localhost bin]# cat /etc/passwd
[root@localhost bin]# id karma
uid=1001(karma) gid=1001(karma) groups=1001(karma)
[root@localhost bin]# vi user.sh -Ã $# check number if parameter
[root@localhost bin]# ./user.sh k kk
2 is number of parameter
Username is k and password is kk
Practice: ssh enable or not
service running or not, if not running, run it , if it is already running,message it as its running
Add 50 user and password
install nginx
install mysql
install postgress
Database backup and restore
[root@localhost ~]# cd /
[root@localhost /]# ls
bin etc lib mnt opt run shell1.sh tmp
boot home lib64 nginxscript proc sbin srv usr
dev HOME media nginxscript.sh root script sys var
[root@localhost /]# cd bin
[root@localhost bin]# vi if.sh
[root@localhost bin]# chmod +x if.sh
[root@localhost bin]# ./if.sh
Enter the name
ram
You are successful
[root@localhost /]# cd bin
[root@localhost bin]# vi else.sh
[root@localhost bin]# chmod +x else.sh
[root@localhost bin]# ./else.sh
Enter the name
ram
You are successful
[[root@localhost bin]# ./else.sh
Enter the name
jeevan
You are not done yet
[root@localhost bin]# name=hari
[root@localhost bin]# if [ $name == "hari" ]; then echo "You are successful"; fi
You are successful
[root@localhost ~]# cd /
[root@localhost /]# cd bin
[root@localhost bin]# vi for.sh
[root@localhost bin]# chmod +x for.sh
[root@localhost bin]# ./for.sh
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5