LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash scripting problem (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-problem-4175478601/)

shiv garg 09-26-2013 03:20 AM

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..

colucix 09-26-2013 03:32 AM

Quote:

Originally Posted by shiv garg (Post 5035053)
#!/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.

druuna 09-26-2013 03:35 AM

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......

SAbhi 09-26-2013 04:33 AM

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.


All times are GMT -5. The time now is 07:17 AM.