run the file based on environment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting run the file based on environment
# 1  
Old 09-30-2011
run the file based on environment

Hi,

I needed to run a script based on the environment..I'm just having some issue with the script..can someone help me ?

Code:
 
#!/bin/ksh
pat01=`uname -a | awk '{printf "%s", $2}'`
 
#if it is server 1 run this file
if [$pat01 == 'cpcmlt01'] ; then
echo "run this file"
 
#if it is server 2 run this file
if [$pat01 == 'cpcmlt02'] ; then
echo "run the second file"
fi
fi


Last edited by moe458; 09-30-2011 at 12:24 PM..
# 2  
Old 09-30-2011
Code:
#!/bin/ksh -x
pat01=`uname -a | awk '{printf "%s", $2}'`

if [ $pat01 == 'cpcmlt01' ] #if it is server 1 run this file
then
   echo "run this file"
else
   if [ $pat01 == "cpcmlt02" ] #if it is server 2 run this file
   then
      echo "run the second file"
   fi
fi

Take off the -x (first line...)
if it works, else, send us the output

Last edited by vbe; 09-30-2011 at 12:41 PM.. Reason: typos
# 3  
Old 09-30-2011
Ok I had some time to test so (changed the names of the servers so I can test...)
Code:
#!/bin/ksh
pat01=$(uname -a | awk '{printf "%s", $2}')

if [ $pat01 == 'oslo4' ] #if it is server 1 run this file
then
   echo "run this file"
else
   if [ $pat01 == "oslo6" ] #if it is server 2 run this file
      then
      echo "run the second file"
      exit 2
   fi
   echo "Not concerned..."
   exit
fi
echo "Finish"

# 4  
Old 09-30-2011
1) You have no spaces between the brackets and the contents.
2) == only works in certain shells, and even then, only inside [[ ]]. Use =.
3) You should quote all your variables in case any of them are blank.
4) awk's printf usually needs () brackets.
5) You have your if's nested when you didn't want or need them to be.

So:

Code:
#!/bin/ksh
pat01=`uname -a | awk '{printf("%s", $2)}'`
 
#if it is server 1 run this file
if [ "$pat01" = 'cpcmlt01' ] ; then
echo "run this file"
fi

 
#if it is server 2 run this file
if [ "$pat01" = 'cpcmlt02' ] ; then
echo "run the second file"
fi

But I think you could do it more elegantly with case:

Code:
case `uname -a | awk '{printf("%s", $2)}'` in
cpcmlt02)
        ;;
cpcmlt02)
        ;;
*)     echo "Unknown server"
        ;;
esac

In fact, you might not need uname at all:

Code:
case $HOSTNAME in
cpcmlt02)
        ;;
cpcmlt02)
        ;;
*)     echo "Unknown server $HOSTNAME"
        ;;
esac

# 5  
Old 09-30-2011
Output:
Code:
oslo4:/export/home/vbe $ ./debugscript 
run this file
Finish
oslo4:/export/home/vbe $ 
#now set $pat01 == 'oslo5'
oslo4:/export/home/vbe $ ./debugscript 
Not concerned...
# else if [ $pat01 == "oslo4" ]
oslo4:/export/home/vbe $ ./debugscript 
run the second file

# 6  
Old 09-30-2011
I guess it got it to work ..thank you guysSmilie

Code:
 
pat01=`uname -a | awk '{printf "%s", $2}'`
 
#if it is server 1 run this file
if [ $pat01 == "cpcmlt01" ] ; then
echo "run this file"
 
#if it is server 2 run this file
if [ $pat01 == "cpcmlt02" ] ; then
echo "run the second one"
fi
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Run shell script based on date file

Hi Team, I have a to run a script based on a date present in a different file which updates everyday. Kindly help with the solution. My current execution : ksh scriptname.sh 10152019. But here i want to enter this date from a file which gets updated daily. My appraoch : date file location:... (3 Replies)
Discussion started by: midhun3108
3 Replies

2. Shell Programming and Scripting

Run script through cron with user environment variables

Hi everyone, I wrote a script that is supposed to be run by cron on a daily basis. It works just fine if I run it manually, but due to a lack of environment variables (which are available during my user session but not when cron runs the script) it keeps failing to run successfully. Here's the... (2 Replies)
Discussion started by: gacanepa
2 Replies

3. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

4. Shell Programming and Scripting

help with script parameter checking based on environment

I need to check if the parameters are correctly passed based on the Environment I am in. For e.g when I am in dev the 1st paramter needs to be either A OR B OR C OR D similarly when I am in qa the parameter needs to be either e or f so i need to write a case staement or a if statement to... (1 Reply)
Discussion started by: dsravan
1 Replies

5. Shell Programming and Scripting

How to run csh environment parameters from k-shell script?

Hi Guys, I need to run an alias from a k-shell script ,for example 10204, which points to : source $ORACLE_BASE/scripts/cshrc/10204/.cshrc Meaning , I need to run in my k-shell script with the env settings parameters of the alias. How to do it in k-shell? Thanks in advance, Nir (2 Replies)
Discussion started by: nir_s
2 Replies

6. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

7. Shell Programming and Scripting

help with email to be triggered based on fatal error detection from batch run log file neded

Hi, I require need help in two aspects actually: 1) Fatal error that gets generated as %F% from a log file say ABClog.dat to trigger a mail. At present I manually grep the log file as <grep %F% ABClog.dat| cut-d "%" -f1>. The idea is to use this same logic to grep the log file which is... (1 Reply)
Discussion started by: zico1986
1 Replies

8. Shell Programming and Scripting

To run a script based on the value in text file

I have a Text file as shown below /* text file begins---------- ----------- Monthly files Loaded ------------- input_file record_count load_count reject_count ------------ ----------- ----------- ----------- 1_IN.txt 221935 221935 0 2_IN.txt 270668 270668 0 3_IN.TXT 231666 80370 151296... (7 Replies)
Discussion started by: nani1984
7 Replies

9. UNIX for Advanced & Expert Users

how to set the environment variable at run time

hi, I have one environment variable like path in my system.But in my program i need to change that path .suppose it has a value "config" now i need to chage it as "config1" or something else.i need to use that variable for complete project.It means at first it will use the old path but after... (4 Replies)
Discussion started by: sada@123
4 Replies

10. Programming

How to compile and run C++ programs in UNIX environment?

:( :confused: Does anybody here know how to compile and run C++ programs in UNIX enviroment? I am so confused. Any help on this would be greatly appreciated! Thanks! (5 Replies)
Discussion started by: Kahuashi
5 Replies
Login or Register to Ask a Question