LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-24-2003, 05:13 AM   #1
KDE4me
Member
 
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Rep: Reputation: 15
How do I escape $# in shell scripting?


Hi,
I've tried to search for this on google and this site's search engine but they all can't recognize these characters so ...

I'm trying to test for how many parameters a command line argument is given and I use

if [ $# -eq 1 ]
then ...

anyway when I do this in vi & bash they don't recognize everything after #. Is there some way to escape this?

Thanks.
 
Old 06-24-2003, 05:51 AM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 46
The following works for me
Code:
#!/bin/sh
if [ $# == 1 ]; then
  echo "Met condition"
fi
Code:
[jh@e128 jh]$ ./test.sh
[jh@e128 jh]$ ./test.sh bla
Met condition
HTH

Jamie...
 
Old 06-25-2003, 06:26 PM   #3
apeekaboo
Member
 
Registered: Apr 2003
Location: Stockholm/Sweden
Distribution: Kubuntu, Debian, Slax
Posts: 91

Rep: Reputation: 16
Don't know why it doesn't work. It works for me...
Are you sure the $# is the problem?
Usually I always quote the variables in my tests, even though it's not always necessary.
I just saves me some hassle whenever it does matter...
Code:
if [ "$#" -eq 1 ]; then
        echo "you gave 1 argument"
fi
You could test your script by executing it with 'bash -x ./scriptname' to see what really happens.
 
Old 06-25-2003, 11:35 PM   #4
turnip
Member
 
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
vi will see the # as a comment. It will still run as a shell script. I see this all the time in my scripts editing them in vi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
SuidPerl Mail Shell Escape Vulnerability jeremy Linux - Security 1 04-27-2005 04:01 AM
bach scripting and escape characters... Bud-froggy Linux - Newbie 3 07-09-2004 05:55 PM
Escape character not working in shell script philipz Programming 1 04-29-2004 10:58 AM


All times are GMT -5. The time now is 11:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration