Solved

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

Posted on 2011-09-28
5
397 Views
Last Modified: 2012-05-12
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
Comment
Question by:Tolgar
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 36718409
What shell?

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

Open in new window


0
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 36718420
Sorry, there's a typo...

Should be:


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

Author Comment

by:Tolgar
ID: 36718491
I use tcsh. Do this code apply for it?

And what is fi at the end?

Thanks,
0
 

Author Comment

by:Tolgar
ID: 36718540
It says expression syntax for if statement.
0
 
LVL 9

Accepted Solution

by:
jeremycrussell earned 500 total points
ID: 36718871
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question