LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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


Closed Thread
  Search this Thread
Old 01-17-2017, 01:26 PM   #16
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242

@grail

I take it that the while is getting the path to read the FILENAME off of < <(find "$working_dir" -type f -name "*.mp3")
 
Old 01-17-2017, 01:28 PM   #17
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by szboardstretcher View Post
For the <<-EOF explanation:

http://man.cx/bash

Under the "Here Documents" section.

And for the set -- explanation:

http://man.cx/set

Search for -- in the page for information.
Thanks BOOKMARKED and checking them out.
 
Old 01-17-2017, 01:30 PM   #18
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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
 
1 members found this post helpful.
Old 01-17-2017, 01:57 PM   #19
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by szboardstretcher View Post
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
 
Old 01-17-2017, 02:08 PM   #20
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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
 
Old 01-17-2017, 02:16 PM   #21
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by szboardstretcher View Post
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))"
 
Old 01-17-2017, 05:31 PM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
One echo is more efficient than the cat << and the many echo
Code:
echo "
    MAX   is $max
    count is $((++count))
             --------
             $((max - count))

"
Because the echo string needs quotes, an embedded literal quote must be escaped.

Last edited by MadeInGermany; 01-17-2017 at 05:32 PM.
 
2 members found this post helpful.
Old 01-17-2017, 05:50 PM   #23
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by MadeInGermany View Post
One echo is more efficient than the cat << and the many echo
Code:
echo "
    MAX   is $max
    count is $((++count))
             --------
             $((max - count))

"
Because the echo string needs quotes, an embedded literal quote must be escaped.
Point taken .
thanks
 
Old 01-17-2017, 08:55 PM   #24
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
2 members found this post helpful.
Old 01-17-2017, 09:16 PM   #25
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
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.
 
Old 01-18-2017, 01:54 AM   #26
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
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.
 
1 members found this post helpful.
Old 01-18-2017, 07:45 AM   #27
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
Code:
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
they are all the same.
Code:
variable = condition ? value_if_true : value_if_false


Ternary operator in bash


read this too
https://en.wikipedia.org/wiki/%3F:

Last edited by BW-userx; 01-18-2017 at 08:01 AM.
 
Old 01-18-2017, 08:11 AM   #28
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
2 members found this post helpful.
Old 01-18-2017, 08:30 AM   #29
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
What pan is pointing out is this... They are not equal.

The && operator will fire off the command on the right if the exit code of the left was 0.

The || operator will fire off the command on the right if the exit code of the left was not 0.

For example:
Code:
true && echo "true" || echo "false"
outputs 'true' right?

true && false || echo "false"
outputs 'false'
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.
 
Old 01-18-2017, 08:47 AM   #30
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by szboardstretcher View Post
What pan is pointing out is this... They are not equal.

The && operator will fire off the command on the right if the exit code of the left was 0.

The || operator will fire off the command on the right if the exit code of the left was not 0.

For example:
Code:
true && echo "true" || echo "false"
outputs 'true' right?

true && false || echo "false"
outputs 'false'
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...

Last edited by BW-userx; 01-18-2017 at 09:01 AM.
 
  


Closed Thread



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
Help with scripting, aliases, and test statements. ThatCyberGuy Linux - Newbie 4 11-14-2015 02:58 PM
LXer: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting LXer Syndicated Linux News 0 06-29-2014 07:35 PM
LXer: How to Make a Fancy and Useful Bash Prompt in Linux LXer Syndicated Linux News 0 05-09-2014 07:51 PM
LXer: Bash One-Liner Script To Produce Somewhat-Fancy Output Of Who's On Your Linux O LXer Syndicated Linux News 0 11-19-2008 01:40 AM
urpmi looks for fancy dependency (bash) jfi Mandriva 6 03-05-2004 03:35 PM

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

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

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