LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2011, 01:57 PM   #1
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 27

Rep: Reputation: Disabled
how do i test $ in if statement


Can someone help me figure out how do I test $(dollar symbol) in the if statement? Let's say $1="$_ABCE". I want to say that if $1, the argument, contains a "$" then I want to print a special instruction. I've tried this and several other things by changing double quotes to single quotes but still couldn't get it to work:

argum="`echo "$1"`"
if [[ "$argum" =~ "\$" ]]
then
echo " "
echo "For Options symbols, you need a \ in front of the symbol"
fi

Thx.
 
Old 11-11-2011, 02:32 PM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
The simplest method is using single quotes. Why you use echo in first line?
Code:
argum="$1"
if [[ "$argum" =~ '$' ]]
then
  echo
  echo 'For Options symbols, you need a \ in front of the symbol'
fi
And test it with
Code:
./test.sh '$_ABCE'
 
Old 11-11-2011, 02:43 PM   #3
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thx eSelix but still doesn't work

> cat prtest
#!/bin/bash

#argum="`echo "$1"`"
argum="$1"
echo "$1"
#if [[ "$argum" =~ ".*\$.*" ]]
if [[ "$argum" =~ '$' ]]
then
echo " "
echo "For Options symbols, you need a \ in front of the symbol"
fi

Here's the test:

> ./prtest '$_ABCE'
$_ABCE

For Options symbols, you need a \ in front of the symbol


Here's the second test that doesn't have a $ in it:

./prtest 'ABCE'
ABCE

For Options symbols, you need a \ in front of the symbol
 
Old 11-11-2011, 02:47 PM   #4
amateurscripter
LQ Newbie
 
Registered: Nov 2011
Posts: 27

Original Poster
Rep: Reputation: Disabled
didn't work

Quote:
Originally Posted by eSelix View Post
The simplest method is using single quotes. Why you use echo in first line?
Code:
argum="$1"
if [[ "$argum" =~ '$' ]]
then
  echo
  echo 'For Options symbols, you need a \ in front of the symbol'
fi
And test it with
Code:
./test.sh '$_ABCE'
I replied to the post again, should ahve replied to this, can you check out my comment. It didn't work.
 
Old 11-11-2011, 03:04 PM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
I tested your new program and both sample arguments. It works as expected: first print your message and second not. Check that you edited and run proper file. This is your whole program or just fragment?

Please use CODE tags when you paste your code.

Last edited by eSelix; 11-11-2011 at 03:10 PM.
 
Old 11-11-2011, 03:56 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
On the right-hand side of the [[ x =~ y ]] test, quoted/escaped strings are viewed as literal, and unquoted strings are considered regular expressions. So any of the following will work:

Code:
[[ "$argum" =~ \$ ]]
[[ "$argum" =~ "$" ]]
[[ "$argum" =~ '$' ]]
[[ "$argum" =~ [$] ]]
And believe it or not, this should also work:
Code:
[[ "$argum" =~ "\$" ]]
This is due to the nature of double-quoting. Inside them, \$ escapes to a literal $, so it again works as expected. See the quoting section of the bash man page for details.

These will not work correctly:
Code:
[[ "$argum" =~ '\$' ]]	# treated as the literal string \$
[[ "$argum" =~ $ ]]	# treated as the "end of line" regex
If you can't get any of these patterns to do the job correctly, then your problem likely lies somewhere else. I tried your above script and it works perfectly for me too.
 
  


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
test statement in bash casperdaghost Linux - Newbie 8 10-13-2011 07:16 AM
[SOLVED] what does =~ means in if test statement. saini_mw Programming 3 05-09-2011 01:15 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 03:07 AM
EASY scripting question - trying to figure out a test statement Elendrael Linux - Newbie 1 09-16-2007 07:52 PM
LXer: Defence statement released in Israeli GPL test LXer Syndicated Linux News 0 04-03-2007 03:31 PM


All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration