Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now