Total Pageviews

Thursday, March 22, 2012

Unix Shell Scripting KB

Cheat Sheet

Write a shell script to save the output of 'ls -ltr' every five minutes and after every hour it should print which all files are changed/modified/created. "Setup crontab entries to run the commands repeatedly.
find . -mtime 0 # find files modified between now and 1 day ago (i.e. within the past 24 hours)
find . -mtime -1 # find files modified less than 1 day ago (i.e. within the past 24 hours as before)
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10 # find files modified between 6 and 9 minutes ago"

How do you read arguments in a shell program - $1, $2 ? "#!/bin/sh
for i in $*
do
echo $i
done

Hot to check length of a variable
echo ${#var1}
echo "hello" | wc -c

On executing the above script with any number of command-line arguments it will display all the parameters.
$1 would be the first command line argument 
$2 the second and so on
$0 is the name of the script or function

Shell script accepts parameters in following format...
$1 : first
$2 : second....so on upto
$9 : 9th param
whereas $0 : gives script/function name

If your script has more than 9 params then accept in following way...
${12} : 12th param
${18} : 18th param
U can also use Shift for getting the behind 9th parameters.........
exec<$1 or exec<$2"

What are the different kinds of loops available in shell script?
"Broadly categorized in 3
for
while
until"

What is the difference between a shell variable that is exported and the one that is not exported?
"exported variable is visible to the child processes while the normal variables are not.
export LANG C
will make the variable LANG the global variable put it into the global environment. all other processes can use it.

LANG C
will change the value only in the current script.
try this simple script it will clear your concept :-
script a.sh :-
----------
export A=3
B=5
sh b.sh
--------
script b.sh :-
-----------
echo ""exported variable $A is :- $A""
echo ""Variable $B -- $B ""
---------
A persists in b.sh whereas B doesn't. "

If you have a string "one two three", Which shell command would you use to extract the strings? "echo ""one two three""
cut -d"" "" -f 1,2,3
or
echo ""one two three""
awk '{print $1 $2 $3}'
cut awk sed and others are not SHELL commands. One way could be with bash: string one two three tab ($string)$ echo ${tab[0]}one$ echo ${tab[1]}two$ echo ${tab[2]}threehttp://www.big-up.org
echo $string
tr "" "" ""n""
string one two three tab ($string)$ echo ${tab[0]}one ${tab[1]}two ${tab[2]}three"

How will you list only the empty lines in a file (using grep)? "grep ^$ filename.txt
grep ^[ ]*$ filename.txt
In character set (between [ and ] one space and tab is given)
this command will gives all the blank line including those having space and tabs (if pressed)only
We can do this efficiently through awk script
awk '{
if (NF 0)
{
print ""Here comes the empty line""
print $0
}
}' filename
NF number of fields in the current line
$0 current line

grep -v . filename

cat test.sh
grep ^$
grep -v . test.sh
grep '[ t]*$' file_name"

When you login to a c shell, which script would be run first? (before the terminal is ready for the user) ".profile file will execute first when a user logs in.
For C shell ,
first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run. "

How would you get the character positions 10-20 from a text file? "cat filename.txt
cut -c 10-20
cut -c10-20
substring ${string_variable_name:starting_position:length}"

How would you print just the 25th line in a file (smallest possible script please)? "tail -n +25Â
head -1 OR head -n 25
tail -1
head -25 filename
tail -1OR
head -25 file
tail -1 goes wrong because if someone deletes some lines even then it gives last line as 25th line.
so the right option is
tail +25 file
head -1 OR
tail -n +25
head -1 OR
head -n 25
tail -1
sed -n '25p' filename.txt"

How To Connect To Data Base in Solaris "echo Enter User Name 

read name
echo Enter passwd stty -
echo read passstty 
echo sqlplus -s $name@oracle/$pass<"
How would you replace the n character in a file with some xyz? 

"sed 's/n/xyz/g' filename > new_filenamevi file name
then go to command mode
s/n/xyz/gtail -1 sample.txt
sed 's/.$/xyz/'

Check this
perl -p -i -e ""s/n/abc/g"" file_name
We can replace n characters by using the following command:
1 $s/./xyz/g
where 1 shows that the search string will start searching patterns from first line of the file.
'.' for any character.
g for global replacement.


FOLLOWING IS THE ANSWER FOR REPLACING RANGE OF CHARACTERS IN A LINE OF FILE. HERE 5TH TO 10TH CHARACTERS IN THE FILE text.txt ARE REPLACED BY ""XYZ""
cut -c5-10 text.txt
sed 's/^.*$/XYZ/g'search for the character n in filename and replace it with xyz.
sed 's/n/xyz/g' filename"

How to find how many users have logged in and logged out in last five minutes using shell scripts? "By writing ""who -Hu"" you get following details o/p
Using SIGALRM"


Error Handling
Shell Scripting Commands "echo $?
if the output of command is 0 then successfully executed
if the output is non-zero then not successfully executed.
If a command is executed successfully then the exit status of a command is 0. 

We can test whether a command is executed successfully or not by writing 2 line
[ command_name ] # command_name is any unix command
echo $?
# $? contains success/failure of the last command that has been executed"


Other methods of error handling
http://unix.stackexchange.com/questions/21151/error-handling-in-shell-script


$ cat test2.sh
if [ -f run_script.lck ]; then
  echo Script $0 already running
  exit 1
fi
trap "rm -f run_script.lck" EXIT
# rest of the script ...

set -e means that each exit status unequal 0 ends the execution of the script. Similar to that, with  
set -u every (probably accidental) use of an undefined variable ends the execution.
set -x means .....................

command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.

command1 || command2
command2 is executed if, and only if, command1 returns a non-zero exit status.  

http://linuxcommand.org/wss0150.php

[me] $ true || echo "echo executed"
[me] $ false || echo "echo executed"
echo executed
[me] $ true && echo "echo executed"
echo executed
[me] $ false && echo "echo executed"

http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script

Since cd returns a non-zero status on failure, you could do:
cd -- "$1" && echo OK || echo NOT_OK
You could simply exit on failure:
cd -- "$1" || exit 1
Or, echo your own message and exit:
cd -- "$1" || { echo NOT_OK; exit 1; }
And/or suppress the error provided by cd on failure:
cd -- "$1" 2>/dev/null || exit 1

 
"Shell Scripting question : Find text in the file
nishanth.sed
1h
2,10{H;g}
$q
1,9d
N
D
input_file
aa
bb
cc
dd
ee
ff
gg
hh
ii
jj
kk
ll
mm" "script

sed -f nishanth.sed input_file "

What is the difference between writing code in shell and editor? "Code in the script (Shell is interprted) as shell is a interpreter
where as editor is not inter preter certain set of commands(predefined)
are used to handle editor"

"Shell script
You have current directory containing set of directories which contain files.
One file can reside in many directories.
Write script which returns number of unique file names in
all the subdirectories of a current dir." 

"ls -R sort uniq" should work

How do you search the string for vowel's occurrence and number of occurrences of each vowel "grep -io [aeiou] filename
wc -w
for a cat filename
tr -d 'eioubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep a
wc -m
for e cat filename
tr -d 'aioubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep e
wc -m
for i cat filename
tr -d 'aeoubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep i
wc -m
for o cat filename
tr -d 'aeiubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep o
wc -m
for u cat filename
tr -d 'aeiobcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep u
wc -m
it will give the no of time that vowel has been occured
for searching the string
cat filename
grep 'aeiou'
hey please also add tr -d ' before wc -m
FE : cat filename
tr -d 'eioubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep a
tr -d '
wc -m
for counting the vowels
grep -io [aeiou] filename
wc -w
hey please also add tr -d ' before wc -m
FE : cat filename
tr -d 'eioubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$ &^*""()_+{}
:""<>? ./;][ -/n '
grep a
tr -d '
wc -m
for counting the vowels
Even the below command can be used:
grep -io [aeiou] filename
wc -w
For 'a'
cat file_name
tr -cd 'a'
wc -m
For 'e'
cat file_name
tr -cd 'e'
wc -m
For 'i'
cat file_name
tr -cd 'i'
wc -m
For 'o'
cat file_name
tr -cd 'o'
wc -m
For 'u'
cat file_name
tr -cd 'u'
wc -m
I am giving the 3 ways to solve this problem..
1) grep -io [aeiouAEIOU] filename
sort
uniq -c
2) cat filename
sed 's/[a-zA-Z]/&n/g'
sed 's/^ //'
grep [aeiouAEIOU]
sort
uniq -c
3) cat filename
fold -1
sort
sed -n '/^[aeiouAEIOU]/p'
uniq -c
"

"write a script to convert all dos style backslashes to unix style slashes in a list of files?
write a script to list all the differences between two directories ?
what is the command for to display the last newly appending lines of a file during appending data to the same file by same processes ?
what is the command to display top most processes which has utilised most of the cpu?" "for i in `ls *`;do cat $i
sed 'y
\
/
' > $i.newthis will create a new file for each of the replace files so remove the .new line to get the same updated.tail -f filenameshould display the updates made to a file.top -c -d2 should list the processes based on the current usage.
you can use this command to display top most processes which has utilised most of the cpu.

ps aux
sort -n +2
tail -1
lets say window.list has the file list
then this will do the trick

cat window.list
sed 's///\/g'
You can simply use dos2unix command which allow you to change all window special character to Unix format
dos2unix only removes the control-M characters at the end of the line. 

You need to use sed to do the search and replace for '' to '/'."

How to compare floating point number in shell scripting ? "a1 10.50
b1 10.01
if [ $a1 -gt $b1 ]
bc
then
echo ""greater""
else
echo ""lesser""
fi"

how to delete a word from a file using shell scripting??? 

"echo ""Other than the aesthetics there's the synthetics""
sed -e 's/word//g' filename
sed -e 's/apple//' sometext
echo ""Other than the aesthetics there's the synthetics""
sed 's/the//g'
echo ""Other than the aesthetics there's the synthetics""
sed 's///g'
echo ""Other than the aesthetics there's the synthetics""
sed 's/\//g'"

How to extract the second row of a text-file? 

"sed -n '2p' datafile
tail +2

head -1
sed -n '2p' datafile
awk '{print $2}' file_name
sed -n 2 2p file1
head -2 file.txt
tail -1"

How to compress files by using shell scripting? 

"To zip a single file use:-
$ gzip filename .
It will create a file named filename.gz .
To zip a folder first create a .tar file and then zip it.
Let the name of the folder be shell .
To create a .tar file use:-
$ tar cvf filename.tar shell/.
It will create a file named filename.tar
Now use the $ gzip filename.tar .
It will create a file named filename.tar.gz .
So in this way you can compress a file.
Note: To uncompress a file you can use gunzip command.
Fantastic explaination given above......
another way would be...
compress
which will create a filename.Z
and if we want the zip file to be in some other folder
compress -cf
to uncompress
use the following command...
uncompress
Though the explanations given are very good but in real time environment where the file size runs into MBs of data the proper way would be
1: If its a single file then gzip and compress and pack will serve the purpose. Out of these compress gives the more desired results
These commands use different compression algorithms take different amounts of time and reduce files by different amounts.
In general these commands can reduce the size of a file by 50-75 .
"

What is use of "cut" command ? 

Give some examples. 
Can we use "awk" or "sed" instead of "cut" ? 
If yes then give some examples? 
"This utility is use to cut out columns from a table or fields from each line of a file.
cut can be used as a filter.
Either the -b -c or -f option must be specified.
-b list
The list following -b specifies byte positions (for instance -b1-72 would pass the first 72 bytes of each line). When -b and -n are used together list is adjusted so that no multi-byte character is split.
-c list
The list following -c specifies character positions (for instance -c1-72 would pass the first 72 characters of each line).
-d delim
The character following -d is the field delimiter (-f option only). 

Default is tab. Space or other characters with special meaning to the shell must be quoted. delim can be a multi-byte character.
Cut - Utility used to cut/Strip out the required data/text from the source.
Cut can be used in three modes
Stripping by Character
cut -c 1-3
STriping by Byte length
cut -b -1-72
Stripping by delimiter and fields.
cut -d
-f1
where
-d
-> Delimiter used in input text to separate columns
-f1 -> Field/Column number
while processing Huge input files Cut's performance is far better than awk
Cut is a powerful command in Unix Cutting fields we can use the command for example we would like to extract first 3 col
cut -c1-3 sample (Sample is a file name)
To extract first and 3rd col
cut -d;-f1 -f3 sample


1. CUT is used to get the specific portion(column) of information from a files separated by a specific character(ex: CSV file)
ex: more /etc/passwd
cut -d"":"" -f1
The above command is used to list the user accounts on the server.


2. AWK
ex: ls -al
awk '{print $9}'
 

The above command is used to get the list of filenames in the current directory. Here the fields are need not be seperated by a specific charecter.


3. SED is similar to GREP command. The cut command cuts bytes characters or fields from each line of a file and writes these bytes characters or fields to standard output. If you do not specify the File parameter the cut command reads standard input.
To display fields using a blank separated list enter:
cut -f ""1 2 3"" -d : /etc/passwd
The cut command produces:
su:*:0
daemon:*:1
bin:*:2
sys:*:3
adm:*:4
pierre:*:200
joan:*:202
SED is a simple editor which will be editing files without a gui and to provide operations on the command line.sed -e s/word1/word2/g filenamehttp://en.wikipedia.org/wiki/SedAWK is a command line argument used for printing out values and arguments.

awk '{print $1}';http://en.wikipedia.org/wiki/awk
cut -d"":"" -f1 3 file
Delimitor "":""
Fields/Columns 1 and 3 are printed.
In this example awk can be used. sed can not be used.
awk can do all the jobs of cut. cut can give o/p byte wise which is not possible in awk."

How to find see the file which is created today, s date with time after 10 a.m to 5 p.m? 

"find . -atime 0 -type f -exec ls -ltr {} ; grep ""`date +%b %d`""
This command will show the files which are created today. 

But for ur second part of the question i need to analyse more,
Explanation:
find . -atime 0 -type f -exec ls -ltr {}
This will list the files where are accessed and created today.
grep ""`date +%b %d`"" This part of the command will filter out unwanted files which were not created today.
find . -atime 0 -type f -exec ls -ltr {} ;
grep `date + b d`
This command will show the files which are created today. But for ur second part of the quesiton i needto analyse more
Explanation:find . -atime 0 -type f -exec ls -ltr {}
This will list the files where are accessed and created today.
grep `date + b d`
This part of the command will filter out unwanted files which were not created today.
set -u -a
rm -f out2
find $PWD -atime 0 -type f -exec ls -ltr {} ;
grep `date '+ b d'`
tr -s
tr -d : > out2
 

echo -------Files Modified Today--------
cat out2
while read line
do
timechk `echo $line
cut -d -f8`
filename `echo $line
cut -d -f9`
if [ $timechk -gt 1000 -a $timechk -lt 1700 ]
then
echo $filename
fi
done
echo ----------------------------------- "

"Using Bourne shell : if you enter A B C D E F G.......................n after the command,
how will you write a programme to reverse these positional parameters? " 

"str=""alpha beta gamma""
echo $str
awk '{
for (i=NF; i>0; i--) printf (""%s "",$i);
printf(""n"");
}'
If you're talking about parameters passed into a Shell script, that can be modified to the following:
echo $*
awk '{
for (i=NF; i>0; i--) printf (""%s "",$i);
printf(""n"");
}'
The previous answer using ""awk"" reversed all the characters, resulting in:
$ echo $@
rev
i $#
while(($i> 0))
do
echo $i
((i $i-1))
done
str ""alpha beta gamma""
echo $str
awk '{
for (i NF; i>0; i--) printf ("" s "" $i);
printf(""n"");
}'
Results:
gamma beta alpha


If you're talking about parameters passed into a Shell script that can be modified to the following:
echo $*
awk '{
for (i NF; i>0; i--) printf ("" s "" $i);
printf(""n"");
}'"
 


Check if the directory exists [[ -e directoryname ]]

Check if the file exists [[-f filename]]


Check minutes from time `date +%M`

Sleep for # secs sleep #

Expressions expr

date in mm dd hour format date +%m%d%H

Change time for a file 

"touch -t





No comments:

Post a Comment