Operating System - HP-UX
1833800 Members
2569 Online
110063 Solutions
New Discussion

shell script - how hard can it be

 
SOLVED
Go to solution
Adam Noble
Super Advisor

shell script - how hard can it be

Hi,

I simply want to use an if statement to see whether I should send the output of a command to my output file.

I'm using the following and only want to send the output to the file if the 1st 2 characters begin HP.

if [ $revision = "HP*" ];then

The above however doesn't work how do I do this.
5 REPLIES 5
Peter Godron
Honored Contributor
Solution

Re: shell script - how hard can it be

Adam,
how about:
if [ `expr substr "$revision" 1 2` = "HP" ]
then
.
.
fi
Ralph Grothe
Honored Contributor

Re: shell script - how hard can it be

It very much depends on the shell you are doing this in.
For instance shells such as Bash offer an expansion syntax which doesn't even require some external filter commands and yet let you extract a certain substring.
However, with HP posix shell you could do this
(but there are many other solutions conceivable)

(( $(expr "$revision" : ^HP) == 2 )) && echo do something
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: shell script - how hard can it be

Sorry, I made a mistake.
The correct terminology I was referring to when talking about Bash was "Parameter Substitution" and not "expansion".
But this is due to my English non-nativeness, I suppose.
And here's what I meant in Bash:

$ OS=$(uname -s)
$ [[ ${OS:0:2} = Li ]] && echo "we are running Linux"
we are running Linux

Madness, thy name is system administration
Adam Noble
Super Advisor

Re: shell script - how hard can it be

thanks it clearly wasn't as straight forward I was using ksh and your solution worked. Thanks to you both
Dennis Handly
Acclaimed Contributor

Re: shell script - how hard can it be

You really want to use pattern matching:
if [[ "$revision" = HP* ]]; then

As in Ralph's case, you need [[ ]]. Don't quote the HP*.