Estou tentando fazer uma declaração if que diz:
If a file in this directory has .PDF extension do this, otherwise do this...
E eu estou tendo dificuldade em descobrir como fazer isso no bash.
Eu verifiquei aqui: link
primeiro, mas as soluções listadas não funcionam ou dão erros que não sei como corrigir.
Anexado abaixo está o meu script. Eu modifiquei ligeiramente o script em que estava trabalhando na minha pergunta anterior aqui: Change apenas a extensão de um arquivo
O script está abaixo:
#!/bin/bash
#shebang for bourne shell execution
echo "Hello this is task 1 of the homework"
#Initial prompt used for testing to see if script ran
#shopt allows configuration of the shell -s dotglob allows the script to run on dot-files
shopt -s dotglob
#loop to iterate through each file in Task1 directory and rename them
#loop runs if there is a file type of ".PDF" in the folder Task1, if there isn't displays that there i
sn't a file of that type and terminates
if [ [ -n $(echo *.PDF) ] ] # what should I put here???
then
for file in Task1/*.PDF;
do
mv "$file" "${file%.PDF}.pdf"
echo "$file has been updated"
done
else
echo "No files of that type..."
fi
~
** Editar 1: **
De acordo com a resposta do Ipor Sircer abaixo, mudei meu script para o seguinte:
#!/bin/bash
#shebang for bourne shell execution
echo "Hello this is task 1 of the homework"
#Initial prompt used for testing to see if script ran
#shopt allows configuration of the shell -s dotglob allows the script to run on dot-files
shopt -s dotglob
#loop to iterate through each file in Task1 directory and rename them
#loop runs if there is a file type of ".PDF" in the folder Task1, if there isn't displays that there isn't a file of that type and terminates
if [ ls -1 *.PDF|xargs -l1 bash -c 'mv $0 ${0%.PDF}.pdf' ]
then
for file in Task1/*.PDF;
do
mv "$file" "${file%.PDF}.pdf"
echo "$file has been updated"
done
else
echo "No files of that type..."
fi
Eu recebo os seguintes erros:
Hello this is task 1 of the homework
./Shell1.sh: line 12: [: missing ']'
mv: cannot stat ']': No such file or directory
No files of that type...
Editar 2
De acordo com o comentário de Eric Renouf, consertar o espaçamento me dá o seguinte script:
#!/bin/bash
#shebang for bourne shell execution
echo "Hello this is task 1 of the homework"
#Initial prompt used for testing to see if script ran
#shopt allows configuration of the shell -s dotglob allows the script to run on dot-files
shopt -s dotglob
#loop to iterate through each file in Task1 directory and rename them
#loop runs if there is a file type of ".PDF" in the folder Task1, if there isn't displays that there isn't a file of that type and terminates
if [[ -n $(echo *.PDF) ]]
then
for file in Task1/*.PDF;
do
mv "$file" "${file%.PDF}.pdf"
echo "$file has been updated"
done
else
echo "No files of that type..."
fi
No entanto, se eu executar duas vezes, vejo o seguinte:
Hello this is task 1 of the homework
mv: cannot stat 'Task1/*.PDF': No such file or directory
Task1/*.PDF has been updated
Por que eu não vejo mais o echo
, pois não há mais arquivos do tipo .PDF na pasta?