Trying to understand a complex bit of code


 
Thread Tools Search this Thread
# 1  
Trying to understand a complex bit of code

Hi,

To re-introduce myself, I'm a router guy trying to learn some scripting from the examples in my work place...

In a ksh script, one of the script guys wrote the following and I am trying to understand it. I'm hoping someone can explain it to me.

The script flow enters a case structure.
Inside the case, one of the options is as follows:
Code:
      @RESPONSE*) ADDRESPONSE=$(print "$cmd" | sed -e   's/^[   ]*@RESPONSE //')       
  [[ "$ADDRESPONSE" ==   "^M" ]] && \       
  
  				cat $WORKDIR/ADDPROMPTS | sed -e s/ADDRESPONSE/\\\r/g" >   $WORKDIR/ADDPROMPTS.tmp || \       
  
  cat $WORKDIR/ADDPROMPTS | sed -e   "s/ADDRESPONSE/${ADDRESPONSE}\\\r/g" > $WORKDIR/ADDPROMPTS.tmp

What I "get" is:
the first line removes the string "@RESPONSE //" from $cmd putting the new value in $ADDRESPONSE

I don't understand why the second line is in double bracket but this "seems to me" to be a short hand "if" statement with an "and"

and I would appreciate anyone who can explain what's happening in the two sed commands?

Thanks!

Marc
# 2  
If you've ever used 'if' in a shell script, you know what [ ] or [[ ]] statments do.

[ ] and [[ ]] are not even part of if -- they work just fine without it.

&&, || are short-form if then.

Code:
true && echo "This will print"
false && echo "This will not"

true || echo "This will not print"
false || echo "But this will"

# 3  
First, thanks for answering!

To be honest, I've only ever explicitly written if statements.
So the "[]" and "[[]]" without the "if" included in the code is new to me.

To make sure I have read your answer correctly, you are saying the code is equal to:

If $ADDRESPONSE equals ^M, execute the first "cat / sed" command
else execute the second "cat / sed" command ?

Marc
# 4  
Looks like it.

Code:
$ true && echo "command succeeded" || echo "command failed"
command succeeded

$ false && echo "command succeeded" || echo "command failed"
command failed

$

This User Gave Thanks to Corona688 For This Post:
# 5  
OK,

I still don't get why the "[[]]" rather than the "[]"

And then the sed commands.
It appears to me that the first one says:
Code:
   Take the contents of "$WORKDIR/ADDPROMPTS" and swap the string  
   value of $ADDRESPONSE"   for "\r" globally before saving it to 
   $WORKDIR/ADDPROMPTS.tmp

And the second one seems to say:
Code:
   Take the contents of "$WORKDIR/ADDPROMPTS" and swap the string  
   value of $ADDRESPONSE"   for "the string value of $ADDRESPONSE"
    - AND -"\r" globally before saving it to $WORKDIR/ADDPROMPTS.tmp

But I am not sure I am correct.

Marc
# 6  
Quote:
Originally Posted by Marc G
OK,

I still don't get why the "[[]]" rather than the "[]"
[[ ]] is an extended version of [ ]. Some shells don't have it.

I'd start by looking at what strings get input to it, and what strings come out of each half.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

help me to understand this code

Hi guys can you please help me to understand this code . tmpArray=(${line//=/ }) Please next time open a new thread in the appropriate forum and use code tags (6 Replies)
Discussion started by: sandhya.gilla
6 Replies

2. Shell Programming and Scripting

help can't understand the code

Hi All, Good day, i'm just wondering what is the meaning of this code? COUNT_EXTRACTED_FILE=`${ECHO_CMD} "${SE_IN_PATH}${SE_IN_FILE}" | ${AWK_CMD} -F"__" '{print $2}'` Thanks, -niks(newbie) (2 Replies)
Discussion started by: nikki1200
2 Replies

3. Programming

Need help with this c++ source code! DOnt understand what some stuff mean.

Okay so I am just starting programming c++. I just started started to red "C++ for Dummies yesterday and theres a lot of things I do not understand from this book and this source code especially. I will first post the full source code and then post questions about certain thing, usually what they... (2 Replies)
Discussion started by: orszhak
2 Replies

4. Shell Programming and Scripting

Need help to understand Awk code.

Hi Guys, Can someone please explain this code to me. I could figure out it's adding and comparing two fields but I am not sure which ones. sort -t"|" -k3.1 /tmp/mpcashqc.xtr| awk -F"|" '{CHECKAMT+=$3;BATCHTOT=$4;\ items++}END{for(i in CHECKAMT) if (CHECKAMT!=BATCHTOT)... (6 Replies)
Discussion started by: nua7
6 Replies

5. Shell Programming and Scripting

search 3 file and write to 4th file (a bit complex)

hi buddies; rollbackip.txt:10.14.3.65 2 10.14.3.65 3 ... lookup.txt: ... 10.14.3.65 2 10.14.5.55 1 55 10.14.6.66 1 66 10.14.3.65 3 10.14.7.77 3 77 10.14.8.88 2 88 10.14.9.99 4 99 ... ip-port.txt ... port111 3 10.14.5.55 57 port111 2 10.14.5.55 51 port111 1 10.14.5.55 59 ->... (7 Replies)
Discussion started by: gc_sw
7 Replies

6. UNIX for Dummies Questions & Answers

unable to understand a awk code

i am not able to understand the following code for awk: $awk -F"|" '{ kount++} >END { for (desig in kount) > print desig,kount }' emp.list the input file i.e. emp.list is :: 3432| p.k.agrwal |g.m |sales 4566|g.l.sharma |director|production 3433|r shah | g.m | production... (1 Reply)
Discussion started by: streetfi8er
1 Replies

7. Solaris

Porting C++ 32-bit code on 64-bit Solaris

Hi, I am trying to convert 32-bit code to 64-bit. I have defined function int main() { int* l; size_t len1; fun(len1); return 0; } void fun(int* ptr) { cout<<"\nsizeof(ptr)"<<sizeof(ptr); } However while compiling getting error as : Error: Formal argument ptr... (2 Replies)
Discussion started by: amit_27
2 Replies

8. UNIX for Advanced & Expert Users

Code Understand ?

Hey, Could you explain me what this code is doing ? For example: $i has value 9 $1 is the name of the file say FILEBkUp while do if $1.`/bin/expr $i - 1` ] then mv -f $1.`/bin/expr $i - 1` $1.$i fi i=`/bin/expr $i - 1` done Thanks... (2 Replies)
Discussion started by: varungupta
2 Replies

Featured Tech Videos