LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-26-2008, 06:33 PM   #1
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Rep: Reputation: 15
continue a line of code to next line?


I think I do not understand how to continue a line of code from one line on to the next in a script file (not the command line). I have read to use a backslash with a newline caracter. I have tried:

last of this line \n
continuing on this next line

last of this line\n
continuing on this next line

last of this line \<newline>
continuing on this next line

last of this line\<newline>
continuing on this next line

Is one of these correct?
Either I am trying it wrong or there is a different problem with my script.

Also what I have read (I think) says its ok to use ; to run multiple commands on one line in a script file, not only on the command line. Is that correct?

Thanks for any help
 
Old 01-26-2008, 06:40 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by geeyathink View Post
I think I do not understand how to continue a line of code from one line on to the next in a script file (not the command line). I have read to use a backslash with a newline caracter. I have tried:

last of this line \n
continuing on this next line

last of this line\n
continuing on this next line

last of this line \<newline>
continuing on this next line

last of this line\<newline>
continuing on this next line

Is one of these correct?
Either I am trying it wrong or there is a different problem with my script.
For the newline character just hit enter. Enter is not displayed so only the \ will appear when the \ newline combination appears on the screen. Just don't leave a blank between \ and the enter.

Quote:
Originally Posted by geeyathink View Post

Also what I have read (I think) says its ok to use ; to run multiple commands on one line in a script file, not only on the command line. Is that correct?
Yes, that is correct.

--------------------
Steve Stites
 
Old 01-26-2008, 06:40 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
It would help if you post your real code and error messages or unexpected behavior you got.
 
Old 01-26-2008, 07:41 PM   #4
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
I would not actually use this as is for anything, but am trying to learn the commands that I would eventually use for my idea.


This works as expected on command line. I know it is counting and comparing properly because if I echo the <variables> then the correct numbers are printed as a result and when I change the -lt to -gt it changes the kdialog that opens.

[teabear@junker ~]$ hitsformeds=`grep -i -o -c -e pharmacy -e drug -e prescription -e prescriptions -e drugs -e refill -e medicine -e "blood pressure" /home/teabear/Desktop/redir2` ; hitsforputer=`grep -i -o -c -e computer -e programming -e computers -e network -e linux -e laptop /home/teabear/Desktop/redir2` ; if [ "$hitsforputer" -gt "$hitsformeds" ]; then kdialog -msgbox "file will be moved to Puter folder"; else kdialog -msgbox "file will be moved to Meds folder" ; fi
[teabear@junker ~]$


Then in a script file now I add backslashs and hit enter and get this:

sh hitsformeds=`grep -i -o -c -e pharmacy -e drug -e prescription -e prescriptions -e drugs -e refill -e medicine -e "blood pressure" \
/home/teabear/Desktop/redir2` ; hitsforputer=`grep -i -o -c -e computer -e programming -e computers -e network -e linux -e laptop \
/home/teabear/Desktop/redir2` ; if [ "$hitsforputer" -gt "$hitsformeds" ]; then kdialog -msgbox "file will be moved to Puter folder"; \
else kdialog -msgbox "file will be moved to Meds folder" ; fi

When I run the script file the same kdialog opens (Meds) no matter if I use -lt or -gt as the comparison.

Thanks for your help.
 
Old 01-27-2008, 03:32 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Not sure about what you mean by "sh histformeds=.." but the remaining of the syntax used in your example works fine with me.
 
Old 01-27-2008, 06:46 AM   #6
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
Thanks jlliagre,

The tutorial I read when I tried my first script said to put "sh" at the beginning of each file, so I did and it worked. Since then everything I have read says to put !#/bin/bash (or something similar)at the beginning of each file. I never changed out of laziness I guess. Because it worked I figured "sh" was a shortcut to bash or something. Should I change and use the full !#/bin/bash/ ?

Thanks for the help.
 
Old 01-27-2008, 07:21 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
You want #!/bin/bash not !#/bin/bash. The order matters. The first tutorial you saw may have meant to use #!/bin/sh at the top of every script. #!/bin/sh would call the older Bourne shell instead of Bash (the Bourne-again shell, get it?). The Bash shell is a later and more fully-featured version of the "sh" shell, and I think that many people used to recommend using the earlier version since it was more common and more portable. At this point, on many systems /bin/sh is just a symbolic link to /bin/bash anyhow. In any case, you want the number sign before the exclamation no matter what you are calling at the start of the script.

Take a look at this link for more information. The whole tutorial is very good: http://tldp.org/LDP/abs/html/sha-bang.html

Edit - Although I understand the desire to keep helpful command line runs as a single script file, I think that it would be a lot more readable for anyone else (and for you three months from now) if you don't try to keep the script as "one-line" this way.

Last edited by Telemachos; 01-27-2008 at 08:00 AM.
 
Old 01-27-2008, 08:25 AM   #8
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
Thanks Telemachos,

Thanks for the link, I already have it saved in bookmarks and have been trying to make the best use of the site, along with the few others that have been suggested in various post in this forum.

When I start to put together an actual script I will certainly try to use proper form. For now I wanted to make it easier to copy and paste back and forth between command line and script file for testing purpose. That may not be the best of ideas?

What I have been reading it has been hard for me to figure what differences should be expected between running a script from the command line and running it from a file. Anyone with pointers or link to that particular item?

I am going to try to put my script into proper form for a file and see if it works that way.

Thanks
 
Old 01-27-2008, 12:43 PM   #9
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
I put my script into (I think) proper form in a file and it still did not work.

So I thought to myself it must be making it to the "if" part of the script or it would not even be loading the "else" part.
That meant to me it wasn't reading the values of the variables properly for some reason. So, after the grep but before the "if then else"
I put the values of the original variables into new variables and used the new ones with the "if then else" and it started working properly
when ran from a file.

Now, seeing how this script acts differently from command line and file I hope it has popped into someones head "oh yea of course, its because of...."

I would like to know why this worked so I could plan for it in the future.

One little tidbit of help I saw given to someone else that was having a problem with a script was to "get a real editor" Is that critical?
Hopefully not but If so is kdeutils kedit any good?

Thanks

edit to add this is what is working, if I dont reassign the variables it doesn't work.
sh
hitsforputer=`grep -i -o -c -e computer -e programming -e computers -e network -e linux -e laptop /home/teabear/Desktop/redir2`
hitsformeds=`grep -i -o -c -e pharmacy -e drug -e prescription -e "blood pressure" /home/teabear/Desktop/redir2`
tryitlikethis=$hitsformeds
tryitlikethis2=$hitsforputer
if [ "$tryitlikethis" -lt "$tryitlikethis2" ]
then kdialog -msgbox "file will be moved to Meds folder"
else kdialog -msgbox "file will be moved to Puter folder"
fi

Last edited by geeyathink; 01-27-2008 at 12:51 PM.
 
Old 01-27-2008, 05:44 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Please post both the script that works and the script that doesn't.
Enclose the scripts with code tags for better readability.
Post the whole code, including the first line. The last one you posted is still bogus (single sh on a line which would just launch an interactive subshell.
 
Old 01-27-2008, 06:30 PM   #11
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
This works from a file:

Code:
sh
hitsforputer=`grep -i -o -c -e  computer -e programming -e computers -e network -e linux  -e laptop /home/teabear/Desktop/redir2`
hitsformeds=`grep -i -o -c -e  pharmacy -e drug -e prescription -e "blood pressure" /home/teabear/Desktop/redir2`
tryitlikethis=$hitsformeds
tryitlikethis2=$hitsforputer
if [ "$tryitlikethis" -gt "$tryitlikethis2" ]
then kdialog -msgbox "file will be moved to Meds folder"
else kdialog -msgbox "file will be moved to Puter folder"
fi
exit
This does not work from a file:

Code:
sh
hitsforputer=`grep -i -o -c -e  computer -e programming -e computers -e network -e linux  -e laptop /home/teabear/Desktop/redir2`
hitsformeds=`grep -i -o -c -e  pharmacy -e drug -e prescription -e "blood pressure" /home/teabear/Desktop/redir2`
if [ "$hitsformeds" -gt "$hitsforputer" ]
then kdialog -msgbox "file will be moved to Meds folder"
else kdialog -msgbox "file will be moved to Puter folder"
fi
exit
By works I mean I click on the file on my desktop and the correct kdialog opens. The file is set to executable and named testgrep.sh

From what I have just read today, on modern linuxes, sh is just a link to the default and mine is Bash. This seems to run the exact same way when I switched and used !#bash at the beginning.

Thanks,
 
Old 01-29-2008, 10:24 AM   #12
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
I assume that you are now clear about the use of the line continuation character (the same in a script as on the command line) as explained by Jailbait.

The shabang #! (hash pling):
In a script, the very first line, if it begins with #! indicates to the command line interpreter what command should be used to process the script that follows in subsequent lines. That is if the script has been executed as
Code:
./test
or
test (assuming that the script is in the current path)
obviously if you say
Code:
sh test
or
bash test
then you have already told the interpreter what command you wish the script to be processed with and the shebang directive will be ignored.

This was the method used by Unix to automaticaly determine how a program script would be run, file extensions having no meaning and therefore the concept of file associations did not exist.

I hope that makes the #! clearer, if only to ensure that you type it the right way round. Think of it as a special comment. As you know comments all begin with #.

Last edited by PAix; 01-29-2008 at 10:26 AM.
 
Old 01-29-2008, 11:05 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by geeyathink View Post
Code:
sh
...
As already stated, this first line is incorrect. Please replace it by
Code:
#!/bin/bash
Quote:
Code:
...
exit
Your last line, while not incorrect is unnecessary. The script will exit anyway at the end of file.

I don't see any reason that would explain a difference in behavior between both variants.

You can add these lines at the beginning of the scripts (just after the shebang line) to get a trace file and see what really happens.
Code:
exec 2>/tmp/script1.log
set -x
Replace script1.log by script2.log in the second script source code.

Then run both of them from your desktop and post the log files.
 
Old 01-29-2008, 05:01 PM   #14
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
Thank you all for helping me learn, I know your time is valuable.

I kept working on this last night and have moved the word lists to files instead of in the code. Also am adding more lists, but the issue is still the same.

Code:
#!/bin/bash
exec 2>/tmp/script1.log
# edit any indvidual list in the indvlist folder to change what words are counted for each category

hitsfortravel=`grep -i -o -c -f /home/teabear/bashwork/indvlist/travellist /home/teabear/bashwork/redir2`
hitsforputer=`grep -i -o -c -f /home/teabear/bashwork/indvlist/puterlist /home/teabear/bashwork/redir2`
hitsformeds=`grep -i -o -c -f  /home/teabear/bashwork/indvlist/medlist /home/teabear/bashwork/redir2`
mhit=$hitsformeds
phit=$hitsforputer
thit=$hitsfortravel
#
if [ "$mhit" -gt "$phit" ] && [ "$mhit" -gt "$thit" ]
then kdialog -msgbox "file will be moved to Meds folder"
  elif [ "$phit" -gt "$mhit" ] && [ "$phit" -gt "$thit" ]
  then kdialog -msgbox "file will be moved to Puter folder"
    elif [ "$thit" -gt "$phit" ] && [ "$thit" -gt "$mhit" ]
    then kdialog -msgbox "file will be moved to Travel folder"
else kdialog -msgbox "file will not be moved"
fi
exit

script1.log was created but was empty.


Code:
#!/bin/bash
exec 2>/tmp/script2.log
# edit any indvidual list in the indvlist folder to change what words are counted for each category

hitsfortravel=`grep -i -o -c -f /home/teabear/bashwork/indvlist/travellist /home/teabear/bashwork/redir2`
hitsforputer=`grep -i -o -c -f /home/teabear/bashwork/indvlist/puterlist /home/teabear/bashwork/redir2`
hitsformeds=`grep -i -o -c -f  /home/teabear/bashwork/indvlist/medlist /home/teabear/bashwork/redir2`
#mhit=$hitsformeds
#phit=$hitsforputer
#thit=$hitsfortravel
#
if [ "$mhit" -gt "$phit" ] && [ "$mhit" -gt "$thit" ]
then kdialog -msgbox "file will be moved to Meds folder"
  elif [ "$phit" -gt "$mhit" ] && [ "$phit" -gt "$thit" ]
  then kdialog -msgbox "file will be moved to Puter folder"
    elif [ "$thit" -gt "$phit" ] && [ "$thit" -gt "$mhit" ]
    then kdialog -msgbox "file will be moved to Travel folder"
else kdialog -msgbox "file will not be moved"
fi
exit

script2.log
/home/teabear/bashwork/workinggrep.sh: line 13: [: : integer expression expected
/home/teabear/bashwork/workinggrep.sh: line 15: [: : integer expression expected
/home/teabear/bashwork/workinggrep.sh: line 17: [: : integer expression expected

Last edited by geeyathink; 01-29-2008 at 05:04 PM.
 
Old 01-29-2008, 05:15 PM   #15
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
Forget the above post, I forgot to change the variable names for the second one, Ill go do it now. Sorry
 
  


Reply



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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
How much line of code kernel have? alan_ri Linux - General 6 01-11-2008 01:34 PM
line of code indiancosmonaut Solaris / OpenSolaris 5 08-29-2007 04:25 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration