?
Solved

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

Posted on 2003-11-11
5
Medium Priority
?
2,555 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 100 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 100 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 100 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 200 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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.:
Suggested Courses

800 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