0% found this document useful (0 votes)
324 views5 pages

Introduction To Automation With Bash

1. The document discusses various bash commands and how to write and execute bash shell scripts. It shows how to check the shell using commands like echo $SHELL and check installed shells using cat /etc/shells. It also demonstrates downloading, extracting, and installing Tomcat using a bash script. 2. Steps are demonstrated for writing a basic bash script to download and install Tomcat, including using wget to download the file, tar to extract, and rm and mv to clean up. Issues with file permissions and bad interpreters when executing the script are also covered. 3. Techniques are shown for executing custom scripts like checking available disk space. Topics like setting execute permissions, adding scripts to PATH,

Uploaded by

KrishnaReddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
324 views5 pages

Introduction To Automation With Bash

1. The document discusses various bash commands and how to write and execute bash shell scripts. It shows how to check the shell using commands like echo $SHELL and check installed shells using cat /etc/shells. It also demonstrates downloading, extracting, and installing Tomcat using a bash script. 2. Steps are demonstrated for writing a basic bash script to download and install Tomcat, including using wget to download the file, tar to extract, and rm and mv to clean up. Issues with file permissions and bad interpreters when executing the script are also covered. 3. Techniques are shown for executing custom scripts like checking available disk space. Topics like setting execute permissions, adding scripts to PATH,

Uploaded by

KrishnaReddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Introduction to Automation with Bash


========================================
uname
uname -a
How do I check which shell I am using?
[root@node ~]# which $SHELL
/bin/bash

You can type the following command in your terminal to see which shell you are
using:
[root@wm1 home]# echo $0
-bash

cat /etc/shells
systemctl status httpd
vi mycmd
ls
pwd
sh mycmd

2. Basics steps to write and execute bash


===================================================================================
========================

wget http://apachemirror.wuchna.com/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-
9.0.27.tar.gz
tar -xvzf apache-tomcat-9.0.27.tar.gz
rm apache-tomcat-9.0.27.tar.gz
mv apache-tomcat-9.0.27 tomcat9

rm -rf tomcat9
vi install_tomcat.sh
[root@node ~]# which $SHELL
/bin/bash

vi install_tomcat.sh
#!/bin/bash/

wget http://apachemirror.wuchna.com/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-
9.0.27.tar.gz
tar -xvzf apache-tomcat-9.0.27.tar.gz
rm apache-tomcat-9.0.27.tar.gz
mv apache-tomcat-9.0.27 tomcat9

/bin/bash install_tomcat.sh

ls -lrt
rm -rf tomcat9
chmod +x install_tomcat.sh
chmod -x install_tomcat.sh
chmod u+x install_tomcat.sh
./install_tomcat.sh

[root@node ~]# ./install_tomcat.sh


-bash: ./install_tomcat.sh: /bin/bash/: bad interpreter: Not a directory
[root@node ~]# ls -lrt
sh install_tomcat.sh

sudo yum remove wget

[root@node ~]# sh install_tomcat.sh


install_tomcat.sh: line 3: wget: command not found
tar (child): apache-tomcat-9.0.27.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
rm: cannot remove �apache-tomcat-9.0.27.tar.gz�: No such file or directory
mv: cannot stat �apache-tomcat-9.0.27�: No such file or directory
[root@node ~]#

3. Running user defined shell script as a command


===================================================================================
=========================
free -m
free -m | awk 'NR==2{print $4}'
free -m | awk 'NR==2{print $4,"MB(MegaBytes)"}'
vi freem.sh
#!/bash/bin
free -m | awk 'NR==2{print $4,"MB(MegaBytes)"}'

.[root@node ~]# ./freem.sh


-bash: ./freem.sh: Permission denied
[root@node ~]# sh freem.sh
1182 MB(MegaBytes

without providing path execute script


which ls
which pwd
which cp
echo $PATH
mv freem.sh bin/
c
freem.sh -- not providing dot here to execute
cd /bin
mv freem.sh freem

hmod +x freem

freem

mkdir usercmds
cd usercmds
mkdir bin
cd bin
pwd

mv /home/ec2-user/bin/freem /home/ec2-user/usercmds/bin/
cd
pwd
/home/ec2-user/
freem no such file or directory

[root@node usercmds]# cd
[root@node ~]# freem
-bash: /usr/bin/freem: No such file or directory
[root@node ~]# sh freem
sh: freem: No such file or directory
[root@node ~]#

echo $PATH
Eexport PATH=$PATH}:/home/ec2-user/usercmds/bin
echo $PATH
freem

exit
reconnect
echo $PATH
vi .bash_profile
freem -command not found
source .bash_profile

echo $PATH
free

Section 2: Variables and complete echo command


============================================================================
4. simple shell script to display "Welcome to shell scripting "
-----------------------------------------------------------------
echo hi
echo 3456
vi hello.sh
#!/bin/bash
echo "hello, welcome to shell scripting course"

chmod +x hello.sh

[root@node ~]# ./hello.sh


hello, welcome to shell scripting course

vi hello.sh
#!/bin/bash
echo "hello, welcome to shell scripting course"
echo 'this messege using single quotes'

[root@node ~]# ./hello.sh


hello, welcome to shell scripting course
this messege using single quotes
[root@node ~]#

differences amoung double single without quotes


ls
echo "ls"
echo 'ls'
echo ls
echo "$(ls)"
echo '$(ls)'
echo $ls

echo "hi"
echo 'hi'
echo hi
vi hello.sh
#!/bin/bash
echo "hello, welcome to shell scripting course"
echo 'this messege is using single quotes'
echo "the below output is using echo command for ls command"
echo "---------------with double quotes----------"
echo "$(ls)"
echo "---------------with single quotes----------"
echo '$(ls)'
echo "---------------without any quotes----------"
echo $(ls)

[root@node ~]# ./hello.sh


hello, welcome to shell scripting course
this messege is using single quotes
the below output is using echo command for ls command
---------------with double quotes----------
anaconda-ks.cfg
Desktop
Documents
Downloads
hello.sh
initial-setup-ks.cfg
install_tomcat.sh
Music
Pictures
Public
Templates
tomcat9
usercmds
Videos
---------------with single quotes----------
$(ls)
---------------without any quotes----------
anaconda-ks.cfg Desktop Documents Downloads hello.sh initial-setup-ks.cfg
install_tomcat.sh Music Pictures Public Templates tomcat9 usercmds Videos

5. Concepts of Variables
==================================================================================

vi about_variables.sh

#!/bin/bash

x=4
y=55
z=7

chmod +x about_variables.sh
[root@node ~]# ./about_variables.sh
[root@node ~]#

we are not getting any output


so we need echo or direct command

vi about_variables.sh

#!/bin/bash

x=4
y=55
z=7
ls
[root@node ~]# ./about_variables.sh
about_variables.sh Desktop Downloads initial-setup-ks.cfg Music Public
tomcat9 Videos
anaconda-ks.cfg Documents hello.sh install_tomcat.sh Pictures Templates
usercmds
[root@node ~]#

You might also like