Results 1 to 10 of 12
I feel I should know this but I"m drawing a blank:
How do you find a character in a string that's from a variable or from a command you've just ...
- 06-20-2007 #1Just Joined!
- Join Date
- Jun 2005
- Location
- San Francisco, CA
- Posts
- 54
how does you find a character in a string (not a file)?
I feel I should know this but I"m drawing a blank:
How do you find a character in a string that's from a variable or from a command you've just run (`command`)? Is there a single shell command that does this?
- 06-20-2007 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
It depends on the context. Are you looking for a specific string or just say the third character of the string for example.
To just check for a character you could use grepOther wise there are a host of options for checking permissions.Code:grep A $(command_to_be_checked) grep A $SOME_VARIABLE
- 06-21-2007 #3Just Joined!
- Join Date
- Jun 2005
- Location
- San Francisco, CA
- Posts
- 54
Cool, thanks BTR. From its man page I couldn't tell that grep could check strings, I thought it would always interpret any string after the options and character as a filename. Btw, how is grep able to know not to look for the character in a file in the current directory by that name?
- 06-21-2007 #4Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
grep requires a file name to search, or a wildcard/globbing. So
Most Unix commands can read from standard input rather than a file, as Unix treats everything as a file. For a lot of commands however you need to specify a hyphen for standard inputCode:grep something somefile.txt
In the cases above the command you want to run is evaluated because of the $(somecommand) and thus this is the input for grep. This is the more modern way, in older scripts you will see the back ticks `somecommand`.Code:grep something -
An alternative would beThis is piping output to grep (standard input).Code:somecommand |grep something
If you are interested check out the Advanced Bash Scripting guide at tldp.org - you can read it online or download the PDF. It's well worth the read if you will be using command line at all.
- 06-21-2007 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 06-24-2007 #6Just Joined!
- Join Date
- Jun 2005
- Location
- San Francisco, CA
- Posts
- 54
BigTom and GhostDog, thanks for your help. I'll implement one or both of these when I get the next chance.
- 06-27-2007 #7Just Joined!
- Join Date
- Jun 2005
- Location
- San Francisco, CA
- Posts
- 54
It looks like my question has evolved a bit:
Is there a way test for a string within a variable, so that the test can be used in an if statement? I'm trying to have it all happen in the if statement, not by running commands and then doing RESULT_VARIABLE=$? previous to the if statement.
- 06-27-2007 #8Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
hi thr....
I'm a newbie so my answer might not be so efficient..
let us say that u wanna check for the string "ss" in a variable '$line':
then one can do this:
if [ "${line##*ss*}" != "$line" ]; then
echo \'ss\' is found
else
echo \'ss\' is not found
fi
cheers
- 06-27-2007 #9Just Joined!
- Join Date
- Jun 2007
- Location
- India
- Posts
- 16
Or if you want it to be generalized then
$str=ss
if [ "${line##*$str*}" != "$line" ]; then
echo $str is found
else
echo $str is not found
fi
- 06-28-2007 #10Linux User
- Join Date
- Aug 2006
- Posts
- 458
you can use case
Code:# line="lkjsflsltestlksjflsd" # v="test" # case $line in *$v*) echo "found";; *) echo "not found";; esac


Reply With Quote
