BASH fancy test vs if statements how to?
I cannot figure out what search words to get this off of google. So I am coming here to seek my answer(s).
I am wanting to see if I can get this to run faster then it already is by shorting up the code for bash to preform checks before issuing the commands. This is the current way this code block is written. I do not really need it to check to see if the first two varitables are empty or not. Because I already ensured they are not with puddletag. Code:
Code:
[[ ! -d "$move_to"/"$ARTIST"/"$ALBUM" ]] && mkdir -pv "$move_to"/"$ARTIST"/"$ALBUM" Writting it this way Code:
[[ ! -d "$move_to" ]] && mkdir -pv "$move_to" gets me these results Code:
+ [[ ! -d /run/media/userx/NTFS600GB/TEST ]] I hope I explaned that logic ok. even when I chance it. getting rid of all of the echo and making it just one liners Code:
[[ ! -d "$move_to" ]] && mkdir -pv "$move_to" I get this Code:
+ [[ ! -d /run/media/userx/NTFS600GB/TEST ]] SO I am thinking well I got to use the old if then else to get this, or is their a better logical way of doing this so that it will just run faster that I have not thought of yet to do this out there in someone elses head? I guess I could do it like this: But I was looking to beaf up my BASH with using [[ check for true ]] && do something(s) Code:
if [[ ! -d "$move_to"/"$ARTIST"/"$ALBUM" ]] ; then Does anyone have a link to the documentataion on that from of writting BASH to share? [[ if test true ]] && do something(s) |
in short you need { } if you want more commands after &&, otherwise condition will only be valid for the first one
Code:
[[ -d "$move_to"/"$ARTIST"/"$ALBUM" ]] && { |
Quote:
But that leaves an else do something "esle" out of the picture. Code:
[[ true ]] && { do some stuff and some more stuff } else { do something else and some more stuff too} doesn't work Code:
[[ ! -d "$move_to"/"$ARTIST"/"$ALBUM" ]] && { Code:
./ResortMP3: line 58: syntax error near unexpected token `else' I think I almost got it, I am still getting one error on the first creation of a Directory due to the next line Code:
[[ ! -d "$move_to"/"$ARTIST"/"$ALBUM" ]] && { Code:
mkdir: created directory '/run/media/userx/NTFS600GB/Test/Joe Jackson' |
what was that one liner that C has? Something like this (its been a while)
Code:
(?:) OK I think I found it. Code:
[[ $b = 5 ]] && a="$c" || a="$d" BASH Ternary Operators |
No, you cannot use "else" without "if ... then", but you can accomplish it this way:
Code:
[[ some condition ]] && { code block 1; } || { code block 2; } Doing that should be an intellectual exercise only. In the real world, "if ... then ... else ... fi" would be preferable. |
NOT WORKING YET
using this example Code:
[ $b == 5 ] && { a=$c; true; } || a=$d Code:
find "$working_dir" -type f -name "*.mp3" | while [[ $count -lt $max ]] ; do Code:
./ResortMP3: line 65: syntax error near unexpected token `done' Code:
if [[ ! -d "$move_to"/"$ARTIST"/"$ALBUM" ]] ; then { mkdir -pv "$move_to"/"$ARTIST"/"$ALBUM" ; mv -v "${FILENAME}" "$move_to"/"$ARTIST"/"$ALBUM"/ } else { mv -v "${FILENAME}" "$move_to"/"$ARTIST"/"$ALBUM"/ } fi |
I would like to step back a little from what is perhaps the actual problem.
1. mkdir -p will already perform its own test if the path exists and create if not or skip, so I am not seeing the point of the test?? 2. Testing (or using above) on both $move_to and "$move_to"/"$ARTIST"/"$ALBUM" is again a waste as it will make all required parent directories as needed, or again, skip if they all exist 3. This one is personal, but you only need one set of quotes, eg. Code:
"$move_to"/"$ARTIST"/"$ALBUM" Code:
left=$(expr $max - $count) 6. Remember that when piping into a while loop, you will not be able to use any of the variable values outside the loop due to be in a subshell. The alternative would be: Code:
while (( count < max )) |
Quote:
Code:
{ mkdir -pv "$move_to"/"$ARTIST"/"$ALBUM" ; mv -v "${FILENAME}" "$move_to"/"$ARTIST"/"$ALBUM"/; } |
Quote:
Regardless, I am not getting it to work. lol But according to this Ternary operator in bash Their is a way to fake it. Quote:
one example Quote:
Code:
unexpected done The only one I am getting to work is this one. Code:
Cheers and thanks! btw looking at your code example again after I posted this. I see a difference from what I tried. so I'll give that a try. OK I did it! yeah!! woo hoo! :D Code:
[[ ! -d "$move_to"/"$ARTIST"/"$ALBUM" ]] && { mkdir -pv "$move_to"/"$ARTIST"/"$ALBUM" ; mv -v "${FILENAME}" "$move_to"/"$ARTIST"/"$ALBUM"/ ; } || { mv -v "${FILENAME}" "$move_to"/"$ARTIST"/"$ALBUM"/ ; } |
@pan64
@rknichols +1 given thanks! and to @grail for the tips in coding BASH |
Just to conclude, the simplest one liner is:
Code:
mkdir -pv "$move_to/$ARTIST/$ALBUM" && mv -v "${FILENAME}" "$move_to/$ARTIST/$ALBUM/" |
Quote:
Quote:
Quote:
Quote:
Quote:
My Code it now way shorter than the one liner I just posted got me to mark solved. I went back over this post that is when I noticed yours within it. Gave it a look see took your advice and this is what I ended up with. Code:
#!/bin/bash Thanks again! |
No probs ... here are some twists I had on your code:
Code:
#typeset -i count max Take what you like :D |
Quote:
Code:
#!/bin/bash the error Code:
[scripts]>>$ ./MP3Sort Code:
set -- $ARTIST the EOF is doing what? you got links to BASH pages that I could look over to educate myself on this? commenting it out the cat <<-EOF -- to --- EOF - , it runs fine and I see what that set is doing thanks. Changed the echo count to this Code:
|
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. |
All times are GMT -5. The time now is 02:38 PM. |