Need help to understand ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to understand ksh script
# 1  
Old 04-15-2011
Need help to understand ksh script

Hi All,

I have a ksh script & would like to understand mening of below lines in script,

Starting lines of script is as below,
Code:
#!/bin/ksh
#%W% %I% %D% %T% ---- ???
#%W%G --- ???
 
num_ctrl_files=0
OS=`uname`
if [ "$OS" = "SunOS" ]
then
//g' | egrep -v '(.sh:|.ksh:)' | sed 's/^.*://g' | sed 's/^M//g' | grep -v '^[ s]*#' | egrep -i '(.dat|.ctl)' | sed -e 's/^.*=//g' -e 's/,/ /g' -e 's/;/ /g' -e 's/"//g' | tr ' ' '\n' | sed 's/[    ]*//g' | sed '/^$/d' | sort -u | nawk -F"/" '{print $NF}' > $FILE

Please help me to understand above.

Last edited by Franklin52; 04-15-2011 at 05:22 AM.. Reason: Please use code tags
# 2  
Old 04-15-2011
Oh my... ok, let me give it a shot:
Code:
#!/bin/ksh
#%W% %I% %D% %T% ---- these are comments (starting with #)
#%W%G --- they don't do anything


The beginning of this line here:
Code:
//g'


Seems like something's missing there. To me it looks like the end of some global substitution using sed, something like
Code:
 sed 's/delete_this_pattern//g'

Furthermore, you don't have any input specified there... Where is the data, that these filters operate on, coming from? A file? Pipe?
It's gonna want input from stdin...
if statement needs to be closed with 'fi'.

Now for the filters alone, they don't look extremely difficult to understand.

[CODE]
egrep -v '(.sh:|.kshSmilie'[/CODE] is gonna skip lines that contain '.sh:' alebo '.ksh:'
The second filter
Code:
sed 's/^.*://g'

will erase everything before the last colon, including the colon itself. 'g' at the end is not needed.The third filter:
Code:
sed 's/^M//g'

will erase all characters "^M". (these can be seen as delimiters in some windows files.)

Fourth:
Code:
grep -v '^[ s]*#'

will skip (not print for further processing) lines that start with a space or letter 's' present zero or more times and a pound sign. E.g. a line like this:
Code:
sss ss s#something

would be skipped. The line
Code:
sss ttt ff#blah

would not be skipped (there are other chars before pound sign). The line has to contain the pound sign, to be skipped. The next filter
Code:
egrep -i '(.dat|.ctl)'

will keep only lines with '.dat' or .ctl, case insensitive (e.g. '.cTl' would be matched)

This long filter:
Code:

Code:
sed -e 's/^.*=//g' -e 's/,/ /g' -e 's/;/ /g' -e 's/"//g'


has four parts: 1. it erases everything before an equal sign, including.
2. changes all commas to spaces
3. changes all semicolons to spaces
4. erases all double quotes.

This filter
Code:
tr ' ' '\n'

Will change (translate) all spaces into newlines

This one
Code:
sed 's/[    ]*//g'

will erase all spaces (there are none by this time)
This
Code:
 sed '/^$/d'

will erase all empty lines.
And finally,
Code:
sort -u | nawk -F"/" '{print $NF}'


will sort the output lines, printing only the first occurence, if there are multiple same lines; and print only the part of the string after the last slash (-F"/" use slash as delimiter; print $NF, print last field).
The output is gonna be written into file $FILE, which, if exists is gonna get overwritten.

Search for "sed regular expressions" for further explanations.

This script is incomplete, and won't work as is.
This User Gave Thanks to mirni For This Post:
# 3  
Old 04-15-2011
Hi Mirni,

First up all thanks as lot for your quick reply to me.

Even I wondered about the same thing when I saw this code first time that from where it is getting input.

To put nmore focus on this, there is one function & into that function this code is present. Please see below code from begining,

Code:
sybSearch()
{
## changing current directory to that of the specified tree.
cd $TREE
cnt=`find ./ -name INSTALL\* -print -follow  2>/dev/null | wc -l | awk '{print $1}'`
if [ $cnt = 0 ]; then
        echo "\n============================================================================================" > $REPORT
        echo "There is no *INSTALL* file in this Package, please create one and resubmit your Package.." >> $REPORT
        echo "============================================================================================\n" >> $REPORT
        mailx -s "Scan Host: $HOST Tree: $TREE" "$MAILSTR" < $REPORT
        exit $VIOLATION
fi
## Preparing TEMP file
cat <<! > $TEMPSYB
[#]*use[#]*master[#]*$
[#]*use[#]*GDMGAdmin[#]*$
[#]*use[#]*GDMGSecurity[#]*$
[#]*use[#]*sybsecurity[#]*$
[#]*use[#]*sybsystemprocs[#]*$
[#]*use[#]*SEMSAuditDb[#]*$
sp_addlogin[#]*
sp_adduser[#]*
sp_addalias[#]*
sp_dropuser[#]*
sp_dropalias[#]*
sp_droplogin[#]*
sp_locklogin[#]*
sp_addgroup[#]*
sp_modifylogin[#]*
sp_changegroup[#]*
sp_addrole[#]*
sp_addrolemember[#]*
sp_droprole[#]*
^[#]*create[#]*role[#]*
^[#]*create[#]*database[#]*
[#]*grant.*to[#]*public[#]*
[#]*revoke.*from[#]*public[#]*
!
#[#]*grant.*to[#]*public[#]*$
#[#]*revoke.*from[#]*
###############################
#### Get list of all Tables
###############################
//g' | egrep -v '(.sh:|.ksh:)' | sed 's/^.*://g' | sed 's/^M//g' | grep -v '^[ /]*#' | egrep -i '(.dat|.ctl)' | sed -e 's/^.*=//g' -e 's/,/ /g' -e 's/;/ /g' -e 's/"//g' | tr ' ' '\n' | sed 's/[    ]*//g' | sed '/^$/d' | sort -u | awk -F"/" '{print $NF}' > $FILE
num_ctrl_files=0
OS=`uname`
if [ "$OS" = "SunOS" ]
then
//g' | egrep -v '(.sh:|.ksh:)' | sed 's/^.*://g' | sed 's/^M//g' | grep -v '^[ s]*#' | egrep -i '(.dat|.ctl)' | sed -e 's/^.*=//g' -e 's/,/ /g' -e 's/;/ /g' -e 's/"//g' | tr ' ' '\n' | sed 's/[    ]*//g' | sed '/^$/d' | sort -u | nawk -F"/" '{print $NF}' > $FILE
        num_ctrl_files=`wc -l $FILE | nawk '{print $1}'`
else
//g' | egrep -v '(.sh:|.ksh:)' | sed 's/^.*://g' | sed 's/^M//g' | grep -v '^[ s]*#' | egrep -i '(.dat|.ctl)' | sed -e 's/^.*=//g' -e 's/,/ /g' -e 's/;/ /g' -e 's/"//g' | tr ' ' '\n' | sed 's/[    ]*//g' | sed '/^$/d' | sort -u | awk -F"/" '{print $NF}' > $FILE
        num_ctrl_files=`wc -l $FILE | awk '{print $1}'`
fi

# 4  
Old 04-15-2011
It still looks incomplete. What is done with the file $TEMPSYB?
How is the function sybSearch() called?
# 5  
Old 04-15-2011
Please find below code where the function is getting called,

Code:
###################
# Main Body
###################
if [ $RDBMS = "Sybase" ]
then
sybSearch
awk '{
 if (index($0,">>>>CODE VIOLATION") > 0){i=1; t="\n"$0;}
 else
 {if (i==1){print t;i=0}; print $0;}
}' $TEMPRPT1 > $DDLSREPORT
fi

# 6  
Old 04-15-2011
That $TEMPSYB file, that looks like instructions to a database server. Have you found what is done with this file?
I suspect, it was passed on to the db server, and the output is to be run through the filters.
# 7  
Old 04-15-2011
I really don't understand how this <//g'> works !!!

One more thing ... when I executed the script with "set -x " this line starting with //g' executing something like as follows ... " //ged s/"
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help me understand this script

#!/bin/awk -f BEGIN {i=1;file="modified.txt"} { if ($0 !~ /^DS:/) {print $0 >> file} else { if ($0 ~ /^DS:/) {print "DS: ",i >> file;if (i==8) {i=1} else {i++}}; } } END {gzip file} Can someone explain to me how this above script works, I got it from a friend but not able... (3 Replies)
Discussion started by: Kamesh G
3 Replies

2. Shell Programming and Scripting

Need help to understand the below shell script

Please help me to understand the below 3 lines of code.execute shell in jenkins 1)APP_IP=$( docker inspect --format '{{ .NetworkSettings.Networks.'"$DOCKER_NETWORK_NAME"'.IPAddress }}' ${PROJECT_NAME_KEY}"-CI" ) 2)HOST_WORKSPACE=$(echo ${WORKSPACE} | sed... (1 Reply)
Discussion started by: naresh85
1 Replies

3. Shell Programming and Scripting

Help to understand a script

Hello world! Can someone please explain me how this code works? I'ts supposed to find words in a dictionary and show the anagrams of the words. { part = word2key($1) data = $1 } function word2key(word, a, i, x, result) { x = split(word, a, "") asort(a) ... (1 Reply)
Discussion started by: jose2802
1 Replies

4. Shell Programming and Scripting

Need help to understand this small script

Hi Guys, I need to understand below scipt:- -bash-3.00$ cat rsync-copy.ksh #!/usr/5bin/ksh batch <<%EOF% echo "/usr/local/bin/rsync --rsync-path=/usr/local/bin/rsync -a --stats /usr/openv/ /OpenvBCK" > openv.LOG # CG /usr/local/bin/rsync ... (6 Replies)
Discussion started by: manalisharmabe
6 Replies

5. Shell Programming and Scripting

Help me understand the Perl script..

#!/usr/bin/perl use strict; use warnings; print "Demo of array slicing \n"; my @abc="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"; my @a=@abc; my @random=@abc; my @comp=@abc; my @comp1=(@abc,"Hello",@abc); print "abc is @abc \n"; print "a is @a \n"; print "random is @random \n";... (1 Reply)
Discussion started by: dnam9917
1 Replies

6. Shell Programming and Scripting

Need to understand the script pls help

Can u please explain what it is doing #!/bin/sh fullyear=`/home/local/bin/datemmdd 1`"."`date +%Y` uehist=/u05/home/celldba/utility/ue/prod/history echo $fullyear cd $uehist ls -ltr pwroutages.master.$fullyear* | awk '{print $9}' > /u01/home/celldba/tmp/pwroutages_master_all_tmp while... (2 Replies)
Discussion started by: raopatwari
2 Replies

7. Shell Programming and Scripting

Help to understand the script

Hi All; Is there anybody can explain this script please? trap 'C_logmsg "F" "CNTL/c OS signal trapped, Script ${G_SCRIPTNAME] terminated"; exit 1' 2 trap 'C_logmsg "F" "Kill Job Event sent from the Console, Script ${G_SCRIPTNAME] terminated"; exit 1' 15 (3 Replies)
Discussion started by: thankbe
3 Replies

8. Shell Programming and Scripting

Can't understand the script

I am relatively new to Shell Scripting. I can't understand the following two scripts. Can someone please spare a minute to explain? 1) content s of file a are (021) 654-1234 sed 's/(//g;s/)//g;s/ /-/g' a 021-654-1234 2)cut -d: -f1,3,7 /etc/passwd |sort -t: +1n gives error (3 Replies)
Discussion started by: shahdharmit
3 Replies

9. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question