Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jmarauder

macrumors newbie
Original poster
Mar 26, 2009
10
0
Hi all, I have a program for my data structures class due this week, and my instructor has said that the program needs to be able to run from the command line with a string of parameters that needs to be parsed.

Normally all my programs that I have done up to this point have all prompted the user to input the file name for the program's input and generally for every other user defined variables. I have been doing hours of research trying to figure this out for myself, but can not seem to get it done for the life of me.

I know I have written a good function to parse the string by testing it with string I have defined, but I can not seem to get the program to get the string parameter from the command line. I always get this error when i run this command

sr input=example2.txt;search=532;replace=2320

-bash: sr: command not found

any help would be awesome guys!
 
What is the source code of the file called 'sr'? And what is the compiled binary's permissions? (i.e., ls -l sr)

Is 'sr' a shell script or is it a compiled binary?
 
sr is the actual c++ file (sr.cpp)

Okay, you may have at least two issues:

1) The compiled sr binary may not have executable permissions and/or you may not be doing ./sr <command line arguments>

2) In the source code, are you parsing argc/argv? E.g., see:

http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

If you'd like to get better-targeted suggestions, you may want to post the source code snippets or at least the part that handles CLI argument parsing.
 
Okay, you may have at least two issues:

1) The compiled sr binary may not have executable permissions and/or you may not be doing ./sr <command line arguments>

2) In the source code, are you parsing argc/argv? E.g., see:

http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

If you'd like to get better-targeted suggestions, you may want to post the source code snippets or at least the part that handles CLI argument parsing.

here is my terminal window of when I am trying to run the program...

Last login: Sun Sep 27 20:15:26 on ttys000
joseph-wanjas-macbook-pro:~ joe$ cd Desktop/
joseph-wanjas-macbook-pro: Desktop joe$ cd Project\ 1\ Answer/
joseph-wanjas-macbook-pro:project 1 Answer joe$ ./sr sr input=example2.txt;search=532;replace=2320
-bash: ./sr: No such file or directory
joseph-wanjas-macbook-pro:project 1 Answer joe$

does that provide any insight into the problem, or should I post the code snippets?
 
here is my terminal window of when I am trying to run the program... does that provide any insight into the problem, or should I post the code snippets?

That's helpful information -- thanks. Means that when you told Terminal to run a program called 'sr' in the current directory, it couldn't find that. So it spit out the error.

To find out where the 'sr' binary might be, you could try this Terminal command:

Code:
find ~ -name sr

Then cd to whatever directory it mentions then redo the ./sr <cli args> command
 
Did you compile sr.cpp? Because if you didn't, nothing else will work.

In the directory where sr.cpp resides, enter the following command and post the output:

Code:
ls -la
 
Did you compile sr.cpp? Because if you didn't, nothing else will work.

In the directory where sr.cpp resides, enter the following command and post the output:

Code:
ls -la

joseph-wanjas-macbook-pro:project 1 Answer joe$ ls -la
total 48
drwxr-xr-x 9 joe staff 306 Sep 27 19:29 .
drwx------+ 15 joe staff 510 Sep 27 19:29 ..
-rw-r--r--@ 1 joe staff 6148 Sep 27 19:29 .DS_Store
-rw-r--r--@ 1 joe staff 46 Sep 27 19:26 HW1Ans1.txt
-rw-r--r--@ 1 joe staff 46 Sep 27 19:26 HW1T1.txt
-rw-r--r-- 1 joe staff 3061 Sep 27 19:24 Project 1 Answer.1
drwxr-xr-x 5 joe staff 170 Sep 27 19:55 Project 1 Answer.xcodeproj
drwxr-xr-x@ 4 joe staff 136 Sep 27 19:25 build
-rw-r--r--@ 1 joe staff 3059 Sep 27 19:25 sr.cpp
joseph-wanjas-macbook-pro:project 1 Answer joe$

and I have set the executable's working directory to the "Project 1 Answer" directory
 
You need to cd into the directory where the actual executable resides (somewhere in the "build" directory), or enter the path to it relative from where your current working directory is.

Also, separating the command line options with semicolons won't work as they have a special meaning to the shell.
 
You need to cd into the directory where the actual executable resides (somewhere in the "build" directory), or enter the path to it relative from where your current working directory is.

Also, separating the command line options with semicolons won't work as they have a special meaning to the shell.

when I navigate to the actual executable I still get
-bash: sr: command not found
error. I even tried it with just inputing the parameters seperated by commas instead of semicolons. Help please still!
 
Can you do a "ls -al" of the build folder; then try running the app; and paste the results here?

after "ls -al"
joseph-wanjas-macbook-pro:build joe$ ls
Debug sr.build
joseph-wanjas-macbook-pro:build joe$ ls -al
total 0
drwxr-xr-x@ 4 joe staff 136 Sep 28 12:17 .
drwxr-xr-x 6 joe staff 204 Sep 28 12:17 ..
drwxr-xr-x 3 joe staff 102 Sep 28 12:17 Debug
drwxr-xr-x@ 4 joe staff 136 Sep 28 12:17 sr.build

try running program with just input file as parameter
joseph-wanjas-macbook-pro:build joe$ sr input=HW1T1.txt
-bash: sr: command not found
 
The main problem here is that you don't understand how bash interprets commands.

First, you must be in the same directory as the "sr" command you just built, or you must give a pathname to the command. If you're not in that directory, then typing plain 'sr' as the command will never work. Bash doesn't search your entire disk looking for commands. It only searches specific dirs, the ones listed in its PATH variable.

Second, the way bash works, it does not automatically search the current directory. That means you must type the command as "./sr" for it to find the command. (If you're wondering why it's this way, it's for security reasons.)

Third, after the "./sr" you must enter the command-line args in quoted form, otherwise bash will interpret the semicolons as delimiters between separate commands.

To summarize:

Code:
cd where/your/command/is/located
./sr "input=example2.txt;search=532;replace=2320"
Since you haven't explained what these parameters mean, you may have to change the pathname of example2.txt, possibly to a relative pathname like ../../example2.txt. That's just a guess because you haven't said what your program does with its command-line args. Or you could explain what the parameters mean.

Finally, you need to use the 'ls' command, especially with the -l option, and understand what it's telling you. So far, you've posted at least two outputs from 'ls -l', and none of them contained an 'sr' command, yet this absence has gone unnoted.
 
Since you haven't explained what these parameters mean, you may have to change the pathname of example2.txt, possibly to a relative pathname like ../../example2.txt. That's just a guess because you haven't said what your program does with its command-line args. Or you could explain what the parameters mean.

Finally, you need to use the 'ls' command, especially with the -l option, and understand what it's telling you. So far, you've posted at least two outputs from 'ls -l', and none of them contained an 'sr' command, yet this absence has gone unnoted.

the text file will be used as my input file for the project
 
The main problem here is that you don't understand how bash interprets commands.

From what I'm reading, I'm not even sure the OP is clear on the process of compiling to an executable binary from the C++ source, and then executing the binary file. I assume he is, but it's not clear.

The first post suggests that the OP has been able to run programs in the past (programs that prompt the user for input), but the inability to run the executable from the shell (regardless of command-line parameters) seems to contradict this. Unless the previous runs were from within an IDE such as Xcode, and this is the first time running directly from a shell?

I strongly recommend to the OP, stepping back a bit and playing with some simple code to specifically exercise your knowledge of handling command line parameters. Write a little "hello world" program that takes the parameters into argc and argv and print them out.

Once you get that working, then plug it into the rest of your program.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.