bash - woof does not work with files whose names contains spaces or brackets - Ask Ubuntu


i wrote bash script , put in ~/.local/share/nautilus/scripts/.

in nautilus, if right click on file , select script menu, start http server share current file, , qr code display, mobile can scan url download file. server stop when close qr pic window.

it works, when filenames contains space or brackets, cannot download file.

▶ cat ~/.local/share/nautilus/scripts/share-http.bash  #!/bin/bash  port=8080 pkill woof file=`echo -n $nautilus_script_selected_file_paths|sed 's/\ *$//'|sed 's/\ /\\\ /g'` echo $file >> ~/tmp woof -c 4 -p $port \'$file\' & # woof -c 4 -p $port $nautilus_script_selected_file_paths & url=`ifconfig|awk '/inet /&&!/127./{print $2}'|sed 's/.*://'` qrencode -s 5 -o /tmp/url.png "http://$url:$port" eog /tmp/url.png && pkill woof 

this script needs woof+qrencode run.

i find string $nautilus_script_selected_file_paths has suffix space, modify code.

in bash, should work:

▶ woof -c 4 -p 8080 '/home/eexp/download/img_20161024_132 037.jpg' 

but still fails. see commands in ps has apostrophe if has backslash before space.

▶ pgrep woof 13109 /usr/bin/python /usr/bin/woof -c 4 -p 8080 '/home/eexp/download/img_20161024_132 037.jpg' ▶ pgrep woof 13531 /usr/bin/python /usr/bin/woof -c 4 -p 8080 '/home/eexp/download/img_20161024_132\ 037.jpg' 

in gnu/linux except / , \0 (ascii nul) allowed valid filename constituent character. space, tab, newline valid filename constituent characters. when have such filename, need use trivial escaping mechanism literal meaning of character(s).

for example, filename like:

foo bar spam.txt 

you can use single quotes, double quotes or \ escaping spaces:

'foo bar spam.txt' "foo bar spam.txt" foo\ bar\ spam.txt 

single quotes recommended filename foo $bar spam.txt if use double quotes shell take $bar variable expansion, same goes unescaped $ while using \.

without escaping, shell perform word splitting based on values of ifs environment variable (space, tab, newline default), , pathname expansion on string (if there *, ?, [] in filename).

in case variable, need double quote variable expansion:

file='foo bar spam.txt' echo "$file" 

simple $file again fall word splitting (and pathname expansion).


Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User