Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to check "if matches" statement for a string in shell script in UNIX?

Hi,
I have an application running on Unix. I want to write a shell script that will detect whether I am on test machine or not.

The test machine name includes the word test in it. (i.e. myapplicationtest-00-abc)

The production machine does not include it. (i.e. myapplication-00-abc)

So depending on the machine type, I want to assign the value test to a variable. Then I will use this variable in my following commands.

Sudo code
$machineName = uname -n;
 
if ($machineName includes 'test')
{
$var=test
}
else{
$var=''
}

rsync -avz --delete /opt/mydir1/mydir2 root@myapplication$var-00-abc:/opt/mydir1/mydir2

Open in new window


Avatar of jeremycrussell
jeremycrussell
Flag of United States of America image

What shell?

This would work for most.
if [ `uname -n` | grep 'test'` ]
  then
    var=test
else
    var=
fi

Open in new window


Sorry, there's a typo...

Should be:


if [ `uname -n | grep 'test'` ]
  then
    var=test
else
    var=
fi
Avatar of Tolgar
Tolgar

ASKER

I use tcsh. Do this code apply for it?

And what is fi at the end?

Thanks,
Avatar of Tolgar

ASKER

It says expression syntax for if statement.
ASKER CERTIFIED SOLUTION
Avatar of jeremycrussell
jeremycrussell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial