File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes Executing shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Executing shell script" Watch "Executing shell script" New topic
Author

Executing shell script

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
This is in continuation to earlier post about attaching timestamp to copied file.

I created a batch file. when I clicked on the templating.bat for the first time in SSH. Now when I click on the templating.bat, it just opens the notepad page and shows content of the batch file rather than running it. And at the top of the page, it shows a number how many times the file is opened e.g. templating(9).bat. So why is it behaving like this, rather than running the file and creating a copy ?

Thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

OK, I am very confused with what you are doing here. SSH is the secure shell, and that is all it is - a shell. It gives you a prompt where you can type commands (such as asking for a directory listing or starting an editor that works within the shell). It normally does not give you any way of clicking on a file or an executable since it is just a connection to a remote computer - SSH itself does not know what the remote computer's capabilities are, so it would not know what to do with the file.

A lot of shells and programs that wrap shells (such as putty) have a default mode whereby if you double click on a word displayed in the terminal then that word is copied into the clipboard. This can be useful, for example, when copying the filename from higher up in the screen's output and then pasting that file name into the current command line.

So when you say that you are clicking on the templating.bat file and it is opening the file in notepad, I really don't know what you are doing. It does not sound like Unix at all.

Can you provide a little bit more information?

As an aside, under Unix like systems, scripts are usually referred to as scripts , not batch files. Batch files (with the .bat extension) are relics of Microsoft Windows. Normally I would not expect to see a file named 'templating.bat' on a Unix system. If it were a shell script, I would not be surprised if it was named 'templating.sh'. If it was an awk script it might be named 'templating.awk'. But scripts don't really care what the extension is.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
I am using a utility in which two windows are displayed. One windows for the server and one for the loacl machine. I can download the file from server on the loacl m/c and modify it and upload it. Now on the server there is modified file.

When I click templating.bat file on server, it does not run but just opens up. On the unix, file extensio is not of much importance. If I have templating.cfg and templating.bat file on the server and if I click on templating.cfg, that file will open up. If I click on templating.bat, It also just opens up. How to make sure, when tempalting.bat file is clicked, it runs ? If I change extension from templating.bat to templating.sh, will it solve the problem ? If not, then how to run the templating.sh file so that it performs the copy operation ?

thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

I suspect that this may be something specific to whatever utility you are using. Do you know that your utility has the ability to run scripts on the server?

Do you have the execute bit turned on for your script? That is - any file can, in theory, be executable. As I mentioned earlier, the system does not care what the extension is - it can be 'exe', or 'bat', or 'sh', or 'awk' or nothing at all. The system just cares whether you have explicitly stated that the file should be executed. It determines this by looking at the permissions for the file. When you do a "long" listing, you can see what permissions have been set for a file. For example:


The first file has the bits "rwx" set for the user who owns the file (andrew), meaning that the owner can read the file, write to it, and execute it. Anyone in the group 'staff' can also read it or execute it (r-x) but cannot write to the file. Likewise all others who can get to the file can read it or execute it.

The second file is the source file for my application. Since it is just source, it does not have any execute bits set. The owner (me) can read or write to the source file (rw-). Anyone who is a member of the 'staff' group can read the file only (r--), while the other permissions are set so that nobody else can even view the contents of the file (---).

If you do not have the execute bit set, then your script will not execute.

The simplest way to set the execute bit is to run the command chmod +x templating.bat This will turn on the execute bit for owner, group, and other for the file. Similarly you could turn off the execute bit by running the command chmod -x templating.bat There are far more options - for example you could specify that only the user who owns the file may execute it by running the command chmod u+x templating.bat Take a look at the man page for chmod for more information.

I would suggest you verify that the executable bit is set for your script as the very first step.

If that doesn't help, then perhaps your utility only recognizes certain file extensions as being scripts that can be executed - perhaps it is worth changing the filename to 'templating.sh'. Alternatively, look in the configuration options for your utility for any way of configuring known file extensions - probably an advanced option.
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
I added execute option for all users on this file.

To run this file , I have to go in the directory, where this file is located and just type "templating.sh" on command prompt. right ?

I renamed templating.bat as templating.sh. Now I went in the directory , where templating.sh is located and ran from that directory by typing "templating.sh" on cmd prompt.
Output is "ksh: templating.sh: Not Found."

templating.sh is located in this directory only. So why am I getting such an error message ?

After adding execute permission for all users, I tried to run this file from the utility. But still it just opens the file like "edit", rather than running it.

So this file is neither running from within utility nor its running from cmd prompt. Any feedback please ?

thanks


Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

jignesh soni wrote:To run this file , I have to go in the directory, where this file is located and just type "templating.sh" on command prompt. right ?

Almost - you normally have to be explicit about where to find the program you wish to run. The only exception to that rule is if the program is in the path. Type the following at the command line:Any program that is in one of the directories listed will be found automatically if you type the program's name. For all other programs you have to explicitly provide the path to the executable.

Try typing the following:Note that there is a leading period (.) before the slash. That period means that you want to use the current directory.
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
can we use boolean in shell script to select a latest file only if two files are different ?

------------------------------------------------------------------------------------------

if [diff /local/config/templating /local/config/backup/$(ls -1rt | tail -1rt) ] ; then

cp /local/config/templating.cfg /local/config/backup/templating_$(date '+%d%m%y_%H%M')

----------------------------------------

I saved this script in a file template.sh and trying to run it on cmd line. But I am getting an error "./template.sh: cannot execute"
whats wrong with this script ?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

jignesh soni wrote:can we use boolean in shell script to select a latest file only if two files are different ?

Well the if, while, and do ... while options all require parameters that are either boolean or evaluate to a boolean value. That was what I was hinting at when I mentioned the diagnostic code from diff - it returns zero if the files are the same, or one if the files are different.

Your code is nearly all there - looking good.

I am not familiar with what the -t option does within the tail command, however I suspect it is redundant. Certainly a "-r" option is redundant as reversing the order of a single line output from tail will still result in the same line being displayed. I would simply go with a -1 option to tail.

Once again, I would be tempted to break it into multiple lines to try and make it more obvious what each part is doing:


The --brief option to diff will result in almost no output, and also means that diff will exit as soon as it knows that there is a difference between the 2 files - this may not be important in a small file like a configuration file, but it can be valuable if the files are extremely large. Since we don't really care about the output of the diff command, I am sending what little output there is to /dev/null - effectively throwing it away.

Then I take a look at what the diagnostic code of diff is. The "if [[ $? -eq 1 ]]" statement. $? will always contain the diagnostic code of the last run command, so it will either contain zero if the files are the same, or one if they are different. I can then test if the diagnostic code is equal (-eq) to 1, which results in a boolean true or false.

By the way, I prefer the compound "[[ ... ]]" to the builtin "[ ... ]", but in this particular case it is up to you which you use. I find it a little easier to write a compound test using the compound test format (who would have guessed? ) than the builtin. For example, consider the following 2 if statements:



Both perform the same 2 tests, but I personally prefer the second format. However I am not the person who has to maintain your code. So choose whatever you are more comfortable with.

jignesh soni wrote:I saved this script in a file template.sh and trying to run it on cmd line. But I am getting an error "./template.sh: cannot execute"
whats wrong with this script ?


Try running the following 3 commands and provide us with the output:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing shell script
 
Similar Threads
run java class files by double clicking(urgent)
not enough session space
Running batch file from JSP
help for .batch files
JEE5 bean and war deployment
trouble with jar and main class
applet - servlet communication
Out of memory Problem
Shortcut for Project Execution
Learning JBoss
How to suppress duplicate thread?
Windows PATH update
Help resolving my NoClassDefFoundError
about splash screen with jar file...
print Screen from a jsp
JWS: Executable jar runs but doesn't show the data
My war file are not getting unzip in the webapps directory