Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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


0
Tolgar
Asked:
Tolgar
  • 3
  • 2
1 Solution
 
jeremycrussellCommented:
What shell?

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

Open in new window


0
 
jeremycrussellCommented:
Sorry, there's a typo...

Should be:


if [ `uname -n | grep 'test'` ]
  then
    var=test
else
    var=
fi
0
 
TolgarAuthor Commented:
I use tcsh. Do this code apply for it?

And what is fi at the end?

Thanks,
0
 
TolgarAuthor Commented:
It says expression syntax for if statement.
0
 
jeremycrussellCommented:
the 'fi' is closing the if-then-else statement, this syntax works for the Bourne and Korn shells..

For tcsh or csh, use something like


#!/bin/tcsh

set u = `uname -a | grep test`
if ("$?") then
 set var = ""
else
 set var = "test"
endif

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now