Find the answer to your Linux question:
Results 1 to 7 of 7
Hey, I was writing a BASH shell program in xfce linux, and Ive run into a little problem. My program name is prog and after i make the program executable ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    2

    Unhappy bash shell programming

    Hey,
    I was writing a BASH shell program in xfce linux, and Ive run into a little problem.
    My program name is prog and after i make the program executable with chmod, I type in the following on my command line:
    prog $HOME *.
    prog is my command, $HOME is to be used by the program as a string "$HOME", rather than the environment variable. Likewise for *, I want to use it as the string "*", rather than a wildcard.

    The code for my program;prog; is as follows:

    identifier=$1;
    and to access the other variables i.e. *, I just use the shift command.
    How do I quote the value of $1 in a way that the variable stored in $1 i.e. $HOME is treated as a string $HOME, rather than puting the value of my home directory into the variable identifier.
    Thanks

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Either prefix the $ with a backslash (\$HOME) or enclose it in single quotes ('$HOME').

    Play with echo to see the effect:
    Code:
    echo $HOME
    echo \$HOME
    echo "$HOME"
    echo '$HOME'
    Learn to understand and use quoting properly and your scripts will be much better!

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    2
    See, Im facing this problem because of a shell scripting assignment.
    Now the arguments will be passed, arbitrarily to my program.
    The assignment requires me to use the value of $1 as a user id and all the other arguments i.e. $2 onwards as file names. Now my program has to go through all the files specified from $2 onwards and find the user id mentioned in $1 and some associated data with it.
    My problem is as to how can I treat all argument values i.e. $1 onwards as non special. I have no control over the argument values, so I cant quote them on the command line. So in my code if I have the following line
    id=$1, then how do I quote the value of $1 so that the string value of whatever is stored in $1 gets stored in id.

    I found out that if I use the following command:
    read 'id', the everything stored in id will be a non special i.e. $HOME will be stored as $HOME. Is there any way to pipe my command line arguments in the read command?
    Thanks

  4. #4
    Linux Enthusiast apoorv_khurasia's Avatar
    Join Date
    Feb 2005
    Location
    Laurasia
    Posts
    624
    Oh no. And no. This is an assignment problem. This is against the forum rules. Someone is going to come here and lock this thread. And this problem is very simple so you might be able to find the solution yourself.
    "There is no sixth rule"
    --Rob Pike
    Registered Linux User: 400426 home page

  5. #5
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    I'm allowing this thread because the poster isn't asking us to do his or her homework for them, and instead asking a specific question about something they're trying to do. Carry on.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Sorry, I misunderstood the question...

    Regards
    Last edited by Franklin52; 11-28-2007 at 05:57 PM. Reason: misunderstood the question

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The parameters are expanded when the shell invokes your script, so the script receives the expanded parameters. The only way is to quote them.

    Regards

Posting Permissions

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