bash script: “*.jpg” expansion not working as expected inside $(…), for picking a random filebash script to run a second command with select output from first command as variableWild card expansion that works on command line but not in a bash script
Are there any instruments that don't produce overtones?
Why is only the fundamental frequency component said to give useful power?
Source that a married woman seduced by a “messianic figure” is still permitted to her husband
Soft question: Examples where lack of mathematical rigour cause security breaches?
At what point in time did Dumbledore ask Snape for this favor?
Is counterpoint still used today?
How to return a security deposit to a tenant
Cycle through MeshStyle directives in ListLinePlot
Is using haveibeenpwned to validate password strength rational?
What makes Ada the language of choice for the ISS's safety-critical systems?
How can I get an unreasonable manager to approve time off?
What can I, as a user, do about offensive reviews in App Store?
What is the highest possible permanent AC at character creation?
Can I make plugins required?
Universal hash functions with homomorphic XOR property
What ways have you found to get edits from non-LaTeX users?
Taxi Services at Didcot
An average heaven where everyone has sexless golden bodies and is bored
How does an ordinary object become radioactive?
What language is software running on the ISS written in?
How is water heavier than petrol, even though its molecular weight is less than petrol?
Is it a problem if <h4>, <h5> and <h6> are smaller than regular text?
Compiling c files on ubuntu and using the executable on Windows
Does an ice chest packed full of frozen food need ice?
bash script: “*.jpg” expansion not working as expected inside $(…), for picking a random file
bash script to run a second command with select output from first command as variableWild card expansion that works on command line but not in a bash script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a problem, with this simple script (pick a random file):
#!/usr/bin/env bash
set -x
srcDir="/home/user/Desktop/wallPapers/dir1,dir2,dir3"
randomFile=$(find "$srcDir" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' $randomFile
set +x
The problem is that while I can type this at the command line (and works perfectly fine):
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg"
Then the bash debugging set-commands (set -x and +x) tells me, that for some reason bash both encloses the directory string with single quotation marks and it also replaces the double quotation marks with single quotation marks?
./script.sh
+ srcDir='/home/user/Desktop/wallPapers/dir1,dir2,dir3'
++ find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type f -iname '"*.jpg"'
find: ‘/home/user/Desktop/wallPapers/dir1,dir2,dir3’: No such file or directory
+ randomFile=
+ printf '[%s]n'
[]
+ set +x
I understand, this is what bash sees, when the script runs:
find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type -iname '*.jpg'
And this causes the "No such file or directory"-message, very very annoying... I do not understand, why it inserts these single quotation marks, I want double quotation marks used instead, just like on the command line... Could anyone please explain, I would be happy for that, thanks!
command-line bash scripts
add a comment |
I have a problem, with this simple script (pick a random file):
#!/usr/bin/env bash
set -x
srcDir="/home/user/Desktop/wallPapers/dir1,dir2,dir3"
randomFile=$(find "$srcDir" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' $randomFile
set +x
The problem is that while I can type this at the command line (and works perfectly fine):
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg"
Then the bash debugging set-commands (set -x and +x) tells me, that for some reason bash both encloses the directory string with single quotation marks and it also replaces the double quotation marks with single quotation marks?
./script.sh
+ srcDir='/home/user/Desktop/wallPapers/dir1,dir2,dir3'
++ find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type f -iname '"*.jpg"'
find: ‘/home/user/Desktop/wallPapers/dir1,dir2,dir3’: No such file or directory
+ randomFile=
+ printf '[%s]n'
[]
+ set +x
I understand, this is what bash sees, when the script runs:
find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type -iname '*.jpg'
And this causes the "No such file or directory"-message, very very annoying... I do not understand, why it inserts these single quotation marks, I want double quotation marks used instead, just like on the command line... Could anyone please explain, I would be happy for that, thanks!
command-line bash scripts
add a comment |
I have a problem, with this simple script (pick a random file):
#!/usr/bin/env bash
set -x
srcDir="/home/user/Desktop/wallPapers/dir1,dir2,dir3"
randomFile=$(find "$srcDir" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' $randomFile
set +x
The problem is that while I can type this at the command line (and works perfectly fine):
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg"
Then the bash debugging set-commands (set -x and +x) tells me, that for some reason bash both encloses the directory string with single quotation marks and it also replaces the double quotation marks with single quotation marks?
./script.sh
+ srcDir='/home/user/Desktop/wallPapers/dir1,dir2,dir3'
++ find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type f -iname '"*.jpg"'
find: ‘/home/user/Desktop/wallPapers/dir1,dir2,dir3’: No such file or directory
+ randomFile=
+ printf '[%s]n'
[]
+ set +x
I understand, this is what bash sees, when the script runs:
find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type -iname '*.jpg'
And this causes the "No such file or directory"-message, very very annoying... I do not understand, why it inserts these single quotation marks, I want double quotation marks used instead, just like on the command line... Could anyone please explain, I would be happy for that, thanks!
command-line bash scripts
I have a problem, with this simple script (pick a random file):
#!/usr/bin/env bash
set -x
srcDir="/home/user/Desktop/wallPapers/dir1,dir2,dir3"
randomFile=$(find "$srcDir" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' $randomFile
set +x
The problem is that while I can type this at the command line (and works perfectly fine):
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg"
Then the bash debugging set-commands (set -x and +x) tells me, that for some reason bash both encloses the directory string with single quotation marks and it also replaces the double quotation marks with single quotation marks?
./script.sh
+ srcDir='/home/user/Desktop/wallPapers/dir1,dir2,dir3'
++ find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type f -iname '"*.jpg"'
find: ‘/home/user/Desktop/wallPapers/dir1,dir2,dir3’: No such file or directory
+ randomFile=
+ printf '[%s]n'
[]
+ set +x
I understand, this is what bash sees, when the script runs:
find '/home/user/Desktop/wallPapers/dir1,dir2,dir3' -type -iname '*.jpg'
And this causes the "No such file or directory"-message, very very annoying... I do not understand, why it inserts these single quotation marks, I want double quotation marks used instead, just like on the command line... Could anyone please explain, I would be happy for that, thanks!
command-line bash scripts
command-line bash scripts
edited 8 hours ago
Okay Dokey
asked 8 hours ago
Okay DokeyOkay Dokey
409
409
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Brace expansion doesn't occur within a variable assignment, as explained here:
Why do tilde prefixes expand prior to assignment, but braces don't
In other contexts, the quotes would have prevented brace expansion as well.
Even if you do manage to get srcDir to expand to a list of directories, quoting it again in the find command will cause it to be treated as a single argument instead of 3 separate paths.
Probably the right way to do this in bash is to use an array:
#!/usr/bin/env bash
set -x
srcDir=("/home/user/Desktop/wallPapers/"dir1,dir2,dir3)
randomFile=$(find "$srcDir[@]" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' "$randomFile"
set +x
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
add a comment |
As others have already pointed out, the quotes are preventing the brace expansion. You could simplify your script to just:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" | shuf -n 1)"
Or, if your file names can contain newline characters:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
If you want something that can run on arbitrary directories and file types, try this:
#!/usr/bin/env bash
targetFilePattern="$1"
shift
declare -a targetDirs=("$@")
echo "find $targetDirs[@] -type f -iname '$targetFilePattern' | shuf -n 1"
randomFile=$(find "$targetDirs[@]" -type f -iname "$targetFilePattern" | shuf -n 1)
echo "$randomFile"
You can then run it as:
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3
Or even
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3,'a dir with a space'
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
@OkayDokey if you want it in a variable, just userandomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
– terdon♦
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
add a comment |
Brace expansion isnt performed inside double quotes. There is a duplicate question regarding this somewhere. Also, use -printf flag for find, doing command substitution is unnecessary, so you can do this
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -printf '[%f]n' | shuf -n1
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
Fair point,-print0is better for those cases
– Sergiy Kolodyazhnyy
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
|
show 3 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1148573%2fbash-script-jpg-expansion-not-working-as-expected-inside-for-pickin%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Brace expansion doesn't occur within a variable assignment, as explained here:
Why do tilde prefixes expand prior to assignment, but braces don't
In other contexts, the quotes would have prevented brace expansion as well.
Even if you do manage to get srcDir to expand to a list of directories, quoting it again in the find command will cause it to be treated as a single argument instead of 3 separate paths.
Probably the right way to do this in bash is to use an array:
#!/usr/bin/env bash
set -x
srcDir=("/home/user/Desktop/wallPapers/"dir1,dir2,dir3)
randomFile=$(find "$srcDir[@]" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' "$randomFile"
set +x
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
add a comment |
Brace expansion doesn't occur within a variable assignment, as explained here:
Why do tilde prefixes expand prior to assignment, but braces don't
In other contexts, the quotes would have prevented brace expansion as well.
Even if you do manage to get srcDir to expand to a list of directories, quoting it again in the find command will cause it to be treated as a single argument instead of 3 separate paths.
Probably the right way to do this in bash is to use an array:
#!/usr/bin/env bash
set -x
srcDir=("/home/user/Desktop/wallPapers/"dir1,dir2,dir3)
randomFile=$(find "$srcDir[@]" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' "$randomFile"
set +x
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
add a comment |
Brace expansion doesn't occur within a variable assignment, as explained here:
Why do tilde prefixes expand prior to assignment, but braces don't
In other contexts, the quotes would have prevented brace expansion as well.
Even if you do manage to get srcDir to expand to a list of directories, quoting it again in the find command will cause it to be treated as a single argument instead of 3 separate paths.
Probably the right way to do this in bash is to use an array:
#!/usr/bin/env bash
set -x
srcDir=("/home/user/Desktop/wallPapers/"dir1,dir2,dir3)
randomFile=$(find "$srcDir[@]" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' "$randomFile"
set +x
Brace expansion doesn't occur within a variable assignment, as explained here:
Why do tilde prefixes expand prior to assignment, but braces don't
In other contexts, the quotes would have prevented brace expansion as well.
Even if you do manage to get srcDir to expand to a list of directories, quoting it again in the find command will cause it to be treated as a single argument instead of 3 separate paths.
Probably the right way to do this in bash is to use an array:
#!/usr/bin/env bash
set -x
srcDir=("/home/user/Desktop/wallPapers/"dir1,dir2,dir3)
randomFile=$(find "$srcDir[@]" -type f -iname "*.jpg" | shuf -n 1)
printf '[%s]n' "$randomFile"
set +x
edited 8 hours ago
answered 8 hours ago
steeldriversteeldriver
73.6k11121197
73.6k11121197
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
add a comment |
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
This is exactly the solution I was looking for, thank you very much. You keep using the variables, which I wanted to learn how to use. Also, I didn't realise/knew I had to use an array, now I know in the future. In the top of my script I then define "srcDir" - and then the script does whatever it's supposed to do later on, with the result in the "randomFile"-variable. Thank you very much.
– Okay Dokey
7 hours ago
add a comment |
As others have already pointed out, the quotes are preventing the brace expansion. You could simplify your script to just:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" | shuf -n 1)"
Or, if your file names can contain newline characters:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
If you want something that can run on arbitrary directories and file types, try this:
#!/usr/bin/env bash
targetFilePattern="$1"
shift
declare -a targetDirs=("$@")
echo "find $targetDirs[@] -type f -iname '$targetFilePattern' | shuf -n 1"
randomFile=$(find "$targetDirs[@]" -type f -iname "$targetFilePattern" | shuf -n 1)
echo "$randomFile"
You can then run it as:
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3
Or even
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3,'a dir with a space'
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
@OkayDokey if you want it in a variable, just userandomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
– terdon♦
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
add a comment |
As others have already pointed out, the quotes are preventing the brace expansion. You could simplify your script to just:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" | shuf -n 1)"
Or, if your file names can contain newline characters:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
If you want something that can run on arbitrary directories and file types, try this:
#!/usr/bin/env bash
targetFilePattern="$1"
shift
declare -a targetDirs=("$@")
echo "find $targetDirs[@] -type f -iname '$targetFilePattern' | shuf -n 1"
randomFile=$(find "$targetDirs[@]" -type f -iname "$targetFilePattern" | shuf -n 1)
echo "$randomFile"
You can then run it as:
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3
Or even
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3,'a dir with a space'
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
@OkayDokey if you want it in a variable, just userandomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
– terdon♦
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
add a comment |
As others have already pointed out, the quotes are preventing the brace expansion. You could simplify your script to just:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" | shuf -n 1)"
Or, if your file names can contain newline characters:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
If you want something that can run on arbitrary directories and file types, try this:
#!/usr/bin/env bash
targetFilePattern="$1"
shift
declare -a targetDirs=("$@")
echo "find $targetDirs[@] -type f -iname '$targetFilePattern' | shuf -n 1"
randomFile=$(find "$targetDirs[@]" -type f -iname "$targetFilePattern" | shuf -n 1)
echo "$randomFile"
You can then run it as:
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3
Or even
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3,'a dir with a space'
As others have already pointed out, the quotes are preventing the brace expansion. You could simplify your script to just:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" | shuf -n 1)"
Or, if your file names can contain newline characters:
#!/usr/bin/env bash
printf '[%s]n' "$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
If you want something that can run on arbitrary directories and file types, try this:
#!/usr/bin/env bash
targetFilePattern="$1"
shift
declare -a targetDirs=("$@")
echo "find $targetDirs[@] -type f -iname '$targetFilePattern' | shuf -n 1"
randomFile=$(find "$targetDirs[@]" -type f -iname "$targetFilePattern" | shuf -n 1)
echo "$randomFile"
You can then run it as:
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3
Or even
printRandomFile '*jpg' /home/terdon/Desktop/wallPapers/dir1,dir2,dir3,'a dir with a space'
edited 6 hours ago
answered 8 hours ago
terdon♦terdon
70.1k13146230
70.1k13146230
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
@OkayDokey if you want it in a variable, just userandomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
– terdon♦
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
add a comment |
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
@OkayDokey if you want it in a variable, just userandomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"
– terdon♦
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
Thanks - also this looks a bit "hardcoded" - I wanted to have the resulting random file-name in a new variable, so I could keep do some manipulation later on in the script. The prinf was only for myself, to see the value. Sorry, you, couldn't know that, because I didn't tell it before... I'm however upvoting for the good explanation, as my way of saying thanks.
– Okay Dokey
8 hours ago
@OkayDokey if you want it in a variable, just use
randomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"– terdon♦
7 hours ago
@OkayDokey if you want it in a variable, just use
randomFile="$(find /home/terdon/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -print0| shuf -zn 1)"– terdon♦
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
Yes, ok, I know. But the path is kind of "hard-coded" which is ok for a small script. But you're right. The solutions you presented are all good, thanks.
– Okay Dokey
7 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
6 hours ago
add a comment |
Brace expansion isnt performed inside double quotes. There is a duplicate question regarding this somewhere. Also, use -printf flag for find, doing command substitution is unnecessary, so you can do this
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -printf '[%f]n' | shuf -n1
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
Fair point,-print0is better for those cases
– Sergiy Kolodyazhnyy
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
|
show 3 more comments
Brace expansion isnt performed inside double quotes. There is a duplicate question regarding this somewhere. Also, use -printf flag for find, doing command substitution is unnecessary, so you can do this
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -printf '[%f]n' | shuf -n1
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
Fair point,-print0is better for those cases
– Sergiy Kolodyazhnyy
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
|
show 3 more comments
Brace expansion isnt performed inside double quotes. There is a duplicate question regarding this somewhere. Also, use -printf flag for find, doing command substitution is unnecessary, so you can do this
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -printf '[%f]n' | shuf -n1
Brace expansion isnt performed inside double quotes. There is a duplicate question regarding this somewhere. Also, use -printf flag for find, doing command substitution is unnecessary, so you can do this
find /home/user/Desktop/wallPapers/dir1,dir2,dir3 -type f -iname "*.jpg" -printf '[%f]n' | shuf -n1
edited 8 hours ago
terdon♦
70.1k13146230
70.1k13146230
answered 8 hours ago
Sergiy KolodyazhnyySergiy Kolodyazhnyy
76.3k10161337
76.3k10161337
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
Fair point,-print0is better for those cases
– Sergiy Kolodyazhnyy
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
|
show 3 more comments
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
Fair point,-print0is better for those cases
– Sergiy Kolodyazhnyy
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
8 hours ago
Fair point,
-print0 is better for those cases– Sergiy Kolodyazhnyy
8 hours ago
Fair point,
-print0 is better for those cases– Sergiy Kolodyazhnyy
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
Thank you very much, but for a script that could eventually become bigger I prefer executing some kind of expression, based on variables, defined in the top of the script. This looks a bit more like something on the command line - but I upvoted, due to the explanation. The purpose of my script was also not to just print something out on the screen (the printf-line) - but to do some further manipulation. I know you couldn't see that from my original post, so sorry about that... And thanks.
– Okay Dokey
8 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey you seem to be misunderstanding. This (and all other answers so far) are no more "hardcoded" than your approach. But perhaps we can give you better solutions if you edit your question and explain in more detail.
– terdon♦
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
@OkayDokey If you do need to use dir1,dir2,dir3 later in a script, use set command or array to save the directory names. I would explain further but currently have no time. And yes, it was not obvious that you want to reuse those further.
– Sergiy Kolodyazhnyy
7 hours ago
|
show 3 more comments
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1148573%2fbash-script-jpg-expansion-not-working-as-expected-inside-for-pickin%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown