# Error while executing a script

**URL:** <https://community.unix.com/t/error-while-executing-a-script/250866>\
**Category:** Shell Programming and Scripting\
**Created:** [October 8, 2009, 3:58am UTC](https://community.unix.com/t/error-while-executing-a-script/250866 "2009-10-08T03:58:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![shwetainnani](https://community.unix.com/letter_avatar/shwetainnani/32/5_5575768a8748004e209b776fc1b2916d.png) [@shwetainnani](https://community.unix.com/u/shwetainnani)\
**Post date:** [October 8, 2009, 3:58am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/1 "2009-10-08T03:58:51Z")

</div>

My script is as below :

```nohighlight
#!/bin/sh
for line in `cat Results.txt`
do
FEILD1=`echo $line |awk -F"|" '{print $1}'`
FEILD2=`echo $line |awk -F"|" '{print $2}'`
FEILD3=`echo $line |awk -F"|" '{print $3}'`
FEILD4=`echo $line |awk -F"|" '{print $4}'`
echo "$FEILD1 $FIELD2 $FIELD3 $FIELD4"
done 

```

Hi All,

I am trying to execute the above script. Ideally I shud get all that before the fisrt '|' in $1 anything before secon pipe and after first pipe in $2 etc. But the echo of my result is not as expected could you please help.

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [October 8, 2009, 4:14am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/2 "2009-10-08T04:14:47Z")

</div>

Might be the fact that you assign the values to FEILDx variables, but try to print FIELDx variables?

That said, this script can be written just as easily as

```nohighlight
awk -F '|' '{print $1,$2,$3,$4}' Results.txt

```

---

<div class="post-metadata">

**Author:** ![thegeek](https://community.unix.com/letter_avatar/thegeek/32/5_5575768a8748004e209b776fc1b2916d.png) [@thegeek](https://community.unix.com/u/thegeek)\
**Post date:** [October 8, 2009, 4:21am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/3 "2009-10-08T04:21:00Z")

</div>

It seems a simple typo..

FEILD2 & FIELD2

---

<div class="post-metadata">

**Author:** ![shwetainnani](https://community.unix.com/letter_avatar/shwetainnani/32/5_5575768a8748004e209b776fc1b2916d.png) [@shwetainnani](https://community.unix.com/u/shwetainnani)\
**Post date:** [October 8, 2009, 5:31am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/4 "2009-10-08T05:31:56Z")

</div>

but I need to use the FEILD 1, 2 ,3 etc as parameters further and hence I have to store them somewhere

---------- Post updated at 04:31 AM ---------- Previous update was at 03:22 AM ----------

Hi Thanks

I corrected the typo still there is something wrong

My Results.txt contains  
374|Jane - Sensitivities (EUI8)|SYCEUI8\_yyyyMMdd\_RISK.CSV, SYCEUI8\_yyyyMMdd\_PCA.CSV, RMMEUI8\_yyyyMMdd\_ORIGINAL\_Details.CSV|3  
472|Jane - Sensitivities (PRP2)|SYCPRP2\_yyyyMMdd\_RISK.CSV, SYCPRP2\_yyyyMMdd\_PCA.CSV, RMMPRP2\_yyyyMMdd\_ORIGINAL\_Details.CSV|3

So my feilds shud look like  
FEILD 1 = 374  
FEILD 2 =Jane - Sensitivities (EUI8)|  
FEILD 3 = SYCEUI8\_yyyyMMdd\_RISK.CSV, SYCEUI8\_yyyyMMdd\_PCA.CSV, RMMEUI8\_yyyyMMdd\_ORIGINAL\_Details.CSV  
FEILD 4 = 3

which is not happening at the moment. Can some one please help. i have to send this code for UAT by tomorrow . Please help

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [October 8, 2009, 5:40am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/5 "2009-10-08T05:40:12Z")

</div>

Show us the output of your script.

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![shwetainnani](https://community.unix.com/letter_avatar/shwetainnani/32/5_5575768a8748004e209b776fc1b2916d.png) [@shwetainnani](https://community.unix.com/u/shwetainnani)\
**Post date:** [October 8, 2009, 5:44am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/6 "2009-10-08T05:44:03Z")

</div>

Output is as below  
374 Jane  
-  
Sensitivities  
(EUI8) SYCEUI8\_yyyyMMdd\_RISK.CSV,  
SYCEUI8\_yyyyMMdd\_PCA.CSV,  
RMMEUI8\_yyyyMMdd\_ORIGINAL\_Details.CSV 3  
472 Jane  
-  
Sensitivities  
(PRP2) SYCPRP2\_yyyyMMdd\_RISK.CSV,  
SYCPRP2\_yyyyMMdd\_PCA.CSV,  
RMMPRP2\_yyyyMMdd\_ORIGINAL\_Details.CSV 3

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [October 8, 2009, 5:45am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/7 "2009-10-08T05:45:47Z")

</div>

Not sure what your problem is, but the following works for me:

```nohighlight
#! /bin/bash

cat results.txt | \
while read LINE
do
  F_1=$( echo $LINE | awk -F '|' '{ print $1 }' )
  F_2=$( echo $LINE | awk -F '|' '{ print $2 }' )
  F_3=$( echo $LINE | awk -F '|' '{ print $3 }' )
  F_4=$( echo $LINE | awk -F '|' '{ print $4 }' )
  echo "F_1 = '$F_1'"
  echo "F_2 = '$F_2'"
  echo "F_3 = '$F_3'"
  echo "F_4 = '$F_4'"
done

exit 0

# [game over]

```

```nohighlight
[house@universe] sh test.bash
F_1 = '374'
F_2 = 'Jane - Sensitivities (EUI8)'
F_3 = 'SYCEUI8_yyyyMMdd_RISK.CSV, SYCEUI8_yyyyMMdd_PCA.CSV, RMMEUI8_yyyyMMdd_ORIGINAL_Details.CSV'
F_4 = '3'
F_1 = '472'
F_2 = 'Jane - Sensitivities (PRP2)'
F_3 = 'SYCPRP2_yyyyMMdd_RISK.CSV, SYCPRP2_yyyyMMdd_PCA.CSV, RMMPRP2_yyyyMMdd_ORIGINAL_Details.CSV'
F_4 = '3'

```

---

<div class="post-metadata">

**Author:** ![shwetainnani](https://community.unix.com/letter_avatar/shwetainnani/32/5_5575768a8748004e209b776fc1b2916d.png) [@shwetainnani](https://community.unix.com/u/shwetainnani)\
**Post date:** [October 8, 2009, 6:57am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/8 "2009-10-08T06:57:04Z")

</div>

Thats great this works thanks for the help but still not sure why it didnt work in my for loop. Anyways thanks so much for the help

---------- Post updated at 05:57 AM ---------- Previous update was at 04:54 AM ----------

Thanks to the qucik help on the group. I could do much of my coding from the pointers given. I have another question

If I want to just separate the parts coming in feild F\_3 i.e. say F\_3 = 'SYCPRP2\_yyyyMMdd\_RISK.CSV, SYCPRP2\_yyyyMMdd\_PCA.CSV, RMMPRP2\_yyyyMMdd\_ORIGINAL\_Details.CSV'

I mean I want to extarct whatever is coming before the comma and pass it as parameters how do I go about it.

I tried using cut but that didnt help much as number of chars coming before comma may vary. Any quick help would be highly appreciated

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [October 8, 2009, 7:05am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/9 "2009-10-08T07:05:52Z")

</div>

```nohighlight
F_3_1=$( echo $F_3 | awk -F ',' '{ print $1 }' )

```

---

<div class="post-metadata">

**Author:** ![shwetainnani](https://community.unix.com/letter_avatar/shwetainnani/32/5_5575768a8748004e209b776fc1b2916d.png) [@shwetainnani](https://community.unix.com/u/shwetainnani)\
**Post date:** [October 9, 2009, 12:45am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/10 "2009-10-09T00:45:10Z")

</div>

Hi

I have added a if loop to the script viz

```nohighlight
if [`echo $F_4` -eq 3]
then
{
echo "Two FW Jobs"
---
----
---
} 
else 
{
----------
--------
} 
fi 

```

but this is not working it gives the message ./Automation\_2.sh[29]: [3: not found  
when I run this. Could you please help.

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [October 9, 2009, 1:16am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/11 "2009-10-09T01:16:55Z")

</div>

In the line

```nohighlight
if [`echo $F_4` -eq 3]

```

You need to put at least one whitespace on each side of each of the square brackets, otherwise it won't be interpreted as the 'test' instruction.

---

<div class="post-metadata">

**Author:** ![shwetainnani](https://community.unix.com/letter_avatar/shwetainnani/32/5_5575768a8748004e209b776fc1b2916d.png) [@shwetainnani](https://community.unix.com/u/shwetainnani)\
**Post date:** [October 9, 2009, 1:19am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/12 "2009-10-09T01:19:31Z")

</div>

I will take care of these instrucitons going forward. BTW thanks it worked now after adding white spaces

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [October 9, 2009, 2:02am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/13 "2009-10-09T02:02:07Z")

</div>

You don't need to use echo in your if statement :

```nohighlight
if [$F_4 -eq 3]

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [October 9, 2009, 5:44am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/14 "2009-10-09T05:44:18Z")

</div>

Something I've been wondering for months:  
Where does this construct come from?

> [@](#):
>
> for i in `command that produces list`  
> do
> 
> done

I've never seen it in a commercial script and never seen it in a book or manual. It does however crop up at least twice a week on this board.

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [October 9, 2009, 6:02am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/15 "2009-10-09T06:02:21Z")

</div>

> [@methyl](#):
>
> Where does this construct come from?

Maybe from the ["ABS"](http://tldp.org/LDP/abs/html/loops1.html) (- see example 10.7 et sqq.) ...?

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [October 9, 2009, 9:12am UTC](https://community.unix.com/t/error-while-executing-a-script/250866/16 "2009-10-09T09:12:03Z")

</div>

Thanks dr.house . Overall that site doesn't look too bad if you only write for bash. Most of the time they avoid constructs which easily break like:

```nohighlight
for i in `ls -1R`

```

---------- Post updated at 14:12 ---------- Previous update was at 12:50 ----------

Back on topic.

The original problem with:

```nohighlight
for line in `cat Results.txt`

```

The "cat" executes first and create a very wide "for line in" statement with lots of fields made up from the entire contents of Results.txt:  
The field separator in "for" is a space character.  
The "for" loop executes once for every space separated field. Thus $line never contains the whole record. In fact $FIELD2 only contains something if the contents of $line happen to contain a pipe character.
