Understanding Statements in Script

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Understanding Statements in Script
# 1  
Old 07-27-2009
Understanding Statements in Script

Hello All,
I am trying to understand line by line in the existing shell script, could you please help me out understanding the code.

1) i was trying execute the below code replacing the parameter with the real file value, but its throwing me an error.

`echo $line | cut -d "," -f4`

Translated shell command of above cut statement in shell script
GOPSmasterfile.csv | cut -d "," -f4

2) Could you please let me know what would the output or how exactly the commands work concatenated with "|" symbol or how the output or control transfers from commands in a single statement, i was referring to statements 3 & 5.

Below is the original code
while read  line
x=`echo $line | cut -d "," -f4`
cd $x
plant=`echo $line | grep $x  | cut -d "," -f1 | head -1 `
done < $master_file

Appreciate your time and help, Thank you!

# 2  
Old 07-27-2009
1. while read  line
2. do
3. x=`echo $line | cut -d "," -f4`
4. cd $x
5. plant=`echo $line | grep $x  | cut -d "," -f1 | head -1 `
6. done < $master_file

the 3rd line does the following:
echo $line -> Display the line
The displayed line is passed to a PIPE "|". This pipe streams the bytes from the echo command to the cut command.
Hence, the cut command cuts the 4th Field seperated by a delimter ','.
This is equivalent to:
Say your Line is "Name,Age,City,Country,Pincode"
echo $line | cut -d "," -f4
=> echo "Name,Age,City,Country,Pincode" | cut -d "," -f4
=> Country
As Country is the 4th Field separated by a comma.

Similarly, the 5th Line:
plant=`echo $line | grep $x | cut -d "," -f1 | head -1
Here we are executing 4 commands using 3 pipes...the output of the left command being passed onto the right as an input.

echo $line (Display a line) -> grep $x (Find a character in this line) -> cut -d "," -f1 (Cut the 1st field from this line seperated by a comma) -> Give the First line from this output.

Please note that as per your script $x = the output of inital command which I am assuming is a directory name.

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

Multiple expect/send statements not working in shell script

Hi I am trying the following in my bash script which logs into my machine and runs a command. Trying to solve this using expect. The first expect statement is hit and it enters the address "" but when the second expect statement is hit it exits #!/bin/bash expect -c ' spawn... (2 Replies)
Discussion started by: skorada
2 Replies

2. Programming

Shell script - if statements dont work

hi all, i have made a shell script and it runs until it reaches the if statement, doesn't the ! mean only if the command fails it will echo me that message and then exit can anyone please help me what is wrong with my code? many thanks, rob #!/bin/bash echo "is this archive... (10 Replies)
Discussion started by: robertkwild
10 Replies

3. Shell Programming and Scripting

Help understanding the script

Could someone please help me in understanding the code below: #!/usr/bin/ksh Month=`date|cut -c5-7` Day=`date|cut -c9-10` Year=`date|cut -c27-28` Rom2Jul() { case $Month in Feb) Day=$(( $Day+31 ));; Mar) Day=$((... (27 Replies)
Discussion started by: hasn318
27 Replies

4. Shell Programming and Scripting

Simplified awk script for if else statements

Hi, The below awk script that i did is working fine. It gives me the results that i want. But, the script is not smart and very long as i have 8 conditions to meet. The sample script below only show 2 conditions. awk 'BEGIN{FS=OFS=" ~ |\t"} {if (($7>$9) && ($6>$8)){ Ql= $7-$6; Sl=... (6 Replies)
Discussion started by: redse171
6 Replies

5. Shell Programming and Scripting

Can we use two shebang statements in a single shell script?

Hi, As per my understanding, we can use two shebang statements in a single shell script. Please see below snippet- #!/bin/bash .......## some code A #!/bin/csh .......## some code B exit 0; Here, code A will be executed using bash shell and code B will be executed with c shell. ... (9 Replies)
Discussion started by: tarunmudgal4u
9 Replies

6. Shell Programming and Scripting

Need help understanding this script.

Can someone explain what is happening line by line in this script, particularly after the do statement. The script works, it renames all the files in my directory that has a date in the file name. But I would like to know more about it. #!/bin/bash newdate=12-10-1995 for file in *--* do ... (6 Replies)
Discussion started by: Harleyrci
6 Replies

7. Shell Programming and Scripting

Multiple if statements in script and unexpected end of file

Two things... 1) If I have an if statement that is true I want it to exit without running the rest of the file, but if it is false I want it to continue.. I just did if ; then exit else echo "" fi Can that work? I want it to exit if $1 is 0, and if not to continue running the rest of the... (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

9. Shell Programming and Scripting

Python script - control flow statements

Hi guys.I'm just beginner of python. I'm just trying to do some analysis on simple input file. it has 6 columns and i want to consider k,l and m,n if i and j are + after that checking which value is greater or lower in k,l and m,n I have included logic header just to explain what I was... (4 Replies)
Discussion started by: repinementer
4 Replies

10. Shell Programming and Scripting

Running remote shell script containing sql statements

I have a shell script which resides on three SCO machines containing some simple sqlplus statments. I need to run these scripts remotely. Currently, I am trying to use rsh to do so: rsh hostname myscript args The problem is that the arguments to the sqlplus statements in the remote shell... (4 Replies)
Discussion started by: Madbreaks
4 Replies
Login or Register to Ask a Question