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 |
Quote:
Quote:
-------------------- Steve Stites |
It would help if you post your real code and error messages or unexpected behavior you got.
|
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. |
Not sure about what you mean by "sh histformeds=.." but the remaining of the syntax used in your example works fine with me.
|
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. |
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. |
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 |
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 |
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. |
This works from a file:
Code:
sh Code:
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, |
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 Code:
sh test 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 #. |
Quote:
Code:
#!/bin/bash Quote:
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 Then run both of them from your desktop and post the log files. |
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 script1.log was created but was empty. Code:
#!/bin/bash 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 |
Forget the above post, I forgot to change the variable names for the second one, Ill go do it now. Sorry
|
All times are GMT -5. The time now is 04:06 AM. |