Unix
Unix
When a process finishes execution, it will have an exit status to report to its parent
process. Because of this last little bit of information, the process will remain in the
operating systems process table as a zombie process, indicating that it is not to be
scheduled for further execution, but that it cannot be completely removed (and its
process ID cannot be reused) until it has been determined that the exit status is no
longer needed.
When a child exits, the parent process will receive a SIGCHLD signal to indicate that
one of its children has finished executing; the parent process will typically call the
wait() system call at this point. That call will provide the parent with the childs exit
status, and will cause the child to be reaped, or removed from the process table.
Orphan:
If a parent process dies, but its children have not, then those children are
"orphans process.
.PROFILE:
A profile file is a start-up file of an UNIX user, like the autoexec.bat file of
DOS. When a UNIX user tries to login to his account, the operating system
executes a lot of system files to set up the user account before returning the
prompt to the user.
To achieve this in UNIX, at the end of the login process, the operating system
executes a file at the user level, if present. This file is called profile file.
In case of sourcing the profile file in tcsh or csh, the command is:
source $HOME/.login
PATH:
PATH variable tells the places from where the Operating System(OS) will search for
whenever a command is given.
When the ls command is executed, the OS starts searching for an executable
named ls.
Where does it search? It searches in all the directories mentioned in the PATH
variable.
The moment it finds the executable, it executes the command the the output
is displayed.
SHELL:
A Unix shell is a command-line interpreter or shell that provides a user interface for
the Unix operating system and for Unix-like systems.
The UNIX shell is a program that serves as the interface between the user and the
UNIX operating system. It is not part of the kernel, but communicates directly with
the kernel. The shell translates the commands you type in to a format which the
computer can understand. It is essentially a command line interpreter.
Shebang(#!):
#! line states the interpreter to be used by the shell to interpret the script.
shebang line is used to tell shell which interpreter to use for your rest of the
script.
Running it as ./script.sh will make the kernel read the first line (the
shebang), and then invoke kornshell to interpret the script.
The dirname(1) and basename(1) commands
$ line="/dir1/dir2/gr3/file.ksh"
$ echo $line
/dir1/dir2/gr3/file.ksh
$ basename $line
file.ksh
$ dirname $line
/dir1/dir2/gr3
Maximum number of arguments that can be passed to shell:
The maxmium number of arguments can pass to a script is 9. If want to pass more
parameters, need to use the shift function.
#!/bin/sh
set -- "1" "2" "3" "4" "5"
echo $*
shift
echo $*
shift
echo $*
[blstst1a]:/home/bliss/kannan
$ ./"shift_ex.sh"
1 2 3 4 5
2 3 4 5
3 4 5
#!/bin/ksh
echo "Printing second args"
until [[ $# -eq 0 ]];do
echo $1
shift
done
./printer first second "third
forth"
Output:
Printing second args
first
second
third forth
Bourne shell:
var=value
export var
Korn shell:
export var=value
C-shell:
INODE:
All UNIX files have its description stored in a structure called 'inode'. The inode
contains info about the file-size, its location, time of last access, time of last
modification, permission and so on.
UNIX:
SPECIAL VARIABLES:
Variable
Description
$0
$n
$#
$*
$@
$?
$$
The process number of the current shell. For shell scripts, this is the
process ID under which they are executing.
$!
$* and $@ both will act the same unless they are enclosed in double quotes, "".
Example:
#!/usr/bin/ksh
echo "Printing \$* "
for i in $*
do
echo i is: $i
done
echo "Printing \$@ "
for i in "$@"
do
echo i is: $i
done
1 stdout
2 stderr
Example 2:
cat food 2>&1 >file
the shell sees 2>&1 first. That means "make the standard error (file descriptor 2) go to the
same place as the standard output (fd1) is going." There's no effect because both fd2 and
fd1 are already going to the terminal. Then >file redirects fd1 ( stdout ) to file . But
fd2 ( stderr ) is still going to the terminal.
file >/dev/null 2>&1
diff f1 f2 >/dev/null 2>&1
>/dev/null redirects standard out to /dev/null, i.e. throw the output away. 2>&1
redirects standard error to standard out; in this case, it means to throw all the
error output away as well
LINKS IN
UNIX
The file size of a soft linked file is the length of the filename of the original
file. In this case, the original file "file1" is of length 8.
In a soft linked file, the location where the file content is to be stored, the file
name of the original file gets stored, and hence the file size is so
Example:
_______
FIle1--------|
________
|______| welcome|
|_______|
|________|
9962464
_______
FIle3--------|
________
|______| File1
|_______|
|________|
9962471
Soft Link Representation (Files pointing to inodes, in turn pointing to data location)
Hard Link:
ln hard.txt hard1.txt
[blstst1a]:/home/bliss/kannan/unix_work $ ls
total 4
lrwxrwxr-x
1 bliss
bliss
8
soft.txt
-rw-rw-r-2 bliss
bliss
143
-rw-rw-r-2 bliss
bliss
143
ln hard.txt hard2.txt
[blstst1a]:/home/bliss/kannan/unix_work $ ls
total 6
lrwxrwxr-x
1 bliss
bliss
8
soft.txt
-rw-rw-r-3 bliss
bliss
176
-rw-rw-r-3 bliss
bliss
176
-rw-rw-r-3 bliss
bliss
176
-lrt
Dec
Dec
Dec
4 22:50 hard.txt
4 22:50 hard1.txt
-lrt
Dec
Dec
Dec
Dec
4 22:51 hard.txt
4 22:51 hard1.txt
4 22:51 hard2.txt
[blstst1a]:/home/bliss/kannan/unix_work $ rm hard.txt
[blstst1a]:/home/bliss/kannan/unix_work $ ls -lrt
total 4
lrwxrwxr-x
1 bliss
bliss
8 Dec 4 22:47 soft2.txt ->
soft.txt
-rw-rw-r--rw-rw-r-
2 bliss
2 bliss
bliss
bliss
176 Dec
176 Dec
4 22:51 hard1.txt
4 22:51 hard2.txt
Example:
|______| welcome|
_____|_______|
File2--------/
________
|________|
9962464
FIND COMMAND
find path_list selection_criteria action
+ sign is used to search for greater than, - sign is used to search for less than and
without sign is used for exact.
1. find . -mtime 1 (find all the files modified exact 1 day)
2. find . -mtime -1 (find all the files modified less than 1 day)
3. find . -mtime +1 (find all the files modified more than 1 day)
4. find . -mtime +10 (find all the file modified more than 10 days old)
find . -ctime -2 (Display the files which are changed within 2 days)
find . -size +10000c -print -exec ls -l {} \;
Always use a c after the number, and specify the size in bytes, otherwise you
will get confuse because find -size list files based on size of disk block.
To find files using a range of file sizes, a minus or plus sign can be specified
before the number.
The minus sign means "less than," and the plus sign means "greater than."
find . -size +10000c -size -50000c print
lists all files that are greater than 10,000 bytes, but less than 50,000 bytes
To find the smallest file in the current directory and sub directories
find . -type f -exec ls -s {} \; | sort -n
find
find
find
find
find
.
.
.
.
.
-type
-type
-type
-type
-type
inum n
-perm nnn
-links n
s
d
d
f
f
| head -1
UMASK:
Whenever create files and directories, the default permissions that are assigned to
them depend on the systems default setting.
The default permission
rw_rw_rw_ (octal 666) for regular files
rwxrwxrwx (octal 777) for directories
These are not the permission, when create a file or direcotry. The default is
transformed by subtracting the user mask from it to remove the one or more
permission.
umask equal to 022
666 022 = 644 (rw_r__r__ for regular files)
777 022 = 755 (rwx_r_xr_x for directory)
TOUCH:
Sometimes need to set the modification and access time to predefined values.
Syntax:
touch option expression filename
Whenever touch is used without option and expression, both time are set to the
current time.
The expression consist of an eight digit number format MMDDhhmm
Option: -m mean modification time
-a mean access time
touch m 12071030 file.txt (Dec 07 10:30)
touch a 12061030 file1.txt (Dec 06 10:30)
TEE:
TYPE:
Tee command is used to store and view (both at the same time) the output of
any other command.
Tee command writes to the STDOUT, and to a file at a time. By default tee
command overwrites the file.
o $ ls | tee file
Can instruct tee command to append to the file using the option a as shown
below.
o $ ls | tee a file
type is a Unix command that describes how its arguments would be interpreted if
used as command names.
type will display the command name's path. Possible command types are:
o shell built-in
o function
o alias
o hashed command
o keyword
The command returns a non-zero exit status if command names cannot be found.
Example:
$ type test
test is a shell builtin
$ type cp
cp is /bin/cp
$ type unknown
-bash: type: unknown: not found
$ type type
type is a shell builtin
$ type -a gzip
gzip is /opt/local/bin/gzip
gzip is /usr/bin/gzip
USER ENVIRONMENT
ENV:
FINGER:
finger is a program you can use to find information about computer users. It usually
lists the login name, the full name, home directory, shell information about a
particular user as shown below.
finger [email protected]
ID:
prints the user or group identifier of the account by which the program is executed
The root account has a UID of 0
id -un # Where `-u` refers to `--user` and `-n` refers to `--name`
SU:
The su command, also referred to as substitute user, super user, or switch user,
allows a computer operator to change the current user account associated with the
running virtual console.
When run from the command line, su asks for the target user's password, and if
authenticated, grants the operator access to that account and the files and
directories that account is permitted to access.
It should generally be used with a hyphen by administrators (su -, which is identical
to su - root), which can be used to start a login shell. This way users can assume
the user environment of the target user:
FOLD:
Fold is a Unix command used for making a file with long lines more readable
on a limited width terminal.
Most Unix terminals have a default screen width of 80, and therefore reading
files with long lines could get annoying.
The fold command puts a line feed every X characters if it does not reach a
new line before that point. If the -w argument is set, the fold command
allows the user to set the maximum length of a line.
JOIN;
Join command is used to combine two files based on a matching fields in the files.
join [options] file1 file2
Option:
-1 field number : Join on the specified field number in the first file
-2 field number : Join on the specified field number in the second file
-j field number : Equivalent to -1 fieldnumber and -2 fieldnumber
-o list : displays only the specified fields from both the files
-t char : input and output field delimiter
-a filenumber : Prints non matched lines in a file
-i : ignore case while joining
The basic usage of join command is to join two files on the first field. By default the
join command matches the files on the first fields when we do not specify the field
numbers explicitly.
chris 30 3
30 chris 3 db 3
PASTE:
The paste command merges the lines from multiple files. The paste command
sequentially writes the corresponding lines from each file separated by a TAB
delimiter on the unix terminal.
paste [options] files-list
Option:
-d : Specify of a list of delimiters.
-s : Paste one file at a time instead of in parallel.
> paste file1 file2
> cat file1
Unix
Dedicated server
Unix
Linux
Virtual server
Linux
Windows
Windows
> cat file2
Dedicated server
Virtual server
> cat file3
Hosting
Machine
Operating system
> paste -d"|" file1 file2
Unix|Dedicated server
Linux|Virtual server
Windows|
Unix
Linux
Windows
Dedicated server
Virtual server
SPLIT:
The split command splits the file into multiple files with 1000 lines into each output
file by default.
Since the input file does not contain 1000 lines, all the contents are put into only one
output file "xaa". By default, the output files generated contains the prefix "x", and
the suffix as "aa", "ab", "ac" and so on.
FILTERS:
CMP COMMAND:
Two files are compared byte by byte, and the location of the first mismatch is echoed
to the screen.
Cmp, when invoked without option doesnt bother about subsequently mismatches.
CUT COMMAND:
Head and tail command used to slice a file horizontally. The slice a file vertically with
the cut command. Cut identifies both column and fields.
-c ---- output only these characters
SORT COMMAND:
Significance
Uses delimiter char to identify field
Remove duplicate lines
Sort numerically
Reverse sort order
Check if the file is sorted
Sorts file based on the data in the specified field
positions.
Sorts based on months. Considers only first 3 letters as
month
Ignores leading spaces in each line
Places output in file file1
-m list
-k Option:-
You can specify the field positions using the -k option of sort command.
To sort based on the data in the second field, run the below command:
> sort -k2 order.txt
Unix distributed 05 server
Unix distributed 05 server
Distributed processing 6 system
Linux virtual 3 server
-u Option:Only unique values in the output using the - u option of the sort command.
> sort -u order.txt
Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server
> sort -t'|' -nrk2 delim_sort.txt
Declast|12
Mayday|4
Janmon|1
Sort the data in the monthwise using the -M option of the sort command. This is shown
below:
> sort -M delim_sort.txt
Janmon|1
Mayday|4
Declast|12
UNIQ COMMAND:
The uniq command can eliminate or count duplicate lines in a presorted file. It reads in
lines and compares the previous line to the current line. Depending on the options
specified on the command line it may display only unique lines or one occurrence of
repeated lines or both types of lines.
uniq [ -options [ input [output] ]
Options
-u Print only lines which are not repeated (unique) in the original file
-d Don't output lines that are not repeated in the input.
-c Generate an output report in default style except that each line is
preceded by a count of the number of times it occurred. If this option is
specified, the -u and -d options are ignored if either or both are also
present.
-i Ignore case differences when comparing lines
-f Ignore a number of fields in a line
-s Skips a number of characters in a line
-w Specifies the number of characters to compare in lines, after any
characters and fields have been skipped
--help Displays a help message
--version Displays version number on stdout and exits.
uniq file Simply fetches one copy of each line and write it to the standard output.
davel
davel
davel
jeffy
jones
jeffy
mark
mark
mark
chuck
bonnie
chuck
That gives you a truly unique list. However, it's also a useless use of uniq since
sort(1) has an argument, -u to do this very common operation:
% sort -u foo
jones
bonnie
davel
chuck
jeffy
mark
d tells uniq to eliminate all lines with
jeffy
mark
-c tells uniq to count the occurrences of
each line:
% sort foo | uniq -c
1 jones
1 bonnie
3 davel
2 chuck
2 jeffy
3 mark
LINE NUMBERING: nl
/home/bliss $ nl design.txt
1 davel
2 davel
3 davel
4 jeffy
5 jones
6 jeffy
7 mark
8 mark
9 mark
10 chuck
11 bonnie
12 chuck
5|jones
6|jeffy
7|mark
8|mark
9|mark
10|chuck
11|bonnie
12|chuck
Both command have same output.
PATTERN
*
.
+
?
Significance
Display count of number of occurrences
Display list of file name only
Display line numbers along with lines
Doesnt display lines matching expression
Ignore case when matching
Omits filenames when handling multiple files
Match complete word only
To show out only the matched string of the pattern
Display the block number at the beginning of each
line
Silent mode
search recursively i.e. read all files under each
directory for a string
Ouput will not display in terminal
[blstst1a]:/home/bliss/ $ grep -q loop *.sh
[blstst1a]:/home/bliss/ $ grep loop *.sh
sample_prt.sh:loop
sample_prt.sh:end loop;
MATCHES
^ (caret)
$
[abc]
[a-c]
[^abc]
previous character
For Matching at the beginning
For matching at the end
A single character a,b or c
A character between a to c
A single character which is not a,b or c
The sed is an instruction. An instruction combines an address for selecting lines with
an action to be taken.
Command
i,a,c
d
1,4d
r foo
w bar
3,$p
$!p
/begin/,/end/p
q
s/s1/s2/
-e
[[:space:]]
^ *$
Significance
Insert, append and change text
Delete lines
Delete lines 1 to 4
Places contents of file foo after line
Write address line to file bar
Print line 3 to end
Print all the line except last line
Print lines enclosed between begin and end
Quit after reading up to addressed line
Replace first occurrence of string s1 in all
lines with sting s2
e option to do this multiple substitutions
simply a special keyword that tells sed to match either a TAB or a
space.
'*' indicates 0 or more occurrences of the
previous character. '^ *$' indicates a line
containing zero or more spaces.
Note:In the above example each regular expression inside the parenthesis would be
back referenced by \1, \2 and so on. Here I used \ to give line break you should
remove this before running this command.
[blstst1a]:/home/bliss/kannan/unix_work $ cat sed1.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
/unix_work $
This is line
This is line
This is line
This is line
This is line
This is line
This is line
This is line
/unix_work $
This is line
This is line
This is line
This is line
/unix_work $
This is line
This is line
This is line
line number 10
Sed print all the lines on the std output in addition to the lines affected by the action.
So the addressed lines (the first two) are printed twice.
[blstst1a]:/home/bliss/kannan/unix_work $ sed -n '/.\{15,\}/p' sed1.txt
\{m,\} --- m mean length of the character line
Delete the first line AND the last line of a file, i.e, the header and trailer line of a file.
sed '1d;$d' file
With output redirection we can do the same thing.
sed i 4d tem.txt
Execute multiple commands with e option
sed e s/surendra/bca/ e s/mouni/mca/ e s/baby/bba/ tem.txt
How about reducing it more by using ;(Continuation operator) for the same
question?
Linux
Solaris
Delete the last line ONLY if it
contains either the pattern 'AIX' or
'HPUX':
Linux
Solaris
Delete the line containing the pattern
'Unix' and also the next line:
$ sed '/Unix/{N;d;}' file
Cygwin
Solaris
AIX
Note:
N command reads the next line in the
pattern space. d deletes the entire
pattern space which contains the current
and the next line.
Delete only the next line containing the pattern 'Unix', not the very line:
$ sed '/Unix/{N;s/\n.*//;}' file
Cygwin
Unix
Solaris
AIX
Using the substitution command s, we delete from the newline character till the end,
which effective deletes the next line after the line containing the pattern Unix.
Sample script for sending the report to mail id:
#!/bin/ksh
. /bliss/ofc/script/bliss_env
export FILE_NAME=/home/bliss/kannan/unix_work/report.txt
export OUT_FILE=/home/bliss/kannan/unix_work/output.txt
export REPORT_FILE=/home/bliss/kannan/unix_work/final_report.txt
export MAILLIST="[email protected]"
rm -f $FILE_NAME
rm -f $OUT_FILE
$ORACLE_HOME/bin/sqlplus -s $ORACLE_LOGIN <<!>>$OUT_FILE
set echo off
set head off
set feedback off
spool $FILE_NAME;
select
CUSTOMER_ORDER_ID||'|'||COE_ID||'|'||LINE_TYPE_CD||'|'||TELEPHONE_NUM
from telephone_number where customer_order_id=198425;
spool off;
exit;
!
sed '1i\
CUSTOMER_ORDER_ID|COE_ID|LINE_TYPE_CD|TELEPHONE_NUM' $FILE_NAME
>$REPORT_FILE
rm -f $FILE_NAME
if [ -s $REPORT_FILE ]; then
echo $REPORT_FILE "File sending to my id"
uuencode $REPORT_FILE $REPORT_FILE |mailx -s "Test Report" $MAILLIST
fi
exit 0
AWK:
awk is one of the most powerful utilities used in the unix world. Whenever it comes
to text parsing, sed and awk do some unbelievable things.
The syntax of awk is:
awk option 'selection criteria {action}' file
The selection criteria (a form of addressing) filter input and selects lines for the
action component to act on.
where the pattern indicates the pattern or the condition on which the action is to be
executed for every line matching the pattern.
In case of a pattern not being present, the action will be executed for every line
of the file.
In case of the action part not being present, the default action of printing the line
will be done.
Built-In Variables:
Variable
Function
NR
Cumulative number of lines read
FS
Input field separator
OFS
Output field separator
NF
Number of fields in current line
FILENAME
Current input file
ARGC
Number of arguments in command line
ARGV
List of arguments
F
Specify the delimiter
Built-In Functions:
Function
Significance
int(x)
Returns integer value of x
sqrt(x)
Returns square root of x
length
Return length of the complete line
substr(stg,m,n)
Returns portion of string of length n, starting from postion m in
string stg.
index(s1,s2)
Returns position of string s2 in string s1
split(stg,arr,ch)
Splits string stg into array arr using ch as delimiter;
system(cmd
Runs UNIX command cmd, and return its exit status.
Guru Migration
To omit the header record and get
only the names printed:
$ awk -F","
file1
Name
Deepak
Neha
Vijay
Guru
'{print $1}'
[blstst1a]:/home/bliss/kannan/
$ awk -F"," '{print $1,$3}'
OFS="|" file1
Name|Expertise
Deepak|MQ Series
Neha|Power Builder
Vijay|CRM Expert
Guru|Unix
Pass a variable to awk which contains the double quote. Print the quote,
line, quote.
[blstst1a]:/home/bliss/kannan/unix_work/awk_prg $ awk -v
q="'" -F"," '{print q $1 q}' file1
'Name'
'Deepak'
'Neha'
'Vijay'
'Guru'
To double quote the contents, pass the variable within single quotes
[blstst1a]:/home/bliss/kannan/unix_work/awk_prg $ awk -v
q='"' -F"," '{print q $1 q}' file1
"Name"
"Deepak"
"Neha"
"Vijay"
"Guru"
UUENCODE:
(mail sending)
The uuencode command converts a binary file to ASCII data. The uudecode command
converts ASCII data created by the uuencode command back into its original binary
form.
The uuencode command takes the named SourceFile (default standard input) and
produces an encoded version on the standard output.
The encoding uses only printable ASCII characters, and includes the mode of the file and
the OutputFile filename used for recreation of the binary image on the remote system.
- Encode the output using the MIME Base64 algorithm. If -m is not specified, the old
m uuencode algorithm will be used
Example:
uuencode $REPORT_FILE $REPORT_FILE |mailx -s "Test Report" $MAILLIST
PROCESS:
Option
-f
-e
-u usr
-a
-l
-t term
Significance
Fill listing showing the PPID of each process
All process including user and system processes
Processes of user usr only
Processes of all users excluding processes not
associated with terminal
A long listing showing memory related information
Processes running on terminal term (tty03)
The & is the shells operator used to run a process in the background.
NOHUP:
SIGNALS:
The following are some of the more common signals you might encounter and want
to use in your programs:
Signal Name
Signal
Number
SIGHUP
SIGINT
SIGQUIT
SIGFPE
SIGKILL
SIGALRM
14
SIGTERM
15
Description
JOB CONTROL:
CRONTAB:
The crontab command is used to schedule jobs to be run in the future, usually on
some regular schedule (such as every week). The command is run with one of three
command line arguments:
crontab -l
crontab -r
crontab -e
Edit (or create) user's crontab file (starts the editor automatically)
crontab file
Month of
year
Day of week
Command
Ranges 0-59
1-12
0-6
Begins with Sunday (0) and
ends Saturday (6).
0-23 1-31
Entry
@yearly (or
@annually)
@monthly
@weekly
@daily
@hourly
@reboot
Description
Run once a year at midnight in the morning of
January 1
Run once a month at midnight in the morning of
the first of the month
Run once a week at midnight in the morning of
Sunday
Run once a day at midnight
Run once an hour at the beginning of the hour
Run at startup
cron permissions
The following two files play an important role:
Equivalent
To
0 0 1 1 *
0 0 1 * *
0 0 * * 0
0 0 * * *
0 * * * *
@reboot
/etc/cron.allow - If this file exists, it must contain your username for you to use
cron jobs.
/etc/cron.deny - If the cron.allow file does not exist but the /etc/cron.deny file
does exist then, to use cron jobs, you must not be listed in the /etc/cron.deny file.
Default Actions:
Every signal has a default action associated with it. The default action for a signal is
the action that a script or program performs when it receives a signal.
Some of the possible default actions are:
Terminate the process.
Ignore the signal.
Dump core. This creates a file called core containing the memory image of the
process when it received the signal.
Stop the process.
Continue a stopped process
Sending Signals:
There are several methods of delivering signals to a program or script. One of the
most common is for a user to type CONTROL-C or the INTERRUPT key while a script
is executing.
When you press the Ctrl+C key a SIGINT is sent to the script and as per defined
default action script terminates.
The other common method for delivering signals is to use the kill command whose
syntax is as follows:
$kill -signal pid
Here signal is either the number or name of the signal to deliver and pid is the
process ID that the signal should be sent to.
For Example:
$ kill -1 1001
Sends the HUP or hang-up signal to the program that is running with process ID
1001.
To send a kill signal to the same process use the folloing command:
$ kill -9 1001
This would kill the process running with process ID 1001.
Trapping Signals:
When you press the Ctrl+C or Break key at your terminal during execution of a shell
program, normally that program is immediately terminated, and your command
prompt returned.
This may not always be desirable. For instance, you may end up leaving a bunch of
temporary files that won't get cleaned up.
$ trap '' 1 2 3 15
Note that the first argument must be specified for a signal to be ignored and is not
equivalent to writing the following, which has a separate meaning of its own:
$ trap 2
If you ignore a signal, all subshells also ignore that signal. However, if you specify an
action to be taken on receipt of a signal, all subshells will still take the default action
on receipt of that signal.
Resetting Traps:
After you've changed the default action to be taken on receipt of a signal, you can
change it back again with trap if you simply omit the first argument; so
$ trap 1 2
resets the action to be taken on receipt of signals 1 or 2 back to the default.
#!/bin/sh
# trap1a
trap 'my_exit; exit' SIGINT SIGQUIT
count=0
while :
do
sleep 1
count=$(expr $count + 1)
echo $count
done
my_exit()
{
echo "you hit Ctrl-C/Ctrl-\, now
exiting.."
# cleanp commands here if any
}
output:
[blstst1a]:/home/bliss/kannan/unix_work $ ./trap2.sh
1
2
3
4
5
6
7
you hit Ctrl-C/Ctrl-\, now exiting..
SHELL SCRIPT:
Any variable can become an environment variable. First it must be defined as usual;
then it must be exported with the command:
export varnames
Significance
File exists
File is a directory
File is a regular file
Have read permission on the file
-f file
-w file
-x file
If statement:
if [ $# -le 2 ];
then
echo "need 2 argument"
exit 1
fi
Shift:
#!/bin/sh
while [ $# -gt 1 ]; do
echo $1
shift
done
For:
for lst in `ls -l`; do
echo $lst
Case:
case:
#!/bin/sh
set -x
done
read input
case $input in
1) `ls -l`
break ;;
2) `ls`
break ;;
*) `ls -lrt`
exit ;;
esac
DELIM means either a newline or a ; delimiter.
if
if else if EXPR DELIM then STMT(S) DELIM elif EXPR ; then STMT(S) DELIM fi
for
Conditional Test:
String operations
string1 =
string2
string1 !=
string2
-z string
string
-n string
string1 ==
string2
str = regex
-o optname
True if shell option optname is enabled. See the list of options under
the description of the -o option to the set builtin below.
Numeric operations
arg1 OP
arg2
OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary
operators return true if arg1 is equal to, not equal to, less than, less than
or equal to, greater than, or greater than or equal to arg2, respectively.
Arg1 and arg2 may be positive or negative integers.
string1 <
True if string1 sorts before string2 lexicographi- cally in the current locale.
string2
string1 >
True if string1 sorts after string2 lexicographi- cally in the current locale.
string2
File operations
-e file
-d file
-f file
-L file
-r file
-w file
-x file
file1 -nt file2 True if file1 is newer (according to modification date) than file2.
file1 -ot file2 True if file1 is older than file2.
file1 -ef file2 True if file1 and file2 have the same device and inode numbers.
Less frequently used operations
-a file True if file exists.
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-g file True if file exists and is set-group-id.
-h file True if file exists and is a symbolic link.
-k file True if file exists and its "sticky'' bit is set.
-p file True if file exists and is a named pipe (FIFO).
-s file True if file exists and has a size greater than zero.
-t fd
Debugging Scripts
1. Use echo statements.
2. Run bash -n script to check for syntax errors.
3. Use the command set -v to get a verbose dump of each line the shell reads. Use
set +v to turn off verbose mode.
4. Use the command set -x to see what each command expands to. Again, set +x
turns this mode off.
Running it as ./script.sh will make the kernel read the first line (the shebang), and
then invoke bash to interpret the script. Running it as sh script.sh uses whatever
shell your system defaults sh to
. ./my_script.ksh executes the script within your current (probably login) shell.
sh my_script.ksh creates a new shell in a child process and executes within that.
If you run sh file.ksh , you're running "sh" (which may be linked to ksh or bash or
whatever) with file.ksh as input. It's run in a child process, though, so variables you
set are not available later in your current shell.
If you run ./file.ksh , the first 4 bytes of the file (the file "magic") are read,
determined to be a script, and the executable, if available, identified after the
shbang is run, feeding the file as input. Also run in a child, so no variables are
available after control is passed back to your current login shell.
If you have the shebang, then it will be executed using the command interpreter specified
in the shebang.
Execute Shell SCript by Specifying the Interpreter
$ sh scriptfile
rrespective of what is being used as shebang, the interpreter which you have specified
will be used for execution. You can use any interpreter (sh, ksh, bash, csh etc.,).
Execute Shell Script Using . ./ (dot space dot slash)
$ . ./scriptfile
This executes the commands specified in the scriptfile in the current shell, and prepares
the environment for you.
Execute Shell Script Using Source Command
$ source ~/.bashrc
What is Library?
A file that contains only functions is called a library. Usually libraries contain no main
code.
GETOPTS:
The parameters to your script can be passed as -n 15 -x 20. Inside the script, you
can iterate through the getopts array as while getopts n:x option, and the variable
$option contains the value of the entered option.
optstring - the string which contains the list of options expected in the
command line
name - the variable name which is used to read the command line options
one by one.
Env Variables:
getopts command makes use of 2 environment variables:
OPTARG : contains the argument value for a particular command line option.
OPTIND : contains the index of the next command line option.
TR (TRANSLATE)
-d
-s
and d to n.
Example:
tar -cvf kans.tar *
use scp with the -r option. This tells scp to recursively copy the source directory and its
contents.
scp -r script_prg
[email protected]:/bliss/home/kannan/
Therefore, to copy all the .txt files from the revenge directory on your deathstar.com
account to your revenge directory on empire.gov, enter:
scp [email protected]:"revenge/*.txt" ~/revenge/
without password:
Step 1 : local host
> ssh-keygen -t rsa
and when prompted for pass phrase I hit enter. Then id_rsa , id_rsa.pub files created in
<usershome>/.ssh directory.
Step 2 : local host
> scp id_rsa.pub <remoteIP>:/tmp/mykey
IhtvnKfI8Xyhl98eD1eSK2PY4KZ/BbsjCVptpFfGRiQi0Z9C4ApeRukPFGBNqU+X0ComaOj
NHwI5m8/OrwtoWn2osJnQVw9In1u2f8Z5kkgjJCrY/IAS+zMUaR5CUr78=
bliss@blstst1a
[blstst1a]:/home/bliss/.ssh $ scp id_rsa.pub
[email protected]:/bliss/home/authorized_keys
Password:
id_rsa.pub
100% 224
0.2KB/s
00:00
[blstst1a]:/home/bliss/.ssh $
[blstst1a]:/home/bliss/kannan $ scp -r script_prg
[email protected]:/bliss/home/kannan/
Enter passphrase for key '/home/bliss/.ssh/id_rsa':
sample_prt.sh
100% 1600
1.6KB/s
00:00
LOG_OUTP.txt
100% 4000
3.9KB/s
00:00
.txt
100%
16KB 15.8KB/s
00:00
iftesh.sh
100% 126
0.1KB/s
00:00
final_rpt.txt
100%
13KB 12.7KB/s
00:01
shift.sh
100%
63
0.1KB/s
00:00
Sticky bit:It is a user ownership access-right flag that can be assigned to files and directories
on Unix systems.
The sticky bit can be set using the chmod command and can be set using its octal
mode 1000 or by its symbol t (s is already used by the setuid bit).
chmod -t /usr/local/tmp
1. T The sticky bit is set (mode 1000), but not execute or search permission.
2. t The sticky bit is set (mode 1000), and is searchable or executable.
The Entry Type:
- Regular file.
b Block special file (stored in /dev).
c Character special file (stored in /dev).
d Directory.
l Symbolic link.
p FIFO.
s Socket.
w Whiteout.
Description
To create a new process
exec()
wait()
exit()
getpid()
getppid()
nice()
brk()
Example:
if [[ ${PRODUCT_NAME} = "app_rel" ]]; then
while [[ $I -lt ${#App_Dirset[*]} ]]; do
Explain:
The length command gives the length of the argument passed to it.
With no argument, length gives the length of the current line which is $0.
The substr command in awk extracts one character at a time and is appended
to the resultant variable x which is printed at the end using the END label.
--------------------------------#!/bin/bash x="welcome"
len=`echo ${#x}`
while [ $len -ne 0 ] do
y=$y`echo $x | cut -c $len`
((len--))
done
echo $y
--------------------------------cat sample.sh
#! /bin/sh
# reverse a string
[[ -z "$@" ]] && STR=" a b c d e f g h i j k l m n o p q r
s t u v w x y z " || STR="$@"
len=${#STR}
REV=""
for (( i=$len ; i>0 ; i-- ))
do
REV=$REV""${STR:$i-1:$i}
STR=${STR%${STR:$i-1:$i}}
done
echo "Reversed string"
echo $REV
output:
[~/temp]$ ./sample.sh
Reversed string
z y x w v u t s r q p o n m l k j i h g f e d c b a
Print the file content in reverse order:
$ cat file
Linux
Solaris
AIX
$ nl file | sort -nr | cut
-f 2AIX
Solaris
Linux
nl commands numbers the
file contents.
sort sorts the numbered
file in the reverse order
cut command removes the
number and prints the file
contents.
$ tac file
AIX
Solaris
Linux
$ awk '{a[i++]=$0}END{for(j=i1;j>=0;j--)print a[j];}' file
AIX Solaris Linux
Note:
One by one line is read and stored in an
array. At the end, the array is printed in
the reverse order by starting from the last
index.
$ cat file
Unix
Solaris
Linux
#!/bin/bash
while read line
do
x=$line
done < file
echo $x
$ awk 'END{print}' file
Linux
$ sed '$!d' file
Linux
Note:
$ tail -1 file
Linux
$ x=23
$ printf "%04d\n"
$x
$ typeset -Z4 x
$ x=23
$ echo
$x
Output:
0023
Ouput:
0023
Note:typeset has an option -Z which is
$ x=23
$ echo $x | awk '{printf
"%04d\n", $0;}'
x=23
while [ ${#x} -ne 4 ];
do
x="0"$x
done
echo $x
Output:
0023
Output:
0023
Insert a line before or after a pattern:Inserting before the pattern:
$ cat file
Linux
$ awk '/Fedora/{print
"Cygwin"}1' file
Solaris
Fedora
Ubuntu
AIX
HPUX
Linux
Solaris
Cygwin
Fedora
Ubuntu
AIX
HPUX
Note:
The '1' at the end prints every line by
default. On finding the pattern 'Fedora',
'Cygwin' is printed. Hence, first "Cygwin"
gets printed followed by 'Fedora'(1).
$ sed
's/.*Fedora.*/Cygwin\n&/'
file
Output:
Linux
Solaris
Cygwin
Fedora
Ubuntu
AIX
HPUX
Note:
On finding the pattern 'Fedora',
substitute with 'Cygwin' followed by the
pattern matched.
#!/usr/bin/bash
while read line
do
echo $line | grep -q "Fedora"
[ $? -eq 0 ] && echo "Cygwin"
echo $line
done < file
Note:
A line is read. grep -q is silent grep
where the result, if any, will not be
displayed. The status($?) will be 0 if a
match is found and hence 'Cygwin' is
printed.
$ awk '/Fedora/{print;print
"Cygwin";next}1' file
Output:
Linux
Solaris
Fedora
Cygwin
Ubuntu
AIX
HPUX
$ sed
's/.*Fedora.*/&\nCygwin/'
file
Output:
Linux
Solaris
Fedora
Cygwin
Ubuntu
AIX
HPUX
Note:
The difference here is the addition of two
extra commands: print and next. print
will print the current line, and the next
command will skip the current line from
Note:
Patten matched(&) followed by "Cygwin".
printing again.
#!/usr/bin/bash
while read line
do
echo $line
echo $line | grep -q "Fedora"
[ $? -eq 0 ] && echo "Cygwin"
done < file
Different ways to display the contents of a file:File name: a
$ cat a
a
b
c
---------------cat < a
$ grep '.*' a
a
b
c
$ paste a
a
b
c
Note:
To paste contents from
multiple files. But, when
paste is used without any
options against a single
file, it does print out the
contents.
$ while read line
do
echo $line
done < a
Output:
a
b
c
$ sed '' a
a
b
c
Note:
xargs takes input from
standard input. By default,
it suppresses the newline
character. -L1 option is to
retain the newline after
every(1) line.
Note:
sed without any command
inside the single quote just
prints the file.
$ sed -n 'p' a
a
b
c
$ sed -n '1,$p' a
a
b
c
$ awk '1' a
a
b
c
$ awk '{print;}' a
a
b
c
Note:
1 means true which is to
print by default. Hence,
every line encountered by awk
is simply printed.
$ awk '{print $0;}' a
a
b
c
note:
The print command of awk
prints the line read, which
is by default $0().