Solved

how to create a .sh file to execute a series of commands

Posted on 2003-11-11
5
2,553 Views
Last Modified: 2013-12-27
Hi,

   I have some set of commands to execute frequently, when i keep each command in separate file with extension .sh and try to run them they were working fine. But when i try to put them together they were not working , i am new to unix/solaris pls give me tips on this.

Example
CLASSPATH=$CLASSPATH:/xyz/abc.jar
export CLASSPATH
java Main

thanks,
KK

0
Comment
Question by:hyder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 18

Accepted Solution

by:
liddler earned 25 total points
ID: 9723575
#!/bin/sh
CLASSPATH=$CLASSPATH:/xyz/abc.jar
export CLASSPATH
/path/to/java Main

make sure your file is executeable, i.e.
chmod a+x my_script.sh
then run it
./my_script.sh

0
 
LVL 24

Assisted Solution

by:shivsa
shivsa earned 25 total points
ID: 9723595
these should work.
if u want more information to debug scripts.
try to set -x in the beginning and u can see where it is failing.

check where the java program with
1. #which java.

suppose it give /usr/local/bin.

-----
#!/bin/sh
CLASSPATH=$CLASSPATH:/xyz/abc.jar
export CLASSPATH
/usr/local/bin/java Main.
-----

0
 

Author Comment

by:hyder
ID: 9726036
Hi,

 Thanks for the response. I tried and its working fine. I am basically looking to have a .sh file to set ANT_HOME and run it before i run the ant scripts.

I wrote the file
#!/bin/ksh
ANT_HOME='/sabc/mydir/apache-ant'
export ANT_HOME
echo $ANT_HOME

when i run the above file i am able to see the ANT_HOME  value set. But after this when i tried to type the value of ANT_HOME again it is empty. I am doining this in the same session. I believe its opening a new shell to run the script and not reflecting the values to the parent shell. Pls suggest how to overcome this issue.

/>test.sh
/sabc/mydir/apache-ant
/>echo $ANT_HOME
/>
thanks,
H
0
 
LVL 21

Assisted Solution

by:tfewster
tfewster earned 25 total points
ID: 9727238
Correct, the script is starting a sub-shell, which inherits variable settings from its parent shell - But it doesn't work both ways.

To set the variables in the current shell, "source" or "dot in" the script :
sh, ksh -  `. /path/to/script` # Note the space after the "."
csh -  `source /path/to/script`
0
 
LVL 4

Assisted Solution

by:Otetelisanu
Otetelisanu earned 50 total points
ID: 9728878
You must start the script

. ./script

with . the envirnemens go to your schell

Example:

[root@pluto:/db/xx]
[root@pluto:/db/xx] cat ttt
#!/bin/ksh

ANT_HOME='/sabc/mydir/apache-ant'
export  ANT_HOME

[root@pluto:/db/xx]
[root@pluto:/db/xx]
[root@pluto:/db/xx] echo $ANT_HOME

[root@pluto:/db/xx] . ./ttt
[root@pluto:/db/xx] echo $ANT_HOME
/sabc/mydir/apache-ant
[root@pluto:/db/xx]

0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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 previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

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