I work as a grader and files submitted to me end up in a folder named with the student's name which is itself inside a specific folder on my linux share.
Currently I go through each source file and manually add a comment at the top of each file so that when I print them out I can know which files belong to which students.
What I want to do is recursively go through the homework folder and as I drop down into each student folder add a comment to the top of each file in that folder using the student directory name to know what to put into the comment.
This seems similar as far as adding comments at the top of the file but I'm not sure how to dynamically create the comment as I drop into each student folder.
Most recent update
Still doesn't work quite yet but here are the changes I made so you don't have to hunt for them.
I made some changes:
- Single quotes in the printf statement are now double quotes since the
'
inisn't
was terminating it early. - Changed
-writable
to-perm 664
writable threw an error and all the files I'm trying to change are set at 644 when they get uploaded. If you know of a better way than that let me know. - If I understand exec correctly now $1 is undeclared (adding
echo $1
in the exec argument confirmed this) so I added_filepath={}
and removed the{}
at the end of the exec argument. - Initialize
_dirname
with_filepath
- In the two lines using regex to isolate the directory name the double quotes weren't closed. Now the _dirname successfully holds just the name of the directory afterwards
- Now invokes ed with
_filepath
I'm pretty sure the reason it doesn't work is because of single quotes in the line invoking ed are closing the exec argument.
current code:
student_head_action() {
# We can't use parameter expansion on $PWD because of recursion
local _dirname="${1%/*}"
_dirname="${_dirname##*/}"
[[ -d $1 ]] && return 0
if ! [[ -w $1 && -f $1 ]]; then
printf '%s\n' "$1 does not exist or is not writeable, skipping"
return 1
fi
ed -s "$1" <<< $'0a\n'"//${_dirname}"$'\n.\n,s/\r//g\nw'
}
student_head() {
local _file
if (( $# )); then
for _file; do
student_head_action "${_file}" || _retval=1
done
else
if shopt -qs globstar; then
for _file in **/*; do
student_head_action "${_file}" || _retval=1
done
shopt -u globstar
else
printf "%s\n" "Globstar isn't available, attempting to use GNU find ..."
find . -type f -perm 644 -exec bash -c '
_filepath={}
_dirname=$_filepath
_dirname="${_dirname%/*}"
_dirname="${_dirname##*/}"
echo $_dirname
ed -s "$_filepath" <<< '"$'0a\n'"'"//${_dirname}"'"$'\n.\n,s/\r//g\nw'" \;
fi
fi
return "${_retvalue-0}"
~/.bashrc
is not sourced by non-interactive shells, sofind . -type f -exec bash -c 'student_head_action {}' \;
will not work. – Chris Down Sep 16 '11 at 21:32ed -s foobar <<< $'0a\n'"// ${_dirname}"$'\n.\nw'
instead, it's erroring because there is nothing to replace. – Chris Down Sep 16 '11 at 22:55