Results 1 to 6 of 6

Thread: bash scripting and quoting

Hybrid View

  1. #1
    Guest

    bash scripting and quoting

    In a nutshell can someone explain me the difference between the various methods of quoting. I don't seem to understand when to use ' and ` or " characters. And AFAICT it _does_ make a difference.

  2. #2
    Guest

    Re: bash scripting and quoting

    Backquotes (`) are used to store the output of a command in a variable. For example:

    Code:
    OUT = `ls`
    echo $OUT
    This will put the content of 'ls' in the variable OUT


    Single-quotes will print verbatim whatever is within them (including escape characters), while double-quotes will interprete escape characters:

    Code:
    echo '\n' # This will print \n to the screen
    echo "\n" # This will print a new line

  3. #3
    Guest

    Re: bash scripting and quoting

    Cool, that's what I call a precise answer thanks a bunch.

    Now I think I see what my problem is. I guess I will need to nest the quotes. That's not possible, is it?

    This is what I've got a problem with
    Code:
    gkrellmPID=`ps ax|grep [g]krellm|awk '{print $1}'`; \
    if [ -z ${gkrellmPID} ]; \
     * * *then echo "AddToMenu DesktopMenu Start gkrellm Exec exec gkrellm -g +0+460"; \
     * * *else echo "AddToMenu DesktopMenu Stop gkrellm Exec exec kill $gkrellmPID"; \
    fi;
    This works if I call it from the command promt put it has to be called with sh -c from within a menu. So I would need another pair of quotes around it, don't I ???

  4. #4

    Re: bash scripting and quoting

    Code:
    echo '\n' # This will print \n to the screen
    echo "\n" # This will print a new line
    This is technically incorrect.

    In a programming language, yes this is true, but the echo command works differently. If you want to interpret the escaped characters, you have to use the -e option, that is all that matters.

    Code:
    echo -e '\n' # This will print a new line, and
    echo -e "\n" # so will this. But these two won't:
    echo '\n'
    echo "\n"
    I know why it behaves like that, but ultimately it's not important.

    Demian, I'm not sure what's wrong with your quotes. The only problem I can see with your script is that the way you end every line with a semi colon and then a backslash (; \) accomplishes nothing (they in fact cancel each other out to produce the same effect as if there was nothing there).

  5. #5
    Guest

    Re: bash scripting and quoting

    Demian, I'm not sure what's wrong with your quotes. The only problem I can see with your script is that the way you end every line with a semi colon and then a backslash (; \) accomplishes nothing (they in fact cancel each other out to produce the same effect as if there was nothing there).
    Uhm, yeah, I guess that's a bit redundant. You're right, the above script works. But now I have to call the script like this: sh -c 'script goes here'. So I need to quote the whole thing again. Well, actually I want to call this script with the PipeRead command from within an fvwm menu which basically is equivalent to the sh -c thing. So the menu item looks like this:
    Code:
    AddToFunc MakeDesktopMenu
    + I DestroyMenu recreate DesktopMenu
    + I PipeRead 'gkrellmPID=`ps ax|grep [g]krellm|awk \'{print $1}\'`; \
     * * *if [ -z ${gkrellmPID} ]; \
     * * * * * *then echo "AddToMenu DesktopMenu Start-gkrellm Exec exec gkrellm -g +0+460"; \
     * * * * * *else echo "AddToMenu DesktopMenu Stop-gkrellm Exec exec kill $gkrellmPID"; \
     * * *fi;'
    Actually I realize that I do need the ; \ because otherwise fvwm tries to interpret the lines stating with if, then,... and doesn't know what to do. Anyway, I tried to escape the single quotes around the awk "script" but that doesn't work. The message that's spit to the console from the above script is sh: [: too many arguments.

    I was thinking of an alternative way to get the PID of a program that doesn't require two different sets of quotes but couldn't com up with anything.

  6. #6

    Re: bash scripting and quoting

    But now I have to call the script like this: sh -c 'script goes here'.
    That would be what your (currently nonexistant) bangpath is for.

    Well, actually I want to call this script with the PipeRead command from within an fvwm menu which basically is equivalent to the sh -c thing.
    Well first of all, why not just put it into a script and then have Piperead run the script??

    Actually I realize that I do need the ; \ because otherwise fvwm tries to interpret the lines stating with if, then,... and doesn't know what to do.
    Putting it into it's own script would fix that.

    The message that's spit to the console from the above script is sh: [: too many arguments.
    The fix for this is to give less arguments to [.
    Code:
    if [ -z "${gkrellmPID}" ]
    ($gkrellmPID has a space in it and [ is barfing over it).

Similar Threads

  1. Bash scripting and the stderr
    By 10Dedfish in forum Programming
    Replies: 2
    Last Post: 12-30-2003, 01:12 PM
  2. bash scripting question
    By seanmclemon in forum Programming
    Replies: 7
    Last Post: 09-07-2003, 12:58 AM
  3. Bash scripting question
    By kenshi in forum Programming
    Replies: 6
    Last Post: 01-02-2003, 08:36 PM
  4. need some bash scripting help
    By mountainman in forum Programming
    Replies: 2
    Last Post: 08-30-2002, 12:44 PM
  5. Bash Scripting
    By cuban in forum Programming
    Replies: 3
    Last Post: 09-10-2001, 01:03 AM

Bookmarks

Posting Permissions

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