0

I have a script called idk.sh at the root of a folder called autograder. I also have a subdirectory in autograder called hw1 which contains some .sh files. I tried to print out the file name and contents but I failed. actually I tried /hw1, /hw1/, /hw1/* and failed. I dont really understand why I failed to fetch files and hope someone could answer me as I looked up the web and found that the approach should be /hw1/*. Thank you.

#!/bin/sh
for file in /hw1/*
do
        echo $file
        if [ -f $file ]
        then
                cat $file
                echo $file
        fi
done
~
~
Ken Kwok
  • 388
  • 3
  • 19

2 Answers2

0

A directory path starting with / means an absolute path, that is, a path from the root of the filesystem. Relative paths start with any character other than / (and \0, but that's a technicality). You'll also want to use a reference to the directory of the script, to be able to run the script from other directories.

See also:

Community
  • 1
  • 1
l0b0
  • 55,365
  • 30
  • 138
  • 223
0

I would simply do a find to achieve this

find /hw/ -type f -print -exec cat {} \;
Srini V
  • 11,045
  • 14
  • 66
  • 89