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
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
Sorry, there's a typo...
Should be:
if [ `uname -n | grep 'test'` ]
then
var=test
else
var=
fi
Should be:
if [ `uname -n | grep 'test'` ]
then
var=test
else
var=
fi
ASKER
I use tcsh. Do this code apply for it?
And what is fi at the end?
Thanks,
And what is fi at the end?
Thanks,
ASKER
It says expression syntax for if statement.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This would work for most.
Open in new window