[nflug] bash syntax problem

Daniel Colascione dan.colascione at gmail.com
Tue Aug 19 23:43:52 EDT 2008


On Aug 19, 2008, at 8:49 PM, Joe wrote:
> "-x" is cool.  I don't see it in man bash.

It's one of those handy debugging features you never forget about. :-)

>
> From running it that way, it looks like I'm getting a blank  
> prepended to
> the second file name when I use \ at the end of the first argument.  I
> can't see why that would do anything.

My hunch is that you have trailing whitespace problems; line  
continuation works fine for me, and doesn't add any whitespace of its  
own.

Your second argument is prepended with a space because that's what you  
spelled. The shell with concatenate adjacent words into a single  
argument.

$ myprogram a'b'

is exactly the same as

$ myprogram ab

is exactly the same as

$ myprogram "ab"

Make sense so far?

Your second argument to cmp is:

BACKSLASH SPACE DOUBLE-QUOTE [stuff] DOUBLE-QUOTE

The shell looks at the backslash-space, and since the space is  
escaped, begins constructing the current word with it. Then it adds  
the stuff in double quotes to that word.


--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

>
>
> bigbird at sananda:~/bin$ bash -x chkpixx P8063647.JPG
> + cmp '/media/windoze/Documents and Settings/Owner/My
> Documents/Images/Rita/Olympus/P8063647.JPG' ' /media/windoze/Documents
> and Settings/Owner/My Documents/Images/Rita/Even Less Prints/ 
> P8063647.JPG'
> cmp:  /media/windoze/Documents and Settings/Owner/My
> Documents/Images/Rita/Even Less Prints/P8063647.JPG: No such file or
> directory
> + RC=2
> ....
>
> dan.colascione at gmail.com wrote:
>> Try running the script through bash -x.
>>
>> -original message-
>> Subject: [nflug] bash syntax problem
>> From: Joe <josephj at main.nc.us>
>> Date: 08/18/2008 12:04 AM
>>
>> Bash syntax often drives me batty.
>> I'm having a problem printing some digital camera pictures, so I  
>> wrote a
>> quick and dirty script to verify that the pictures I took to the  
>> photo
>> shop are the same as the ones that were on the camera flash card.
>> The script "chkpix" and it's output are below.
>>
>> The problem is in the cmp command line which can't seem to find the
>> second file to compare.  The ls and md5sum commands find the file  
>> just
>> fine, so the path is OK.
>>
>> When I run the cmp command by itself directly from the command line
>> (with no shell variables), it works fine.
>>
>> I modified the script, eliminating the '\' at the end of the first  
>> cmp
>> argument and made the command one long line and it worked.
>>
>> So, what's wrong with breaking up a long command line with one or  
>> more
>> escaped carriage returns?
>>
>> Thanks.
>>
>> Joe
>>
>> chkpix:
>>
>> #!/bin/bash
>> ## compare two pictures for identity
>>
>> cmp "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Olympus/${1}" \ "/media/windoze/Documents and
>> Settings/Owner/My Documents/Images/Rita/Even Less Prints/${1}"
>> ls -l "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Olympus/${1}"
>> ls -l "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Even Less Prints/${1}"
>> md5sum -b "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Olympus/${1}"
>> md5sum -b "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Even Less Prints/${1}"
>>
>>
>> bigbird at sananda:~/bin$ bash chkpix P8063647.JPG
>> cmp:  "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Even Less Prints/P8063647.JPG": No such file or
>> directory
>> -rwxrwxr-x 1 root ntfs-3g 634499 2008-08-06 17:10
>> /media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Olympus/P8063647.JPG
>> -rwxrwxr-x 1 root ntfs-3g 634499 2008-08-16 22:05
>> /media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Even Less Prints/P8063647.JPG
>> 5a54b325abb196f4806edb5d7a4cbf2b */media/windoze/Documents and
>> Settings/Owner/My Documents/Images/Rita/Olympus/P8063647.JPG
>> 5a54b325abb196f4806edb5d7a4cbf2b */media/windoze/Documents and
>> Settings/Owner/My Documents/Images/Rita/Even Less Prints/P8063647.JPG
>> bigbird at sananda:~/bin$
>>
>> bigbird at sananda:~/bin$ cmp "/media/windoze/Documents and
>> Settings/Owner/My Documents/Images/Rita/Olympus/P8063647.JPG"
>> "/media/windoze/Documents and Settings/Owner/My
>> Documents/Images/Rita/Even Less Prints/P8063647.JPG"
>> bigbird at sananda:~/bin$
>>
>> _______________________________________________
>> nflug mailing list
>> nflug at nflug.org
>> http://www.nflug.org/mailman/listinfo/nflug
>>
>> _______________________________________________
>> nflug mailing list
>> nflug at nflug.org
>> http://www.nflug.org/mailman/listinfo/nflug
>>
>>
>



More information about the nflug mailing list