[go: up one dir, main page]

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

Bash Scripting

The document provides an overview of shell and shell scripting in Linux, detailing how to create and execute shell scripts, manage arguments, and use special characters. It also explains the use of crontab for scheduling tasks, including syntax and examples for various scheduling scenarios. Additionally, it covers variable manipulation and reading user input within scripts.

Uploaded by

venkatesh patil
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)
4 views5 pages

Bash Scripting

The document provides an overview of shell and shell scripting in Linux, detailing how to create and execute shell scripts, manage arguments, and use special characters. It also explains the use of crontab for scheduling tasks, including syntax and examples for various scheduling scenarios. Additionally, it covers variable manipulation and reading user input within scripts.

Uploaded by

venkatesh patil
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

SHELL: Its program that takes a commnads from the user and give them to OS.

SHELL
interacts between user and the kernel

SHELL SCRIPTING: A text file where you put series of linux commands that can be
called as shell script

--->It is used to automate the manual and repetitive tasks.


---> Shell script file has an extension of .sh normally

shebang:

#!/bin/bash---> we call it as shebang--->which invokes bash shell . If we dont use


this a default shell will be used.

To check the available shells -->cat /etc/shells


TO check the default shell echo $SHELL
To check the default path echo $PATH
TO check the default user echo $USER

How to write a shell script

vi sample.sh

#!/bin/bash
pwd
echo "hello"
date
ls -al
uname -a
df -h

save and exit

we need to give execute permission to the user and groups


sudo chmod ug+x scriptfilename or sudo chmod 774 scriptfilename

we can run the script following ways


./scriptfilename
sh scriptfilename
bash scriptfilename

Arguments-->some statements or strings or numbers etc which are passed along with
the script

example ./sample.sh shreeni 32 bangalore


arguments are separated by space

How to read arguments inside script


$1-->1st argument
$2-->2nd argument
$3-->3rd argument
-
-
-
-
$n

example
vi arguments.sh
#!/bin/bash
echo "i am $1"
echo "i am aged $2"
echo "i am from $3"

to run this ./arguments.sh shreeni 34 bangalore

Assignments: understand the diffrence between diffrent shells , understand


diffrence between ubuntu and redhat linux
Do some hands on bash scripting , create some shell files with some
commands, arguments.
===================================================================================
======================================

2.Variables.sh
______________________________
#!/bin/bash

a=`pwd`
b=`ls -lrt`
c=`echo "I am from India"`

echo $a
echo $b
echo $c

3 manipulatevar.sh
#author shreeeni
#!/bin/bash

# Define variables
name="John"
age=25

# Print variable values


echo "Name: $name"
echo "Age: $age"

# Manipulate variables
age=$((age + 5))
greeting="Hello, $name! You will be $age years old in 5 years."
echo $greeting
_______________________________

Reading the input passed to the script

The read command in a shell script is used to read input from the user and store it
in a variable.
When the read command is executed in a shell script, the script waits for the user
to enter input from the keyboard.
Once the user has entered input and pressed the Enter key,
the input is read by the read command and stored in the variable specified in the
command.

#!/bin/bash

echo "Please enter your name: "


read name

echo "Hello, $name! Nice to meet you."

#!/bin/bash
echo "enter your name"
read name
echo hi $name
echo "enter your place"
read place
echo "You are from $place"
-----------------------------------------------------------------------------------
--

Special charecters:
--------------------------
--------------------------
These have special meaning to a shell.They are

$0-->Name of the file itself.


$#-->The toltal number of arguments passed to the script.
$*-->All arguments passed to the script.
$@-->All arguments passed to the script stored in script in array format.
$$-->Process id of current running process
$!-->It gives process id of the last command went into background.
$?--> Status of last executed command. Zero(0)means success and non-zero means
failure.

exmple vi special.sh
#!/bin/bash
echo "$0"
echo "$#"
echo "$*"
echo "$@"
echo "$$"
echo "$!"
echo "$?"

Numerics:
-----------------
-----------------
-eq--> equals
-gt--> greater than
-lt--> less than
-ge--> greater than or equal to
-le--> less than or equal to
-ne--> not equal to

diff.sh

#!/bin/bash

# Print all command line arguments using $*


echo "Printing command line arguments using \$*:"
for arg in "$*"; do
echo "$arg"
done

# Print all command line arguments using $@


echo "Printing command line arguments using \$@:"
for arg in "$@"; do
echo "$arg"
done

-----------------------------
===================================================================================
================
#CRONTAB:The crontab is a list of commands that you want to run on a regular
schedule,
and also the name of the command used to manage that list.
=========

* * * * *
Min hours Date month Day of the week
(0-59 min) (0-23hrs) (1-31 days) (1-12) (00-06) 00-sunday
01-Monday
-
06-saturday

Commands:
export VISUAL=vi
installing new crontab
crontab -e --> to create and edit the crontab job
crontab -l --> to view the crontab jobs
systemctl status cron
grep cron /var/log/syslog --->tocheck logs

syntax:
* * * * * /path to script

Examples:
run at 10am everyday
00 10 * * * cd /test/d3/dir/shellscript && ./simple.sh

run script at 1 pm every friday


00 13 * * 05 /script path

run script at 1 pm every monday to friday:


00 13 * * 01-05 /script path

run script at 8:30pm every tuesday and thursday


30 20 * * 02,04 /script path

run script at 10pm coming monday to saturday


00 22 22-27 11 01-06 /script path

run script every 30 min


*/30 * * * * /script path

run script every 2 min


*/2 * * * * /script path
run script for every min
* * * * * /script path

run script every hour


00 * * * * /script path

run script every day


00 00 * * * /script path

run script for every 2 hour


* */2 * * * /script path

run script for every 2 days


00 00 */2 * * /script path

run script for every year:


00 00 01 01 * /script path

Note:
1) Crontab will always schedule scripts for minutes and will not schedule for
seconds.
2) If you specify all * then script will run every minute.
* * * * *

You might also like