Filenames with spaces in bash scripts.
Code:
#list.sh Code:
$ list.sh 0 3 4 |
I think you should definitively read these:
http://mywiki.wooledge.org/BashPitfa...8ls_.2A.mp3.29 http://mywiki.wooledge.org/ParsingLs http://mywiki.wooledge.org/BashFAQ/020 The first two articles explain what and why is wrong. The last one proposes solutions. |
Code:
list="$(ls *mp3)" Code:
for file in *mp3 |
One option may be to read the list of files directly into an array with one file per line:
Code:
IFS=$'\n' Stefan |
Thank you very much. The links solved the problem. Could I ask you how to determine, within a bash script, if a certain string contains a given substring. For example, given "superior" I want to know it if contains "upe", which here is certainly true.
More precisely, the problem is this: one of the arguments can be of the form m-n, for example, Code:
list.sh 2 4 6-8 |
You can try the pattern matching operator =~. In order to match a numeric interval, looking for an hyphen is not enough, otherwise strings like "-8" or "hello-world" or "6-" would be valid. Instead you have to match the numbers as well:
Code:
[[ $1 =~ ^[0-9]+-[0-9]+$ ]] && echo "$1 is a numeric interval" |
Cool! Then I could do
Code:
s1=$(echo $1|sed s/"-"/" ") |
I did it this way:
Code:
# play.sh |
Now you have a solution .. here is another way to think about :)
Code:
#!/bin/bash Many others of course ... just thought it might interest you. |
I like it. It seems more compact.
|
If I press ^C while the script is running it has no other effect than making mplayer go on to the next song. How could I stop the program in response to ^C or some other key?
|
Probably need to investigate the trap and / or wait command.
|
Quote:
Quote:
Code:
foo=6-8 |
Quote:
|
Quote:
|
I'm usually a stickler for this kind of thing, but I actually think it doesn't matter too much to worry about filenames with newlines in them, particularly when working directly on the command line or in other quick&dirty situations.
That said, when writing a script that will be used repeatedly, it is generally better to do it correctly the first time. Then you'll never have to worry about correcting it later. The main issue I have with propofol's script is here: Code:
list=($(ls -1 *.mp3)) I will also say that changing IFS globally is generally bad form, and can lead to non-transparent code and hard-to-diagnose errors. As usual, simple globbing is all you generally need, either to set an array, or processed directly in a for loop. Code:
list=( *.mp3 ) Code:
while IFS='' read -r -d '' fname; do How can I find and deal with file names containing newlines, spaces or both? http://mywiki.wooledge.org/BashFAQ/020 One more point that hasn't been mentioned yet. Never use #!/bin/sh as the shebang unless you specifically need POSIX-compatible portability. If you intend to rely on Bash specific features like arrays, then you need to call it explicitly with #!/bin/bash. Otherwise it won't work properly if the system you run it on doesn't have bash as its system shell. |
All times are GMT -5. The time now is 07:17 PM. |