Problem with variables and bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with variables and bash script
# 1  
Old 08-15-2014
Problem with variables and bash script

From the command line:
Code:
dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX
-rw-r--r--  1 dion  staff  157934  7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX

works as expected
However in a script:
Code:
#!/bin/bash

GARMIN_DEVICE_NUMBER="3816821036"
sourcefile="/Users/dion/Library/Application\ Support/Garmin/Devices/${GARMIN_DEVICE_NUMBER}/History/2014-06-07-055251.TCX"
destfiles="/tmp/"
ls -l "${sourcefile}"
echo cp -- "${sourcefile}" "${destfiles}"
cp -- "${sourcefile}" "${destfiles}"

the following is returned
Code:
dions-air:scripts dion$ ./test.command 
ls: /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX: No such file or directory
cp -- /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX /tmp/
cp: /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX: No such file or directory

Some help would be appreciated. I have no idea where I have gone wrong. Thanks.
# 2  
Old 08-15-2014
Quote:
Originally Posted by dionbl
From the command line:
Code:
dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX
-rw-r--r--  1 dion  staff  157934  7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX

works as expected
However in a script:
Code:
#!/bin/bash

GARMIN_DEVICE_NUMBER="3816821036"
sourcefile="/Users/dion/Library/Application\ Support/Garmin/Devices/${GARMIN_DEVICE_NUMBER}/History/2014-06-07-055251.TCX"
destfiles="/tmp/"
ls -l "${sourcefile}"
echo cp -- "${sourcefile}" "${destfiles}"
cp -- "${sourcefile}" "${destfiles}"

the following is returned
Code:
dions-air:scripts dion$ ./test.command 
ls: /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX: No such file or directory
cp -- /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX /tmp/
cp: /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX: No such file or directory

Some help would be appreciated. I have no idea where I have gone wrong. Thanks.
Note that in the successful ls, the output does not contain a backslash character before the space. In that ls command, you used a backslash to escape the space to keep the shell from treating it as two filename operands.

In the cases that failed you quoted the filenames AND escaped the space. You need to do one or the other; not both. If at all possible, NEVER create filenames that contain a space, tab, or newline character. To solve the problem in this case, remove the \ character from the quoted strings when you assign the value to sourcefile. That is, change:
Code:
sourcefile="/Users/dion/Library/Application\ Support/Garmin/Devices/${GARMIN_DEVICE_NUMBER}/History/2014-06-07-055251.TCX"

to:
Code:
sourcefile="/Users/dion/Library/Application Support/Garmin/Devices/${GARMIN_DEVICE_NUMBER}/History/2014-06-07-055251.TCX"

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-16-2014
That worked.Smilie

I was not aware that the issue was you should not escape and quote.

In terms of the spaces, I agree; however, on apple, they create a folder called "Application Support" and there is no way to get around that, all software expects things to be in that folder. It is one of the annoyances of apple.

Thanks for your help. I really appreciate it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Connecting and changing variables in Bash script

#!/bin/bash X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H) Y=$(</home/cogiz/topcardinplay.txt) # KS A=( "${Y::1}" ) B=( "${Y:1}" ) for e in ${X}; do if ]; then # searching for valid cards K,S or 8 ... (0 Replies)
Discussion started by: cogiz
0 Replies

2. Shell Programming and Scripting

Sending awk variables into curl in a bash script

Hello experts! I have a file1 with the following format (yr,day, month, hour,minute): 201201132435 201202141210 201304132030 201410100110 ... What i want to do is to assign variables and then use them in the curl command to download the text of each event from a web page. What I have... (6 Replies)
Discussion started by: phaethon
6 Replies

3. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

4. Shell Programming and Scripting

Problem with positional variables in BASH

Hello, my problem is simple & I searched a lot but I couldn't find anything about it: Basically I'd like to pass $i to a variable, $i being the positional variable; but it is unknown in the beginning so I can't do it like eg. myvar=$3, it HAS to be the "i".. First, I tried myvar=$($i) ... (8 Replies)
Discussion started by: timmyyyyy
8 Replies

5. UNIX for Advanced & Expert Users

Bash script with export variables

Hi all guys, how you can read in thread title, I'm deploying a bash script in which I have to export some variables inside it. But (I think you know) the export command works only inside the script and so, on exit command, the variables aren't set like I set inside the script. Consequently in... (8 Replies)
Discussion started by: idro
8 Replies

6. Shell Programming and Scripting

Multiple Variables for BASH script

Hello, I am new to the whole "scripting" thing. Below is the script that I have so far and where i need the Variables to go (VAR#) #!/bin/bash #Sample Script VAR1= echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." read VAR1 case $VAR1 in 1) echo "Pick... (4 Replies)
Discussion started by: eclerget
4 Replies

7. Shell Programming and Scripting

Passing variables problem - Bash

I have a following problem: #!/bin/bash NUM=`cat accounts | wc -l`; for i in {1..$NUM} do account=`awk "NR==$i" accounts`; echo -e "\nAccount: $account\n"; sudo ./backup_maildir $account; done "accounts" is a file with regular e-mail addresses, one in each line.... (2 Replies)
Discussion started by: bobanpetrovic
2 Replies

8. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

9. Shell Programming and Scripting

Picking high and low variables in a bash script - possible?

Is it possible to have a bash script pick the highest and lowest values of four variables? I've been googling for this but haven't come up with anything. I have a script that assigns variables ($c0, $c1, $c2, and $c3) based on the coretemps from grep/sed statements of sensors. I'd like to also... (5 Replies)
Discussion started by: graysky
5 Replies

10. Shell Programming and Scripting

Bash script to read a hostname and separate into variables

Hi All, I'm trying to concoct a bash script to use with a Puppet Implementation that will accept a hostname and break it down into variables. For example, my hostnames look like this --> machinename-group-building.example.com I'm looking for a way in the script to read until the first... (4 Replies)
Discussion started by: glarizza
4 Replies
Login or Register to Ask a Question