Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > bash shell programming
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 11-24-2007   #1 (permalink)
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
morbidbubba is offline  

Reply With Quote
Old 11-24-2007   #2 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 1,004
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!
scm is offline   Reply With Quote
Old 11-24-2007   #3 (permalink)
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
morbidbubba is offline   Reply With Quote
Old 11-28-2007   #4 (permalink)
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
apoorv_khurasia is offline   Reply With Quote
Old 11-28-2007   #5 (permalink)
Super Moderantor
 
techieMoe's Avatar
 
Join Date: Aug 2004
Location: Texas
Posts: 9,148
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
techieMoe is offline   Reply With Quote
Old 11-28-2007   #6 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 598
Sorry, I misunderstood the question...

Regards

Last edited by Franklin52; 11-28-2007 at 05:57 PM.. Reason: misunderstood the question
Franklin52 is offline   Reply With Quote
Old 11-29-2007   #7 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 598
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
Franklin52 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting FLAC to Mp3 in Suse? Pumalite SuSE Linux Help 13 04-12-2007 01:59 AM
Linux LiveCD with C Shell Programming cyberquest Redhat / Fedora Linux Help 0 08-07-2006 08:04 AM
Porting a shell script from BASH to ASH Violao Linux Programming & Scripting 1 06-04-2006 02:48 PM
Install_all script not working Red Hat 9 Aston Villa Installation 8 08-22-2003 12:37 AM

Free Magazines
Free Network Mapping Tool for Microsoft® Office Visio® Professional 2007 Users
Don't map your network by hand – let LANsurveyor Express for Microsoft Visio Professional 2007 automatically create network diagrams for you.
subscribe
Free eBook:"Vulnerability Management for Dummies"
Get all the Facts and See How to Implement a Successful Vulnerability Management Program.
subscribe
Google vs The World: The Battle of the Message Security Vendors
With such a powerful name behind it, Google Message Security stands out in a sea of products that do exactly the same thing - or so they say.
subscribe

Safe, Secure Backup


All times are GMT. The time now is 04:22 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2