I want to add content to a beginning of each file in current directory. I need to learn how to take all file names from current working directory at once and put them as arguments; I don't need to do this by specifying every file manually as an argument to the script. I want to use this new knowledge for further scripts. I don't need sideways or different solutions. I need to learn how to take command ( ls
) output and put it as an argument. I tried and failed to do that with:
./file-edit.sh $(ls)
./file-edit.sh `ls`
This is my script which works:
#!/bin/bash
lineno=2 # Append from line 2
BAD_ARGS=85 # Error code
string0="###################################################################"
string2="#Description :"
string3="#Date :`date +%d-%B-%Y`"
string4="#Author :Milos Stojanovic"
string5="#Contact: :https://www.linkedin.com/in/xxx/"
string6="###################################################################"
# Can be any other strings
if [ ! -f $1 ]; then
echo "Please specify at least 1 filename as an argument!"
exit $BAD_ARGS
fi
until [ -z "$1" ]
do
string1="#Script Name :$1"
file=$1;
sed -i ""$lineno"i $string0\n$string1\n$string2\n$string3\n$string4\n$string5\n$string6" $file
echo "File "\'$file\'" altered at line number: $lineno"
shift 1
done
exit 0