I have a script that makes inline replacements to other scripts in a given directory: it loops through all files with extension "sh". What is a standard way of preventing that the script does not change itself ($0)?
What utility is used to process your script (such as awk, sed, bash, or ksh)?
Show us the loop in your script that processes your *.sh files.
The file edited for brevity looks as follows:
for file in *.sh
do
# identify file extension
extension=${file##*.}
# one space after the '#'
sed -i '' -e 's/#/# /' $file
sed -i '' -e 's/# /# /' $file
done
should you be using bash (which Don Cragu asked for), extended pattern matching might work:
shopt -s extglob
for i in !(${0#*/}); do echo $i; done
EDIT: or some grep -v $0
in the for loop.
You could just exclude the running script file:
for n in *.sh; do
if [[ "$n" != `basename $0` ]]; then
echo $n
fi
done
I like this solution because it requires a mere addition to the for loop without additional indentation:
As an alternative to the cool script solutions, could you simply name the script file different - not ending with .sh - or move it to a different directory?
Yes, I thought about that too. However, I felt the name change was too big a compromise, given that all other scripts have the sh or bash extension.
Moving to a different directory is not an option either, because one can never be sure that a copy of the script is going to end up in a directory which has sh or bash files in it.