Solved

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…

785 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