2

I have no problem running my C programs by switching to their directory and starting them with ./. However, I'm not sure why changing to the directory and then typing sh [Program Name] gives me this error:

Fortune_Teller_5000: 1: Fortune_Teller_5000: Syntax error: "(" unexpected

and:

./Fortune_Teller_5000: 1: ./Fortune_Teller_5000: Syntax error: "(" unexpected

I read in the bash man pages that the sh command is a shell that doesn't read the .bashrc file, so I'm a little confused as the why I can't use it to run programs I made. I did use it to install my printer drivers according to the directions on the HP Linux Imaging and Printing page.

cat
  • 1,672
  • http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash – TheWanderer Jun 28 '16 at 13:37
  • i assumed by the messages that the debugger in sh didn't like the program, which is confusing since bash gave me zero error messages when i compiled it –  Jun 28 '16 at 13:42
  • 1
    Is Fortune_Teller_5000 a C program, C source code or a shell script? You may need to clarify your question. – Weboide Jun 28 '16 at 13:44
  • 2
    Can you not just copy the error but also the call? – Braiam Jun 28 '16 at 22:57
  • you mean the exit status? the exit status is 2, so pretty general "error" –  Jun 29 '16 at 00:51
  • No, the command you are using that produce the error. – Braiam Jun 29 '16 at 13:52
  • 1
    You seem to be a bit confused about the roles of the shell and a compiler. – Carsten S Jun 29 '16 at 14:37
  • "No, the command you are using that produce the error." I said what it was in the first paragraph. "You seem to be a bit confused about the roles of the shell and a compiler. " Duh. Try learning BASH, C programming, asembly, and general computer science all by yourself. –  Jun 29 '16 at 17:59

2 Answers2

19

Typing sh program_name assumes that program_name is a shell script and executes the script (so it must be in a sh/bash language). If Fortune_Teller_5000 is a binary file (a Compiled C program, or even C source code), running sh Fortune_Teller_5000 will not work as expected.

Here are 2 ways you could do it:

  1. Type sh then ENTER, and then type ./Fortune_Teller_5000 (you may need to cd into the proper directory)
  2. Type sh -c ./Fortune_Teller_5000 or sh -c /path/to/folder/Fortune_Teller_5000
Weboide
  • 10,493
  • all three methods worked thanks a lot. edit: oh btw, it cracks me up that with the first method it strips my color codes for the prompt string bare down to the ASCII text, so it looks hideous! –  Jun 28 '16 at 13:46
  • Glad to hear it! Please make sure to accept my answer if this helped in resolving your problem! Edit: yes, as you mentioned sh would not read .bashrc and so would handle colors differently, or not at all, compared to bash. – Weboide Jun 28 '16 at 13:51
  • 3
    Incidentally, what sense does it make to run your C program inside another shell? You aren't using any of its features in any way, so you could simply start your program directly. – Matteo Italia Jun 28 '16 at 20:13
  • This could be used for testing under different environments where the shell is different, that's one example. – Weboide Jun 29 '16 at 00:02
8

Running sh [Program Name] instructs the sh program to execute a shell script named [Program Name] so if this file is a C program, the command will fail.

The POSX specification for sh, the standard command language interpreter states that the argument for sh should be the “pathname of a file containing commands”.