Register
It is currently Mon Oct 20, 2014 5:19 am

\ escaped space is not being preserved after variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Sep 15, 2011 8:48 pm   

Joined: Thu Sep 15, 2011 8:41 pm
Posts: 2
I am trying to put absolute paths into my script and folder/folder\ folder is not being preserved. /folder/folder folder is being returned.

Here is an example:

Code:
#!/bin/bash
clear
read -p "Enter the Absolute Path to Source File/Folder: " Source
read -p "Enter the Absolute Path to Destination File/Folder: " Destination
echo "Usage: ln -vs $Source $Destination"
ln -vs $Source $Destination
cd $Destination; cd ..
ls -lasS


Code:
Enter the Absolute Path to Source File/Folder: /Users/joel/Library/Application\ Support
Enter the Absolute Path to Destination File/Folder: /Users/joel
Usage: ln -vs /Users/joel/Library/Application Support
/Users/joel/Library/Application Support
/Users/joel
computer:~ joel$


Does anyone know how to get the \ escape the be there after being put into a variable?

Thanks,
Joel


Last edited by joelcarlton on Thu Sep 15, 2011 10:55 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Sep 15, 2011 10:53 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
cross site posting is not very nice


Top
 Profile  
 PostPosted: Fri Sep 16, 2011 9:57 am   

Joined: Thu Sep 15, 2011 8:41 pm
Posts: 2
I have it all worked out; see below

Code:

# Clear screen for input
clear
# Gather Source
read -p "Enter the Absolute Path to Source File/Folder: " Source
# replace spaces in Source variable with "\ "
Source2=$(echo $Source|sed -e 's| |\\ |g')
# Gather Destination
read -p "Enter the Absolute Path to Destination File/Folder: " Destination
# replace spaces in Destination variable with "\ "
Destination2=$(echo $Destination|sed -e 's| |\\ |g')

# Clear screen for output
clear
# Display current working directory
echo "Long listing taken from: `pwd`"
echo ""
# Execute command
eval ln -s $Source2 $Destination2
# cd to directory where link was created
eval cd $Destination2
# Extract the name of the link from the path of the
# source file/folder turn that into a variable
greppath=$(echo $Source | rev | cut -d"/" -f1 | rev )
# Grep the name of variable just created and display
# it in a long listing showing the link
ls -lasS | grep "$greppath"
# Show Usage
echo "Usage: ln -vs $Source2 $Destination2"
echo ""



Top
 Profile  
 PostPosted: Tue Sep 20, 2011 12:50 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
$ help '\read'
[...]
-r      do not allow backslashes to escape any characters
[...]


Use more quotes.


Top
 Profile  
 PostPosted: Thu Sep 22, 2011 11:27 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
I told him pretty much the same thing on the other forum, but he couldn't wrap his head around it there either...
I've given up on this one.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP