Question regarding cat command

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

Some comments:

  • If you use bash, then start your script with /bin/bash.
  • NAWK and AWK are unused, they are distracting.
  • It is possible you are using a version of ls that add some characters to the file name that makes it unusable by cat.
  • It is also possible the xml filename contains blanks, in such case, you'll need to use double quotes at several places in your script.
  • mac4rfree suggestion is incorrect, multiple files are happily displayed by cat.

What says:

echo $Y | od -c

?

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.:mad:

*******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.

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

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

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