LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2013, 04:20 AM   #1
shiv garg
LQ Newbie
 
Registered: Sep 2013
Location: Amritsar
Distribution: fedora 19
Posts: 11

Rep: Reputation: Disabled
bash scripting problem


hey there
i was trying to execute a c program using a bash shell script
script was as follows

#!/bin/bash
`gcc shiv.c -o shiv`
`chmod 777 shiv`
`./shiv`


this was displaying an error that enter command not found.(ENTER is written in the first printf command in shiv.c file.


then i tried
#!/bin/bash
echo `gcc shiv.c -o shiv`
echo `chmod 777 shiv`
echo `./shiv`
then the problem was that i had to enter all the inputs first then the output was coming.
e.g in my c program

main()
{
int a;
printf("enter the no of persons");
scanf("%d",&a);
}
when i executed the script written three commands as mentioned above the scanf statement was running before printf;


kindly help me..
 
Old 09-26-2013, 04:32 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by shiv garg View Post
#!/bin/bash
`gcc shiv.c -o shiv`
`chmod 777 shiv`
`./shiv`


this was displaying an error that enter command not found.(ENTER is written in the first printf command in shiv.c file.
The main problem here is the weird usage of backticks, that mean command substitution: the ./shiv command is executed and the output is executed again, hence the error you get. Why did you use backticks?
Code:
#!/bin/bash
gcc shiv.c -o shiv
chmod 777 shiv
./shiv
Moreover the chmod statement is not needed, since gcc produces an executable with the proper permissions.
 
Old 09-26-2013, 04:35 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You don't need the back-ticks, try this:
Code:
#!/bin/bash
gcc shiv.c -o shiv
chmod 777 shiv
./shiv
EDIT: "Beaten" by colucix......

Last edited by druuna; 09-26-2013 at 04:44 AM. Reason: Fix error
 
Old 09-26-2013, 05:33 AM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
Quote:
echo `chmod 777 shiv`
just to make you understand backticks are used for a purpose generally to store an output from a command not anywhere.

echo is itself a "command" and then you are using backticks under it for chmod too, that is never required.

give some time to read google pages and get the correct use of commands before actually using them in scripts.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Bash scripting problem! daltocli Programming 2 04-12-2007 08:21 AM
bash scripting problem sutley Linux - Software 1 12-17-2004 12:33 PM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 01:31 PM
bash scripting problem raven Programming 7 03-10-2003 06:48 PM


All times are GMT -5. The time now is 10:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration