0

I am having trouble understanding how my code is working. I am new to this project and never learned unix before. My coworker quickly showed me how to run the program but didn't explain how it worked

After making sure all the C++ files compiled properly by running make, he went to the command prompt and typed ./run.sh list.txt

I took a look at the run.sh file. It is below:

DIR=/directory/scripts/binaries/what

LIST=$1

while read PDF
do 
    echo "HOLD ON ${PDF}..." 
    ./makeInputAX.sh ${PDF} ${PDF} >INPUT/${PDF}.something 
    ${DIR} -AX INPUT/${PDF}.something # I don't want to reveal the actual types of files I'm dealing with, so I called them 'something'
done < ${LIST}

the makeInputAX.sh file is:

$PDF=1
echo "THEFILE ABC/{PDF}.something"

and list.txt simply contains:

12345

So I already know that run.sh reads whats inside list.txt, and assigns 12345 to be the LIST variable. I do see HOLD ON 12345 in the command prompt. What confuses me is that I thought run.sh runs first, then calls makeInputAX.sh, so how can PDF be assigned to list.txt, when that assignment was in makeInputAX.sh not run.sh?

What also confuses me is that in the command prompt, I then see the outputs from the program, as they were written in the cpp files that are part of the project. However, those files are in a seperate directory, not in /directory/scripts/binaries/what. I made changes to those cpp files and want to see the new outputs I should get, but I am getting an error:

 could not open the somethingelse file for read

However, could not open the .. is not written anywhere in my C++ code. To fix that, do I need to look in my cpp files or is there something to these sh files that can help?

2

$1 is the first argument to the script, i.e. list.txt

LIST=$1

simply copies that filename to the LIST variable, so $LIST now contains lists.txt as well -- it doesn't contain 12345.

while read PDF
do
    ...
done < ${LIST}

starts a loop whose standard input is redirected to the file list.txt, and each time through the loop it reads a line from the file into the variable PDF. So $PDF will then contain `12345.

The variables in makeInputAX.sh are unrelated to the variables in run.sh. So they each have their own variables named PDF, and they operate independently. The only exception would be environment variables, which are declared using the export command; environment variables set in a calling script are copied to the environment of the scripts it calls (but not the other way around).

I don't see anything in your scripts that could be producing the "could not open the somethingelse file for read", it must be coming from the C++ program. It's possible it's in a library that your program calls, so you wouldn't see the message anywhere in your code.

Your makeInputAX.sh script doesn't look correct, though. It should probably be something like this:

PDF=1
echo "THEFILE ABC/${PDF}.something"

You put $ before a variable when inserting its value in another command, you don't use it in the assignment. You had it backwards, with the $ in the assignment, but not in the echo command.

  • thanks for the reply. I'm still confused as to how the C++ files and the shell scripts are even related. DIR=/directory/scripts/binaries/what probably has something to do with it, but how? – user4352158 Dec 18 '14 at 22:23
  • The shell script just does the same thing as if you'd typed these commands by hand. ${DIR} is replaced by that value, so it runs the what program with the arguments in the shell script. I assume that's the C++ program that you compiled. – Barmar Dec 18 '14 at 22:32
  • I also don't understand why some (not all) of the outputs in the C++ files, that is cout don't display on the command prompt. – user4352158 Dec 18 '14 at 22:51
  • I don't see any reason why the output of the programs wouldn't appear on the screen. You're not redirecting the output to a file. – Barmar Dec 18 '14 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.