Hello Friends,
I have a question, i am trying to write a shell script in the bash shell.
#!/bin/sh
NAWK=/bin/nawk
AWK=/bin/awk
FIX_XML_PATH=/home/administrator/testfix/fix/
Y=`ls $FIX_XML_PATH | grep xml`
echo $Y
cat $Y
in this case when i do the echo $Y command it gives me the file name and when i do cat $Y, is give the error that not a file or directory.
What am i doing wrong can u please suggest?
Thanks
Adi:(
adi, can you tell me what is the value of Y. I think you are getting more than one filename. In that case, it may throw the error.
Can you run the script as
sh -vx <scriptname>
paste the logs that will help us in figuring out the error easily.
Quote the variable if you have spaces in your filename:
cat "$Y"
Regards
Scott
August 6, 2009, 10:25am
5
Hi.
This is probably because the script is not in the same directory as the xml files, so you need to add the path to the filenames.
One option is:
#!/bin/sh
NAWK=/bin/nawk
AWK=/bin/awk
FIX_XML_PATH=/root/tmp/XML/
Y=`ls $FIX_XML_PATH | grep xml`
echo $Y
echo $Y | awk -v D=$FIX_XML_PATH -v RS=" " '{system( "cat " D $1)}'
Or a better option is to change the line:
Y=`ls $FIX_XML_PATH/*.xml`
Hi,
I chaged the script a little bit,
#!/bin/sh
FIX_XML_PATH=/home/administrator/testfix/fix
FIX_FILE=`ls $FIX_XML_PATH | grep xml`
echo $FIX_FILE
cat $FIX_FILE
Variable $FIX_FILE store a xml file.
*******logs*******
$ sh -vx xmlParse.sh
#!/bin/sh
FIX_XML_PATH=/home/administrator/testfix/fix
+ FIX_XML_PATH=/home/administrator/testfix/fix
FIX_FILE=`ls $FIX_XML_PATH | grep xml`
ls $FIX_XML_PATH | grep xml
++ ls /home/administrator/testfix/fix
++ grep xml
+ FIX_FILE=fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-
0-2009B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
echo $FIX_FILE
+ echo fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-
09B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-02-00-2
TFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
cat $FIX_FILE
+ cat fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-0
9B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
cat: fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-02
B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml: No su
or directory
franklin, one interesting i found,
ad02 $ cat $a
10081551
10081599
10082234
10082259
20081134
20081159
30082232
10087721
kjlsab;kfd
kjbnaskj
kjabn;kj
akjldsb;k
akj;dhbsa
ad02 $ cat "$a"
cat: 0652-050 Cannot open aout.xml
bout.xml
cout.xml.
ad02 $ echo $a
aout.xml bout.xml cout.xml
ad02 $
here without quotes, it opens all the three files, but with quotes, it is throwing an error.
aigles
August 6, 2009, 10:38am
8
asirohi:
Hi,
I chaged the script a little bit,
#!/bin/sh
FIX_XML_PATH=/home/administrator/testfix/fix
FIX_FILE=`ls $FIX_XML_PATH | grep xml`
echo $FIX_FILE
cat $FIX_FILE
Variable $FIX_FILE store a xml file.
*******logs*******
$ sh -vx xmlParse.sh
#!/bin/sh
FIX_XML_PATH=/home/administrator/testfix/fix
+ FIX_XML_PATH=/home/administrator/testfix/fix
FIX_FILE=`ls $FIX_XML_PATH | grep xml`
ls $FIX_XML_PATH | grep xml
++ ls /home/administrator/testfix/fix
++ grep xml
+ FIX_FILE=fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-
0-2009B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
echo $FIX_FILE
+ echo fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-
09B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-02-00-2
TFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
cat $FIX_FILE
+ cat fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-0
9B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml
cat: fix_com.ibm.rational.clearcase.nt_i386.ifix0003_7.1.2.0000-7-1-0-02
B-TESTFIX-CC-D090803_for_7.1.2.0000-7-1-0-02-00-2009B-D090617.xml: No su
or directory
In the result of the ls command, the path is not present.
Modify your script :
#!/bin/sh
FIX_XML_PATH=/home/administrator/testfix/fix
FIX_FILE=`ls $FIX_XML_PATH | grep xml`
echo $FIX_FILE
cat $FIX_XML_PATH/$FIX_FILE
jean-Pierre.
thaks scottn, i got got that, i will keep in mind the way you told me.
Take care guys
-Adi
methyl
August 6, 2009, 10:47am
10
The output from "ls" does not include" the directory name.
A quick fix is to "cd" to the directory first.
Other correspondents have raised potential issues with getting multiple hits. We can only comment if we can see sample filename(s). Your code is written as if the string "xml" can appear anywhere in the filename and you are only expecting one file.
#!/bin/sh
FIX_XML_PATH=/home/administrator/testfix/fix
FIX_FILE=`ls $FIX_XML_PATH | grep xml`
cd ${FIX_XML_PATH}
echo $FIX_FILE
cat $FIX_FILE
For something that doesn't choke on multiple files or embedded spaces, use something like:
find $FIX_XML_PATH -type f -name "*xml*" | while file=$(line)
do
basename "$file"
cat "$file"
done
mac4rfree:
franklin, one interesting i found,
ad02 $ cat $a
10081551
10081599
10082234
10082259
20081134
20081159
30082232
10087721
kjlsab;kfd
kjbnaskj
kjabn;kj
akjldsb;k
akj;dhbsa
ad02 $ cat "$a"
cat: 0652-050 Cannot open aout.xml
bout.xml
cout.xml.
ad02 $ echo $a
aout.xml bout.xml cout.xml
ad02 $
here without quotes, it opens all the three files, but with quotes, it is throwing an error.
If there are more files the use of double quotes is not a good idea, but if you have file names with spaces in the variable then you have a problem. With double quotes the files are passed as one literal string, check the difference between:
cat "aout.xml bout.xml cout.xml"
and
cat aout.xml bout.xml cout.xml