0

When I run the shell script on the terminal, It wouldn't work for some reason. let's say my file name is final_exam.sh, and below are my command.

chmod 755 final_exam.sh
sh final_exam.sh

and it says

ls: cannot access 'PATH_TO_FILES/A.csv': No such file or directory
ls: cannot access 'PATH_TO_FILES/B.csv': No such file or directory

what should i do? Any suggestion/advice will be appreciated.


I have been trying to run the shell script, but it doesn't work saying "No such file or directory". I am attaching the screenshots of my results, and any of your advice/help will be very appreciated. Thanks a ton.

stats@stats-VirtualBox:~/Desktop$ ls 
FINAL_Project  __MACOSX
stats@stats-VirtualBox:~/Desktop$ cd FINAL_Project
stats@stats-VirtualBox:~/Desktop/FINAL_Project$ cd final_exam
stats@stats-VirtualBox:~/Desktop/FINAL_Project/final_exam$ pwd
/home/stats/Desktop/FINAL_Project/final_exam
stats@stats-VirtualBox:~/Desktop/FINAL_Project/final_exam$ ls
Domains_FormB.csv  final_project.sh  Numeric1.awk     WidetoLong.r
Domains_FormV.csv  FirstConvert.r    Numeric2.awk
Final_Output       FormV.csv         SecondConvert.r
stats@stats-VirtualBox:~/Desktop/FINAL_Project/final_exam$ 

So, basically, ^^ has the full pile path of csv. files.

and here's my shell script.

# 9 Path to the folder with the data
#PATH_TO_FILES=../.
PATH_TO_FILES=home/Desktop/FINAL_Project/final_exam

sed -i 's/^M//g' FirstConvert.r
sed -i 's/^M//g' SecondConvert.r
sed -i 's/^M//g' WidetoLong.r

# 1 Remove any files that had been created in previous attempts
rm -f Final_Output
touch Final_Output

for file in `ls PATH_TO_FILES/Form?.csv`
do
    fname=`echo $file | sed 's/.*\///g' | sed 's/\.csv//g'`
# 2 Separate the answer key from the student answers
    grep "KEY" $file > OUT_Answers_$fname
    grep -v "KEY" $file > OUT_StudentAnswers_$fname

# 3 Use the R script provided that matches the student answers to the answer key
    Rscript FirstConvert.r OUT_StudentAnswers_$fname OUT_Answers_$fname OUT_FirstConvert_$fname
# 4 Use the R script provided which changes the R object from wide to long format
    Rscript WidetoLong.r OUT_FirstConvert_$fname OUT_WidetoLong_$fname

    sed -i 's/\"//g' OUT_WidetoLong_$fname 

 
# 5 Format the resulting question numbers from the file in step 4
    awk -f Numeric1.awk OUT_WidetoLong_$fname > OUT_Questions_$fname 
done

for file in `ls PATH_TO_FILES/Domains_Form?.csv`
do
    fname=`echo $file | sed 's/.*\///g' | sed 's/Domains_//g' | sed 's/\.csv//g'`
# 6 Select only the domain number and question number from the domains file and remove the header
    tail -n +2 $file | cut -d',' -f3,4 | sed 's/,/ /g' | sed 's/\r//g' > OUT_Domain_tmp_$fname
# 7 Modify the script from step 5 to format the question numbers in the file you created in step 6 to be sorted correctly
    awk -f Numeric2.awk OUT_Domain_tmp_$fname > OUT_Domain_$fname
# 8 Join your student's answer file to the domains on/by question number
    join -1 3 -2 2 -o 1.1,1.2,1.3,2.1,1.4 OUT_Questions_$fname OUT_Domain_$fname >> Final_Output
done

# Clean intermediate files
rm -f OUT_*

enter image description here

4
  • Referencing the answer provided by @arielCo, you should rephrase your question to reflect what's really happening. – ajgringo619 Dec 14 '20 at 0:46
  • So the question is basically as you can see in the screenshot, whenever I run the shell script, it's not running but just says ls:cannot access 'PATH_TO_FILES/Form?.csv': No such file or directory', which is weird cuz i have those two files in the same directory as where I am running the code. That's what's happening right now, and I don't know how to fix it. – lengiosi Dec 14 '20 at 1:45
  • Guys I put the $ in front of PATH_TO_FILES/Form?.csv and PATH_TO_FILES/Domains_Form?.csv, but it still doesn't work. Any further suggestion to execute the code? Any help will be appreciated – lengiosi Dec 14 '20 at 9:34
  • 2
    @lengiosi I removed your edit as it invalidates the given answers. This is not a debugging session. If you have further issues with your code, then please ask a new question. As far as I can see, you are running code that you don't show, or you are showing error messages from some other script. The script that you show does not produce a "file not found" error for any file called A.csv or B.csv. – Kusalananda Dec 14 '20 at 9:58
1

After reading all comments and not finding it, assuming your for-loops include the $

I'd say PATH_TO_FILES=home/Desktop/FINAL_Project/final_exam is missing a leading /.

Your script is probably searching in <current dir>home

You can find out by "echo"ing the variable in the beginning of your script:

...
echo $PATH_TO_FILES
...

Also, since it's in the title of the question, you don't need to chmod your script when you run it with sh or bash like this

bash my_script.sh

In fact, you can omit the shebang entirely.

Hope that helps

0

The error message is not about final_project.sh. It does open and run, and the error message is from ls which I presume is being called by the script: it's complaining about two arguments that don't correspond to any files it can find.

Please paste the contents of your script instead of a screenshot (use the {} button in the toolbar to format it as code), and the full path to those .csv files. We may be able to figure out where it's looking for those files.

2
  • I just edited my post. Could you please look it up? – lengiosi Dec 14 '20 at 1:25
  • @lengiosi I removed your corrective edit since it makes the question hard to follow. If you have further questions, ask a new question. – Kusalananda Dec 14 '20 at 9:52
0

As pointed out by arielCo, the script content when executing is throwing errors, you can modify the above script for loop to clear out the issue.

for file in `ls PATH_TO_FILES/Form?.csv converted into

for file in `ls $PATH_TO_FILES/Form?.csv`

The addition of $ before the variable will substitute the value of variable.

4
  • I did put $ in from of it, but still it shows me the same error message. – lengiosi Dec 14 '20 at 9:28
  • @lengiosi It's highly unlikely that you still get the same error message as the shell would expand the variables now. Don't change the code in the question. It makes it very difficult to follow what's happening. This is not a live debugging session. – Kusalananda Dec 14 '20 at 9:54
  • 1
    To loop over the result of a filename globbing pattern, there is absolutely no need to use ls or a command substitution. Just saying for file in "$PATH_TO_FILES"/Form?.csv; do would be enough, and would additionally work with filenames that have spaces in their names and values of $PATH_TO_FILES with spaces etc. – Kusalananda Dec 14 '20 at 9:56
  • 1
    ^^^ Ditto. – Kamil Maciorowski Dec 14 '20 at 10:59

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