Meaning of script with echo, -d, -f1, -f2


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Meaning of script with echo, -d, -f1, -f2
# 1  
Old 02-19-2014
Meaning of script with echo, -d, -f1, -f2

Hello Friends,

I am a new learner of Unix & need to understand below script as start up,

Can anyone explain the meaning of each line listed below.

Thanks for your time.
Code:
#!/usr/bin/ksh
PARAMS=$1
#echo "parms passed is $PARAMS @"

STATUS=`echo ${PARAMS} | cut -d: -f1`
JOBNAME=`echo ${PARAMS} | cut -d: -f2`
EMAIL_ADDRESS=`echo ${PARAMS} | cut -d: -f3`

#echo "email address is ${EMAIL_ADDRESS}"

while [ $EMAIL_ADDRESS > '' ] 
do

  EMAIL_ADDRESS_1=`echo ${EMAIL_ADDRESS} | cut -d~ -f1`
  EMAIL_ADDRESS=`echo ${EMAIL_ADDRESS} | cut -d~ -f2- -s`
done


Last edited by Franklin52; 02-19-2014 at 09:56 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 02-19-2014
its not with echo , -d etc...
you should read :
Code:
STATUS=`echo ${PARAMS} | cut -d: -f1`

affect to the variable STATUS the the output of:
Code:
echo ${PARAMS} | cut -d: -f1`

which is an echo of another variable piped to cutcmd -with -d: -f1 as argument...
I suggest you look at cut manpages!!
# 3  
Old 02-19-2014
Quote:
Originally Posted by DK2014
I am a new learner of Unix & need to understand below script as start up,

Can anyone explain the meaning of each line listed below.
First off: you shouldn't, as a beginner, even look at this script: it is very poorly written and if it really does what it is supposed to do this is out of coincidence rather than planned.

Code:
#!/usr/bin/ksh

use this shell (KornShell) as commando processor

Code:
PARAMS=$1

This is supposed to put the first parameter passed on command line into the variable "PARAMS", but if this parameters contains blanks this line will fail because the variable declaration is not quoted. Correct would be:

Code:
PARAMS="$1"

Code:
#echo "parms passed is $PARAMS @"

This is a comment (like everything after "#" until the end of the line).

Code:
STATUS=`echo ${PARAMS} | cut -d: -f1`

This (and a lot of similar lines) is a waste of time and system resources. It executes a command (echo $PARAMS | cut -d: -f1) in a subshell ("`...`") and stuffs the output of this command into a variable STATUS. First, the backquotes used for the subshell are deprecated and shouldn't be used any more. Second, it too is not quoted like the first line and therefore prone to failure. To make the construct syntactically correct it should read:

Code:
STATUS="$(echo ${PARAMS} | cut -d: -f1)"

But the whole thing is unnecessary altogether, because what this is supposed to do - cut the content of "$PARAMS" from the beginning to the first ":"
- can be done without a subshell by simple parameter expansion:

Code:
STATUS="${PARAMS%%:*}"

The same goes for all the other lines analogously.

Code:
while [ $EMAIL_ADDRESS > '' ]
do
done

This is simply a syntax error, because [ $EMAIL_ADDRESS > '' ] won't work. Even if it would work, using "$EMAIL_ADDRESS" unquoted would cause a runtime error if the variable contains nothing. If it is supposed to mean "as long as "$EMAIL_ADDRESS" is not empty" (which is my suspicion) it will have to be written:

Code:
while [ -n "$EMAIL_ADDRESS" ]

Add to this that no attempt at error handling is made, parameters are not checked at all and the script has no output. I suggest you throw away this rubbish and try to write a script yourself. Even as a total beginner you will probably create something better than this.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 02-20-2014
Hello bakunin,

Thanks for the detail explanation. This does help me in understanding.

Actually we are migrating our projects from unix server to windows 2008 server, so there are few scripts which are written in unix shell which needs to be converted to Powershell compatible to windows 2008.

So first i need to understand the unix shell script & then convert it to powershell.

Any suggestions as what will be the best approach to do this.

Few more points if you can help,

1) In below 3 lines, what is the meaning of "cut -d: -f1", i mean what does "cut" do, what does "-d:" do & similarly what does "-f1" do.

Code:
STATUS=`echo ${PARAMS} | cut -d: -f1`
JOBNAME=`echo ${PARAMS} | cut -d: -f2`
EMAIL_ADDRESS=`echo ${PARAMS} | cut -d: -f3`

similarly what does "-f2", "-f3" do.

Is there any online link or PDF file where I can find the meaning of below,

1) cut
2) -d
3) -f
4) -d~
5) echo
6) exit
7) when to use {}
8) whats use of $
9) -z
10) -s
11) ls

Thanks again for your time.

Last edited by bakunin; 02-20-2014 at 06:29 AM..
# 5  
Old 02-20-2014
Quote:
Originally Posted by DK2014
Is there any online link or PDF file where I can find the meaning of below,
Much better: there is a command and a database on every UNIX system: "man". If you need information about "command", enter "man command" and this should bring up a general description and all the possible options and arguments for "command", along with some example usages.

To answer your question partly:

"cut" gets some input stream and extracts a certain piece, which it outputs:

Code:
<input stream> | cut <options> | <part of the input stream cut out>

The used options and arguments tell you how the input stream is cut:

"-d<character>" means "use this character as the field delimiter". Consider the input stream:

"ab:cd:ef:gh"

"cut -d:" would now mean that ":" is used to split the stream into pieces.

"-f<number>" means to extract the field with the number after splitting the stream into pieces. Using the above stream

Code:
echo "ab:cd:ef:gh" | cut -d: -f1

would yield "ab" (the first "field"), while

Code:
echo "ab:cd:ef:gh" | cut -d: -f3

would yield "ef". Now, compare this explanation with the content of "man cut" (and all the other commands you wanted to know) and you will see that i would have to write a whole lot of text to come close to the precision of these explanations.

Some hints, though:

"[ ...]": look under "man test". Why this is so has historical reasons.

"while", "read": internal shell commands, they do not have a man page of their own. see under "man ksh" and "reserved words".

I hope this helps.

bakunin
# 6  
Old 02-20-2014
Hi bakunin,

Thanks a lot for your guidance & explanation.

I have about 20 script files & all of them begins with "#!/usr/bin/ksh", this means its "The Korn shell".

I don't have access to the unix server as its on my client place & I just have to work on the test of the scripting part.

In order to find the meaning of all the commands & other things line variables, functions etc, please let me know what should i search on internet (some key words etc which can help me to quickly find these cmds etc).

Thanks again Smilie
# 7  
Old 02-20-2014
This forum contains manpages Smilie

Some basics about the Korn Shell: Man Page for ksh (all Section 1) - The UNIX and Linux Forums

Hope this helps
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Meaning of =~ in shell script

Please let me understand the meaning of following line in unix bash scripting .is =~ means not equal to or equal to . if ]; then echo -e "pmcmd startworkflow -sv ${INTSERV} -d ${INFA_DEFAULT_DOMAIN} -uv INFA_DEFAULT_DOMAIN_USER" \ "-pv INFA_DEFAULT_DOMAIN_PASSWORD -usdv... (2 Replies)
Discussion started by: harry00514
2 Replies

2. Shell Programming and Scripting

What is the meaning of ## in UNIX shell script?

Hi All, I am new to unix shell scripting and I was documenting one of the unix script and encountered below statements - for ii in `ls -1rt /oracle/admin/MARSCOPY/ext_files/fpm-ifpm/*.small.txt | tail -1 | awk '{print $1}'` do smallssim=${ii##/oracle/admin/MARSCOPY/ext_files/fpm-ifpm/}... (2 Replies)
Discussion started by: shuklajayb4
2 Replies

3. UNIX for Dummies Questions & Answers

UNIX Script - snipet meaning?

What would the below code snippet mean? my ($_configParam, $_paramValue) = split(/\s*=\s*/, $_, 2); $configParamHash{$_configParam} = $_paramValue; (2 Replies)
Discussion started by: MaKha
2 Replies

4. UNIX for Dummies Questions & Answers

Meaning of awk script

what does this mean? awk '!a||a>$1 {a=$1} END {for (i in a) print a,i}' file (6 Replies)
Discussion started by: osama ahmed
6 Replies

5. Shell Programming and Scripting

Whats the meaning of set -e inside the script

Hi, I would like to ask about the meaning or purpose of set -e in the script bash, Does it mean if a wrong command in the script it will close or exit the script without continuation thats what happen if i set it in the terminal. Thanks in advance (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Shell Programming and Scripting

printf/echo in a second script

This may be little confusing. I have Script1, which pulls data from the system and creates another script(lets say script2). While I run script1 I need to add printf/echo statements for script2, so that when I run script2 I see those statement. eg: script1 765 printf " display frame-$1 timeoffset... (2 Replies)
Discussion started by: miltonrods
2 Replies

7. Shell Programming and Scripting

echo prints nothing-shell script

could anyone tell me why when i execute the following script, echo returns blank set k = 1 echo $k (9 Replies)
Discussion started by: saman_glorious
9 Replies

8. UNIX for Dummies Questions & Answers

meaning of script

hello every one i want to know meaning of following line INST_PARA=$HOME/install/Install.Para SAVEMEDIUM=`awk '$2=="ArchiveSave"{print$4}' $INST_PARA` (4 Replies)
Discussion started by: kaydream
4 Replies

9. Shell Programming and Scripting

Using echo to create a script

Hello all, I want to be able to create a script on the fly from another script by echoing lines into a file, but am running into difficulty, as it isn't working right. What am I doing wrong? echo "for i in `grep $FRAME /root_home/powermt.sort.fil |awk '{print $7}'`" > pvtimout_set.sh... (5 Replies)
Discussion started by: LinuxRacr
5 Replies

10. Shell Programming and Scripting

Meaning of this line in script

Can anyone explain me the meaning of line #2 in these lines of shell script: if ; then ${EXPR} " ${MACTIONS } " : ".* ${ACTION} " >/dev/null 2>&1 || die "$USAGE" else Sorry in case this is a trivial thing (I am not an expert in this). (3 Replies)
Discussion started by: radiatejava
3 Replies
Login or Register to Ask a Question