Você pode verificar se o argumento é um arquivo ou um diretório e, em seguida, criar uma lista de arquivos para tratar e fazer um loop sobre eles.
#!/bin/bash
# The script takes one arguments that must be a file or a directory
# Treating a file or all files in a directory
export FILES
if [[ -f "$1" ]]
then
echo "First argument is a file"
FILES=$1
elif [[ -d "$1" ]]
then
echo "First argument is a directory"
FILES=$(find "$1" -type f)
fi
for file in $FILES
do
echo "Treating file $file"
# do stuff here
done
Saídas:
[user@host test]# ls ~/test
tmp.1bmMi7hOsF tmp.7VFvw4nHNz tmp.ieLecqRusH tmp.o6Rfc2QQdb tmp.SQqRwtdro2 tmp.YahRzYY5mV tmp.ZPGgJPTMZJ
tmp.2KCyJr7lLr tmp.GNvgwYbkRO tmp.MsESiT8n1Q tmp.Q81MtVS97H tmp.WSlNZ4G6Uc tmp.yLwBItk2qX
[user@host test]# ~/test.sh ~/test/tmp.YahRzYY5mV
First argument is a file
Treating file /home/user/test/tmp.YahRzYY5mV
[user@host test]# ~/test.sh ~/test/
First argument is a directory
Treating file /home/user/test/tmp.7VFvw4nHNz
Treating file /home/user/test/tmp.MsESiT8n1Q
Treating file /home/user/test/tmp.WSlNZ4G6Uc
Treating file /home/user/test/tmp.o6Rfc2QQdb
Treating file /home/user/test/tmp.GNvgwYbkRO
Treating file /home/user/test/tmp.YahRzYY5mV
Treating file /home/user/test/tmp.1bmMi7hOsF
Treating file /home/user/test/tmp.ZPGgJPTMZJ
Treating file /home/user/test/tmp.Q81MtVS97H
Treating file /home/user/test/tmp.2KCyJr7lLr
Treating file /home/user/test/tmp.yLwBItk2qX
Treating file /home/user/test/tmp.ieLecqRusH
Treating file /home/user/test/tmp.SQqRwtdro2
[user@host test]#