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.
Right on. The simple explanation for EOF not working is that linuxquestions copy/paste will convert TABS to SPACES. <<-EOF will only remove TABS.
You just have to either move EOF to the beginning of the line like this:
EOF
or put in TABS instead of SPACES in your favorite editor:
<TAB><TAB>EOF
fixed it by removing spaces and adding tabs, but it does not increment the counts or minus (does not do the math), nor does tabs move it off the left side of the terminal, which the later is not that big of a deal.
Code:
cat <<-EOF
MAX is $max
count is $((++count))
--------
$((max - count))
EOF
I wouldnt put the increment inside a HEREDOC. Try something more like this untested psuedo example code:
Code:
count=0 #initialize outside of loop
while read -r line
do
$((++count)) #increment outside HEREDOC, but inside loop
cat <<-EOF
count is $count
EOF
done < file
I wouldnt put the increment inside a HEREDOC. Try something more like this untested psuedo example code:
Code:
count=0 #initialize outside of loop
while read -r line
do
$((++count)) #increment outside HEREDOC, but inside loop
cat <<-EOF
count is $count
EOF
done < file
Code:
In computing, a here document (here-document, here-text, heredoc, hereis, here-string or
here-script) is a file literal or input stream literal: it is a section of a source code
file that is treated as if it were a separate file.
this is workking fine though.
Code:
echo "MAX is $max"
echo "count is $((++count))"
echo " --------"
echo " $((max - count))"
My apologies, I had not tested it and did not realise that the content would not increase or calculate
The echo is just as good, maybe better in this case, but as to the indenting, as mentioned by szboardstretcher, all tabs are removed, so if you need to indent something inside the heredoc when using
the -EOF version, you need to supply a space prior to the the indentation.
Code:
cat <<-EOF
<tab>First line
<tab><sp><tab>next line
EOF
The second line above would now effectively place 'next line' a tab distance from the edge.
My apologies, I had not tested it and did not realise that the content would not increase or calculate
The echo is just as good, maybe better in this case, but as to the indenting, as mentioned by szboardstretcher, all tabs are removed, so if you need to indent something inside the heredoc when using
the -EOF version, you need to supply a space prior to the the indentation.
Code:
cat <<-EOF
<tab>First line
<tab><sp><tab>next line
EOF
The second line above would now effectively place 'next line' a tab distance from the edge.
yeah that worked, copy paste fails at times. lol
I've never ventured into the meat of BASH. I was just sticking with what I leanred. pot shoting as you seen mixing different methoids. I just kept seeing others use that one liner and liked it. so I wanted to see if I could put it to use in this script.
Code:
[ true ] && do something (else) do something else
which equated to
Code:
[ true ] && do someting ; || do something else
then someone else in here pointed out that
Code:
mkdir -p
already test to see if it is there or not, so that widded it down even further to two lines of code for that code block I was using.
Now it is like amost nothing compaired to what I started out with. which was even less then the other scripts I wrote to deal with my mp3's.
I do enjoy changing that while loop getting rid of that pipe for sure.
Thanks to you and all of the others in this post, points given to everyone.
if command1 then command2 else command3
# and
[ command1 ] && command2 || command3
are not the same, not equal to each other, you must not mix these two different constructs.
construct
Code:
if true && do this ; || (or) if not true do this
(true?do this:not true does this)
[true] && do this || not true does this
if true then do this else it is not true so do this instead
I do not know what you are talking about seeings how you did not define what you said to prove your theory that they are not the same.
it is no off - yes no logic
Ternary operator ( ?: ) in Bash is not supported per se'
Code:
(true?do this:not true does this)
so its equivalent is
Code:
[ variable = condition (which evaluates to a true statement) ] && value_if_true || value_if_false
replaces if then else statements
Code:
if [ true ] ; then
value_if_true
else
value_if_false
fi
no, they are not the same. I have already tried to explain it here, on LQ, but here is it again (in short):
using if command1 then command2 else command2 the execution of command2 and command3 depends only on the result of command1
using [ command1 ] && command2 || command3 the execution of command3 may depend on both command1 and command2.
that is definitely a difference.
So even though your first command is true, we can make the "OR" fire off. Which is wrong. That is not If, Then, Else. That is 'If command 1 exits zero, run command 2. If command 2 is non-zero, run command 3.'
In the simplest terms,. the || does not care about command1 at all. It only cares about command2.
Last edited by szboardstretcher; 01-18-2017 at 08:31 AM.
So even though your first command is true, we can make the "OR" fire off. Which is wrong. That is not If, Then, Else. That is 'If command 1 exits zero, run command 2. If command 2 is non-zero, run command 3.'
In the simplest terms,. the || does not care about command1 at all. It only cares about command2.
I like this argument, Now I got a whip out my editor and put away what I was doing. hold on,
let me put this to the test
I'll be back...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.