ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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?
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.
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/ ?
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.
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.
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.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
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 #.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.