Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.:

914 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

12 Experts available now in Live!

Get 1:1 Help Now