Posted on: Oct 08, '08

Shell Script
A shell is a command line interpretor. It takes commands and executes them. We can run N no of common commands one after another by putting them in one file( shell script ) in proper order.
Creating a simple shell filesuppose you want to view the some files in some directory regularly. so you need to use following command:
find /path/to/folder/*.ext -mtime 30 -type f -exec ls -lh {} \;This will display all files which are 30 days older from given folder path .
we can do this task in shell script as follows
#!/bin/sh
find /path/to/folder/*.ext -mtime 30 -type f -exec ls -lh {} \;
exit 0_______________________________________________________________
How to create shell script file with vi editor.1. vi file_name.sh - create the shell file
2. First statement of shell file should be
#!/bin/sh - start of shell script
3. write all commands one after another. - all commands to be executed
4.End of shell script - end of shell script
exit 0
5.come out of vi editor and run the shell file - run the shell file
./file_name.sh
simple.................
_______________________________________________________________
Passing value to shell scriptWe can also pass the arguments to our shell script. In our example we are displaying all the files which are modified 30 days before. Now suppose we want to see all the files which are modified 10 days before , then we need to make change in our shell script. To avoid this we can pass the date count to our script from command line. Just go through the sample file given below.
e.g.
#!/bin/sh
clear
echo *SCRIPT NAME = $0
echo *No Of Parameters = $#
echo *Parameters List = $*
echo List of files modified before $1 days.
find /path/to/folder/*.ext -mtime $1 -type f -exec ls -lh {} \;
exit 0
* echo is used to write output.
* $0 gives the script name.
* $# gives no of parameter count.
* $* gives a string of all parameters to script
* Individual argument can be accessed with $1,$2,$3,$4,$5.............
$1 for first parameter and so on..
How to execute ./file_name.sh +10
In this case +10 is passed to our shell script as first parameter and thus script runs accordingly.
Tags: php, shell scripting, shell commands, linux