bash script: “*.jpg” expansion not working as expected inside $(…), for picking a random filebash...
Second (easy access) account in case my bank screws up
Déjà vu, again?
Are there any important biographies of nobodies?
How to handle self harm scars on the arm in work environment?
Cycle through MeshStyle directives in ListLinePlot
Why is one of Madera Municipal's runways labelled with only "R" on both sides?
How to tell your grandparent to not come to fetch you with their car?
Lualatex function producing trailing characters
What's up with this leaf?
Does Disney no longer produce hand-drawn cartoon films?
Is counterpoint still used today?
Passing multiple files through stdin (over ssh)
Thread Pool C++ Implementation
Fixing obscure 8080 emulator bug?
This riddle is not to see but to solve
Is it possible to have a wealthy country without middle class?
Pre-1972 sci-fi short story or novel: alien(?) tunnel where people try new moves and get destroyed if they're not the correct ones
Arriving at the same result with the opposite hypotheses
1980s live-action movie where individually-coloured nations on clouds fight
Taxi Services at Didcot
C++ Arduino IDE receiving garbled `char` from function
Medieval flying castle propulsion
Did Milano or Benatar approve or comment on their namesake MCU ships?
English word for "product of tinkering"
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 11 hours ago
Okay Dokey
asked 11 hours ago
Okay DokeyOkay Dokey
459
459
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
10 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
10 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♦
10 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
10 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 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♦
10 hours ago
Fair point,-print0
is better for those cases
– Sergiy Kolodyazhnyy
10 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
10 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♦
10 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
10 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
10 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
10 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 10 hours ago
answered 11 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
10 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
10 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
10 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
10 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
10 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♦
10 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
10 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 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
10 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♦
10 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
10 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 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 9 hours ago
answered 10 hours ago
terdon♦terdon
70.2k13146230
70.2k13146230
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
10 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♦
10 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
10 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 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
10 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♦
10 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
10 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 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
10 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
10 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♦
10 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♦
10 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
10 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
10 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 hours ago
@OkayDokey see update for how to make it truly not hardcoded.
– terdon♦
9 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♦
10 hours ago
Fair point,-print0
is better for those cases
– Sergiy Kolodyazhnyy
10 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
10 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♦
10 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
10 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♦
10 hours ago
Fair point,-print0
is better for those cases
– Sergiy Kolodyazhnyy
10 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
10 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♦
10 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
10 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 10 hours ago
terdon♦
70.2k13146230
70.2k13146230
answered 11 hours ago
Sergiy KolodyazhnyySergiy Kolodyazhnyy
76.3k10161337
76.3k10161337
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
10 hours ago
Fair point,-print0
is better for those cases
– Sergiy Kolodyazhnyy
10 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
10 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♦
10 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
10 hours ago
|
show 3 more comments
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
10 hours ago
Fair point,-print0
is better for those cases
– Sergiy Kolodyazhnyy
10 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
10 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♦
10 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
10 hours ago
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
10 hours ago
This breaks in the unlikely case where the file names contain newlines.
– terdon♦
10 hours ago
Fair point,
-print0
is better for those cases– Sergiy Kolodyazhnyy
10 hours ago
Fair point,
-print0
is better for those cases– Sergiy Kolodyazhnyy
10 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
10 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
10 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♦
10 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♦
10 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
10 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
10 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