Issue
In a specific Unix directory on a server. I would like to list down files and delete them in a sequence one by one.
Below program is showing errors in for
loop. Can anybody help to make it work?
#!/bin/sh
address=/apps/applications/jboss-as/servers/scripts/
files=$(ls $address)
echo Below files will be deleted-
echo $files
k=0
for i in "$files" do {
rm $i
k++
}
done
echo ${k} files are deleted.
Solution
I fixed the script, but as others pointed out, you may want to use something other than the ls
, but I left it in. There were other syntax errors as well. Here it is:
#!/bin/bash
address=/apps/applications/jboss-as/servers/scripts/
files=("$address"/*)
echo Below files will be deleted-
echo "${files[@]}"
k=0
for i in "${files[@]}" ; do
echo "removing $i ..."
rm "$i"
((k++))
done
echo ${k} files are deleted.
UPDATE:
Since you're still having trouble, you might add declare -a files
just above the files=( "$address"/* )
. It might fix the problem for you (but, the script runs fine without it here), and it doesn't hurt anything when I run the script here with it. So, either way ...
Answered By - Craig Estey Answer Checked By - Dawn Plyler (WPSolving Volunteer)