Solved

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

Posted on 2003-11-11
5
2,549 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
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 20

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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…
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.

837 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