Find the answer to your Linux question:
Results 1 to 6 of 6
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4

    \ escaped space is not being preserved after variable


    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

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I'm not exactly sure what you're doing, but how about something like this:
    Code:
    #!/bin/bash
    read -p "Enter Source: " Source
    read -p "Enter Dest: " Dest
    
    # replace spaces in Source variable with "\ "
    Source=$(echo $Source|sed -e 's| |\\ |g')
    
    echo "Usage: ln -vs $Source $Dest"
    
    # if you're using the variable in some command, eval it
    eval ln -sv $Source $Dest

  3. #3
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4
    That works, Wow, Thank you.

    Would it be possible for you to explain the usage of sed -e 's| |\\ |g

    Code:
    # replace spaces in Source variable with "\ "
    Source=$(echo $Source|sed -e 's| |\\ |g')

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    The first part of the sed command is the regular expression (regex) that some string ($Source) will get matched on - in our case, the space character (| |), and the second part is a replacement for it (in our case, a backslash and a space), but you have to escape the backslash (|\\ |). The pipes, '|', are just the field separators, they do nothing else. The 's' at the beginning tells sed that it is going to do a substitution of the 1st field with the second field. The 'g' (or global) at the end means replace all instances of the character (space, in our case).

    So what we're doing is taking the input the user gave to us as $Source, then we run $Source thru sed to replace all spaces found with a backslash (escaped) and a space and save the output of sed to the same variable name (Source).

  6. #5
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4
    Thanks for the clear and detailed definition. I will post the completed script when I get a chance.

    Thanks,
    Joel

  7. #6
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4
    Here is the finished script; thanks for everyones help.

    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 ""

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •