LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2009, 10:55 PM   #1
zebrapositions
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Rep: Reputation: 0
bash script, special charaters stopping cp


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")
 
Old 09-19-2009, 10:59 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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
 
Old 09-20-2009, 12:34 AM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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/'.
 
Old 09-20-2009, 01:00 AM   #4
zebrapositions
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
just need to make it work with set variables
 
Old 09-20-2009, 01:13 AM   #5
zebrapositions
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.
Code:
#!/bin/bash
#cp_xvid

BASEFILE=`basename "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"`
BASEDIR=${PWD##*/}
NEWDIR=/home/amd64/test/$BASEDIR

if [ -e /home/amd64/test ]; then
	zenity --error --text "cp $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS $NEWDIR"
	mkdir "$NEWDIR";
	(
	echo "pulsate!";
	cp $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS "$NEWDIR";
	) | zenity --window-icon=/home/amd64/Convertxtodvd.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 quotes around $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS in all formats i know and nothing works.
 
Old 09-20-2009, 01:47 AM   #6
zebrapositions
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Code:
CPDIR=$(echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | sed "s/\n//g")
yup damn newline in the variable. thnx for the help on the other stuff guys. better way to do this?
Code:
 CPDIR=$(NAUTILUS_SCRIPT_SELECTED_FILE_PATHS//\n/}
someting like that? i have no clue on the syntax. i would like to know, please leave a link if you have something i can read slakmagik
 
Old 09-20-2009, 01:52 AM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 09-20-2009, 02:03 AM   #8
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by zebrapositions View Post
[CODE]yup damn newline in the variable
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.
 
Old 09-20-2009, 02:09 AM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Try this:
Code:
SENSIBLE_VAR="$(printf "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS")"
That's very similar to the assignment by echoing you were doing in the first place, but with a different purpose.
 
Old 09-20-2009, 02:21 AM   #10
zebrapositions
LQ Newbie
 
Registered: Sep 2009
Posts: 6

Original Poster
Rep: Reputation: 0
sorry if i confused you slakmagik. just wondering if something like this would work
Code:
BASEFILE=${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS%\n}
and also do you have a link to a webpage that explains the syntax of ${VARIABLE%/*} and etc or what thats called?
 
Old 09-20-2009, 02:41 AM   #11
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
No problem - it's easy to do.

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.
 
Old 09-20-2009, 03:04 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using special symbols in bash script Dr_Death_UAE Linux - General 10 11-14-2008 12:48 PM
Bash script stop CTRL+C stopping joshiggins Linux - Software 2 06-12-2008 03:45 AM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 11:56 AM
inserting special characters into mysql with bash script ihopeto Linux - Newbie 1 12-05-2006 12:46 PM
Star Office Special charaters Fried General 0 09-15-2001 02:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration