LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problems with simple script.... (https://www.linuxquestions.org/questions/programming-9/problems-with-simple-script-267991/)

jong357 12-19-2004 04:58 AM

Problems with simple script....
 
Please be gentle. I know NOTHING about Bash Scripting. I promise I'll read up on it after someone holds my hand first.... :D

And there is an existing ~/Desktop/test.txt already.

Code:

#!/bin/sh
#
# Begin ~/Desktop/test

if [ -x ~/Desktop/test.txt ]; then
  cat >> ~/Desktop/test.txt << "EOF"
  This is the text I want to append
  EOF
fi

echo 'I'll run another command here if I figure out the above'

# End ~/Desktop/test

Thanks all.... I feel ashamed....:cry:

druuna 12-19-2004 05:56 AM

Hi,

First thing I noticed is the here-document structure that is not correct.

Although it looks nice to ident code in an if-then structure, you do need to use the here-document tokens exactly the way it was declared.

Code:

if [ -x ~/Desktop/test.txt ]; then
  cat >> ~/Desktop/test.txt << "EOF"
  This is the text I want to append
EOF
fi

For details on here-documents: man bash and look for the "Here Documents" section.

Hope this gets you going again.

jong357 12-19-2004 08:53 AM

Hmm... That sort of makes sense after reading about it... :scratch:

Atleast I'm getting something different.

[root@darkstar ~/Desktop] sh test
test: line 11: unexpected EOF while looking for matching `''
test: line 14: syntax error: unexpected end of file

That should be right. It said the dilemeter is the matching result of word only without the quotes and with no prepending spaces, so... yea. Your right. Still doesn't seem to be working.

Well, basically, I'm just lost. It doesn't have to append at the end of the file nor does it have to use an if/then structure. I simpily need to append some text to an existing file (cat is the only way I know how), wherever it goes (beginning/middle/end), and then I need to run another command after that... Man I'm stumped. I'll keep looking for literature but any additional help would ofcourse be welcome.

Thanks again!

druuna 12-19-2004 09:30 AM

Hi again,

There are some more 'syntax errors' in your script:

This: echo 'I'll run another command here if I figure out the above' won't work. The ' in I'll is seen as the closing pair for the ' that's behind echo. This is a better solution:

echo "I'll run another command here if I figure out the above"

You can also 'escape' the special character:

echo 'I\\'ll run another command here if I figure out the above'

I also expect that the use of -x is incorrect (are you looking for a directory or executable file??). If you want to check if the file already exists you should use the -a

Your script should look, something like this:

Code:

#!/bin/sh
#
# Begin ~/Desktop/test

if [ -a ~/Desktop/test.txt ]
then
  cat >> ~/Desktop/test.txt << "EOF"
  This is the text I want to append
EOF
fi

echo "I'll run another command here if I figure out the above"

# End ~/Desktop/test

You should do what you stated in your first post (reading about bash/scripting). I would suggest bying a good book about this subject as well.

Hope this clears things up a bit more.

jong357 12-19-2004 09:51 AM

Nice.... Thanks.

So, basically it was the EOF indent on Line 8 and the -x switch that was holding me up. I was stumbling to be sure. I knew my line 14 syntax error was something silly. I've caught myself using apostrophies in echo's more than once... :rolleyes: So, this works as expected then

Code:

#!/bin/sh
#
# Begin ~/Desktop/test

if [ -a ~/Desktop/test.txt ]; then
  cat >> ~/Desktop/test.txt << "EOF"
This is the text I want to append
EOF
fi

echo 'Run another command'

# End ~/Desktop/test

Thanks again! I really appreciate it.... Think I am going to buy myself a good bash book for Xmas... ;)

chrism01 12-19-2004 06:55 PM

Here's a free Bash scripting guide from TLDP (The Linux Doc Proj).
http://www.tldp.org/LDP/abs/html/index.html
enjoy + merry xmas :)

bigearsbilly 12-20-2004 06:08 AM

incidentally, it's NOT a bash script.

you have

Quote:

#!/bin/sh


All times are GMT -5. The time now is 10:00 PM.