LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2011, 12:13 AM   #1
sagar.gunjal
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
how to use exec in shell script


Hello to all,

this is my first post.

i am in need to use "exec -a java mono rename.exe" in shell script.
but when i do the same in script it gives error that "-a: not found"

plz help me to achieve this with shell script or simple C program
 
Old 12-15-2011, 12:29 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Are you using Bash? The -a option to exec is not in the POSIX spec; it is only a Bash feature. If you're using some other shell, it won't work.

Last edited by jhwilliams; 12-15-2011 at 12:30 AM.
 
Old 12-15-2011, 12:37 AM   #3
sagar.gunjal
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
yes i am using the Bash. but its noting working
 
Old 12-15-2011, 01:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please show the script so we may have a better idea of the issue?

Just to confirm, your current shell is also bash? (ie output of echo $SHELL)
 
1 members found this post helpful.
Old 12-15-2011, 01:39 AM   #5
sagar.gunjal
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
here is my script code

#!bin/sh
exec -a java mono rename.exe


also i tried c code for this as....

#include<stdio.h>
void main()
{
system("exec -a java mono rename.exe ");
}

compile this with gcc and run this but same error -a not found
 
Old 12-15-2011, 02:43 AM   #6
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
#!bin/sh

I don't know much about shell scripting but this should be

#!/bin/sh

...but maybe just a typo in the post?

Last edited by j-ray; 12-15-2011 at 02:45 AM.
 
1 members found this post helpful.
Old 12-15-2011, 04:41 AM   #7
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
j-ray is right about the missing /, but #!/bin/sh is still incorrect. -a is not an option to exec when using a POSIX shell (sh.) You want to be using bash as the interpreter, if you're going to keep the -a.

Change it to #!/bin/bash
 
1 members found this post helpful.
Old 12-15-2011, 04:59 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
jhwilliams is of course correct. I do always find it interesting that what someone says and then when they show the example are often different things.
Quote:
Originally Posted by sagar.gunjal
yes i am using the Bash
Quote:
Originally Posted by sagar.gunjal
#!bin/sh
Apart from the typo these two statements are not the same
 
Old 12-15-2011, 09:51 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.


To be fair, a lot of people don't really understand what the shebang does. They run the script from their bash consoles, but don't realize that that first line in the script is what tells the system which interpreter to use for it. The shell you launch it in doesn't matter when you execute a script directly.

And for the record, the default "/bin/sh" interpreter may actually be bash, depending on how your system is configured. But sh is always treated as a posix-compliant script, and even bash will ignore or error out on most non-posix structures inside such a script.

Of course, you can alternately run a script by directly specifing the interpreter you want as the program name, and pass the script itself to it as an argument. Run this:

Code:
bash scriptname.sh
...and it will be interpreted as a bash script, no matter what the shebang says (since the line starts with #, it's treated as a simple comment).


BTW, The c code likely doesn't work because exec is not a stand-alone program, but a shell built-in. The system function isn't told what interpreter to use for that command.
 
1 members found this post helpful.
Old 12-16-2011, 08:00 AM   #10
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by David the H. View Post
To be fair, a lot of people don't really understand what the shebang does. They run the script from their bash consoles, but don't realize that that first line in the script is what tells the system which interpreter to use for it.
Especially as there is the default /bin/bash in Linux, hence it even runs with a typo in the first line most likely in the way you expect it.

To the original problem: whether I use #!/bin/sh or #!/bin/bash doesn’t matter, it always accepts -a. I assume the original poster is using Solaris, where the default may be csh. And as a result of the typo the default csh on Solaris is used for the script and throws the error as in csh the option -a isn’t allowed to exec.
 
Old 12-16-2011, 08:08 AM   #11
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by Reuti View Post
I assume the original poster is using Solaris, where the default may be csh. And as a result of the typo the default csh on Solaris is used for the script and throws the error as in csh the option -a isn’t allowed to exec.
I disagree; probably that sh is a symbolic link to the more common dash or busybox; csh has mostly been tossed in the trash by now. To that point: are you kidding me, Solaris defaults to csh?
 
Old 12-16-2011, 09:15 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
/bin/sh on linux is generally a link to /bin/bash

and it generally isn't compatible with POSIX /bin/sh
even though it claims to be.

It generally doesn't fail where it would on unix /bin/sh
 
1 members found this post helpful.
Old 12-16-2011, 10:01 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
#include <stdio.h>
#include <unistd.h>

void main (void)
{
    return execlp ("mono", "java", "rename.exe", NULL);
}
 
1 members found this post helpful.
Old 12-19-2011, 09:11 AM   #14
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
How are the following different?
Quote:
#!/bin/bash
exec -a java mono rename.exe

Quote:
#!/bin/bash
java mono rename.exe &
 
1 members found this post helpful.
Old 12-19-2011, 09:26 AM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by grob115 View Post
How are the following different?
The first one replaces the shell process with the java process, and the second one causes the shell to wait until the java process finishes, and then continue.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use "exec" command in shell script? btn Linux - Newbie 1 09-08-2011 12:02 PM
Why using exec in shell programming williamhomanchun Linux - General 6 05-14-2008 01:56 PM
Using exec in a shell script opus-outlaw Linux - Desktop 1 01-19-2008 11:47 AM
exec the expect command in a shell script wanghao Linux - Networking 6 11-23-2007 11:06 PM
Assistance with 'find -exec cp' shell script dick.swift Linux - Software 6 01-23-2006 10:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration