I am trying to write a simple shell script .
The script goes to a folder, loops through every file, and reads every line in each file and prints them.
Am I doing anything wrong?
cd "My Directory Goes Here"
for myFile in `ls`
for line in `cat $myFile`;
do
echo "$line"
done
done
You’re missing a
dofor the outer loop and you’re better off using$()instead of backticks (easier to read, easier to nest, and it should be understood by any modern/bin/sh). Also, you don’t need to calllsto get a list of files in the current directory, you can just use*:The above will miss files like
.dotfilebut you can usefindif you need those too:And if you have to deal with files that contain spaces in their names then you’re better off using something other than the shell such as Perl, Ruby, or Python.