Solved

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

Posted on 2011-09-28
5
393 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now