- Shell Script is series of command written in plain text file.
- To find all available shells in your system type following command:
$cat /etc/shells - To find your current shell type following command
$ echo $SHELL - Example:
$vi first.sh
#!/bin/sh
# Comment
clear
echo “Knowledge is Power”
$chmod 755 first.sh
$./first.sh - RAM memory is divided into small locations, and each location had unique number called memory location/address, which is used to hold our data. Programmer can give a unique name to this memory location/address called memory variable or variable.
- Show system variables:
$set - Define a user defined variable:
variable_name=value; - Rules for naming variable name:
- Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character.
- Don't put spaces on either side of the equal sign when assigning value to variable.
- Variables are case-sensitive.
- You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition)
$ vech=
$ vech="" - Do not use ?,* etc, to name variable names.
- Use echo command to display text or value of variable. Display text or variables value on screen.
Syntax: echo [options] [string, variables...]
Options:
-n
Do not output the trailing new line.
-e
Enable interpretation of the following backslash escaped characters in the strings:
\a: alert (bell)
\b: backspace
\c: suppress trailing new line
\n: new line
\r: carriage return
\t: horizontal tab
\\: backslash
- Arithmetic operations
Syntax: op1 math-operator op2
ex.: $ echo `expr 10 \* 3` /* expr expression is enclosed by `(back quote)signs, \ is the escape character */ - Three types of quotes:
“
Double Quotes
"Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $).
‘
Single quotes
'Single quotes' - Enclosed in single quotes remains unchanged.
`
Back quote
`Back quote` - To execute command
- The value of exit status is zero/nonzero means the results of executing shell command(s) is successful/not successful. Use $? get the value of exit status.
- Use read to get input (data from user) from keyboard and store (data) to variable.
Syntax: read variable1, variable2,...variableN
#!/bin/bash
# Script to read your name from keyboard
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!" - Run more commands on one command line
Syntax: command1; command2 - Shell built in variables:
$?
Exit status.
$#
Number of command line arguments. Useful to test no. of command line args in shell script.
$*, $@
All arguments to shell.
$-
Option supplied to shell.
$$
PID of shell.
$!
PID of last started background process (started with &).
$i
i-th command line argument. Ex: $myshell foo bar, $0 is myshell, $1 is foo, $2 is bar.
- You can't assign the new value to command line arguments.
- Redirection of standard output/input
- > redirector symbol
Syntax: Linux-command > filename, to output Linux-commands result (output of command or shell script) to file. - >> redirector symbol
Syntax: Linux-command >> filename, to output Linux-commands result (output of command or shell script) to END of file. - < redirector symbol
Syntax: Linux-command < filename, to take input to Linux-command from file instead of key-board.
- > redirector symbol
- A pipe is a way to connect the output of one program to the input of another program without any temporary file.
- A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. Pipes are used to run more than two commands ( Multiple commands) from same command line.
Syntax: command1 | command2 - If a Linux command accepts its input from the standard input and produces its output on standard output is known as a filter.
Ex: $sort < sname | uniq > u_name, uniq is filter which takes its input from sort command and passes this lines as input to uniq. - A process is program (command given by user) to perform specific Job. In Linux when you start process, it gives a number to process (called PID or process-id), PID starts from 0 to 65535.
- An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be used to refer specific running process.
- Commonly used commands with process:
For this purpose
Use this command
Example
To stop processes by name i.e. to kill process
killall {Process-name}
$ killall httpd
To stop all process except your shell
kill 0
$kill 0
To display a tree of processes
pstree
$pstree
- Mar 01 Sat 2008 23:03
Shell script - 1. Getting started with Shell Programming
全站熱搜
留言列表