LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-19-2004, 05:58 AM   #1
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Rep: Reputation: 52
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....

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....
 
Old 12-19-2004, 06:56 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 12-19-2004, 09:53 AM   #3
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
Hmm... That sort of makes sense after reading about it...

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!
 
Old 12-19-2004, 10:30 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 12-19-2004, 10:51 AM   #5
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
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... 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...
 
Old 12-19-2004, 07:55 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
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
 
Old 12-20-2004, 07:08 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
incidentally, it's NOT a bash script.

you have

Quote:
#!/bin/sh
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
problems creating a simple bootable cd to perform a simple task czarherr Linux - Software 1 11-11-2004 06:22 AM
Simple script problems... king_nothingzzz Programming 3 06-18-2004 11:04 PM
A Simple Script fiod Linux - General 3 09-20-2003 09:56 PM
Simple C Shell script is not so simple elconde Programming 2 09-17-2001 12:53 AM


All times are GMT -5. The time now is 10:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration