ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sorry im a newb to linux scripting. i made a script for nautilus to make a dir on a external hdd and copy a avi over, pretty simple task. works fine but i started altering it to make it work with file that are named with special characters and white spaces ex: (this movie (2009) [eng].avi). i somehow managed to get the mkdir to work properly but cannot work the cp command. any help?
Code:
#!/bin/bash
#cp_xvid
BASEFILE=`basename "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"`
BASEDIR=$(echo $PWD | sed "s/.*\///")
NEWDIR=$(echo "/media/XViD/"$BASEDIR)
CPDIR=""$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS""
if [ -e /media/XViD ]; then
mkdir "$NEWDIR";
(
echo "pulsate!";
cp "$CPDIR" "$NEWDIR";
) | zenity --window-icon=/home/amd64/xvid.png --width=600 --progress --pulsate --title "$BASEDIR" --text "Moving ""$BASEFILE"" to XViD" --auto-kill --auto-close
else
zenity --error --title "Not Mounted" --text "Please mount /media/XViD"
fi
ive tried something like this
Code:
CPDIR=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed "s/ /\\\ /g" | sed "s/(/\\\(/g" | sed "s/)/\\\)/g" \
| sed "s/\[/\\\\[/g" | sed "s/\]/\\\\]/g")
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
all i know as anything that has spaces needs to be surrounded by ""
for example
Quote:
hello world.jpg
is interpretted as hello and world.jpg
but
Quote:
"hello world.jpg"
is interpetted as hello world.jpg
so cp "hi there.dat" .\somedir is ok
cp hi there.dat .\somedir is not
if that didnt help someone elze will come along
Just a couple of notes: using the shell's internal parameter expansion is usually better than calling on sed and the like:
BASEDIR=${PWD##*/}
and you don't need to 'echo' things in $(command expansion) - you can just directly assign the strings, as above and for:
NEWDIR=/media/XViD/$BASEDIR
Also, what's the value of $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS? Where's it coming from?
On topic, could you give a literal example and the error output. You should be able to copy 'this movie (2009) [eng].avi' simply be quoting it, as smeezekitty says, though you may be better off renaming it.
As far as that goes, you generally only need to invoke sed the once - it can take a complex regular expression and, if you really need several expressions, you can run one sed with semi-colon separated args - sed 's/foo/bar;s/baz/mu/'.
like i said im a newb so the echoing was my way to make it work for now... lol.
Code:
$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
is the selected file in gnome (running ubuntu 9.04) (right-click > scripts > your_script). my problem is i cant understand how to get the quotes to work with a variable. i tried
Code:
\"$BASEDIR\"
and other various methods i could think of. when i try to "debug" by running commands in terminal (cause i dont recieve output the way this script is run) i get errors like
Code:
cp: target `[Eng]\'' is not a directory
or
cp: target `[Eng]"' is not a directory
or something similar.
this is all i need it to do(the rest is unnecessary for now)
Code:
target /home/amd64/this movie directory/this movie (2009) [eng].avi
mkdir /media/somewhere_else/this movie directory
mv /home/amd64/this movie directory/this movie (2009) [eng].avi /media/somewhere_else/this movie directory
think i may have found a pro, maybe my main one. think
Code:
$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
needs to be stripped down, seems it may have a return (\n) @the end. This is what my code looks like now and it works still with files without whitespace and special characters but thats it.
Okay, sorry if I'm not following along, but if I only had gxmessage and I wanted to copy an oddly titled avi to /tmp, this would work for me:
Code:
#!/bin/bash
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS="$HOME/this movie (2009) [eng].avi"
BASEFILE=${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS##*/}
BASEDIR=${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS%/*}
TARGETDIR=/tmp
if [[ -d $TARGETDIR ]]; then
gxmessage "Copy $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS to $TARGETDIR?"
if (( $? == 0 )); then
cp "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" "$TARGETDIR"
fi
else
gxmessage "$TARGETDIR not mounted."
fi
I set the variable that nautilus is supposed to give you to a full path and quote it. I split that path into its directory and file components. I define the target directory. I use a new-style test to see if it exists as a directory - if it doesn't, the user gets the "unmounted message'. Otherwise it pops up a gxmessage to show what's going to happen and get input from the user about whether they want to go ahead. If they decline, nothing happens. If they accept, it performs the action exactly as it showed the user. And it Works for Me.
If it's not, try to redirect output to a file, such as doing a dummy script that just does 'echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS > mydebugfile' or put 'bash -x' in the shebang of the script you're using and redirect stderr to a file and interpret the whole thing. Because, yeah, it must be nautilus that's doing you wrong.
That's insane on nautilus' part. Ordinarily, I'd say to use the variable without quotes, which would collapse the newline, but you have whitespace and special chars, so that won't work. Let me think a bit.
As far as reading, there's the bash scripting guides at TLDP but I don't get a whole lot from them. There's #bash on freenode, but it can be an evil place. comp.unix.shell is good on usenet if you have a good killfile. The bash manual is actually very good if you sort of get a basic handle on stuff so that it actually makes sense. It's not the first thing I'd plunge into heavily, but the second.
That snippet wouldn't. You could keep trying variations if you wanted to, but the printf should be okay.
As far as a link, no, not off hand. The bash manual explains it under parameter expansion - just type '/Parameter E' in less and it should take you right to it.
How about adding some degug statements so you cane see what's going on inside your script? Something like
Code:
#!/bin/bash
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS="$HOME/this movie (2009) [eng].avi"
echo "DEBUG: NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is '$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'"
BASEFILE=${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS##*/}
echo "DEBUG: BASEFILE is '$BASEFILE'"
BASEDIR=${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS%/*}
echo "DEBUG: BASEDIR is '$BASEDIR'"
TARGETDIR=/tmp
if [[ -d "$TARGETDIR" ]]; then # double quotes added in case $TARGETDIRcontains whitespace characters
gxmessage "Copy $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS to $TARGETDIR?"
#if (( $? == 0 )); then
if [[ $? -eq 0 ]]; then
cp "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" "$TARGETDIR"
fi
else
gxmessage "$TARGETDIR not mounted."
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.