I am trying to make an if-statement that says:
If a file in this directory has .PDF extension do this, otherwise do this...
And I'm having a hard time figuring out how to do this in bash. I checked here : https://stackoverflow.com/questions/3856747/check-whether-a-certain-file-type-extension-exists-in-directory
first but the solutions listed either don't work or give errors I don't know how to fix.
Attached below is my script. I slightly modified the script I was working on in my previous question here: Change only the extension of a file
The script is below:
#!/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
~
**Edit 1: **
As per Ipor Sircer's answer below I changed my script to the following:
#!/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
I get the following errors:
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...
Edit 2
As per Eric Renouf's comment fixing the spacing gives me the following 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
However if I run it twice I see the following:
Hello this is task 1 of the homework
mv: cannot stat 'Task1/*.PDF': No such file or directory
Task1/*.PDF has been updated
Why don't I just see the else echo since there are no files of type .PDF in the folder anymore?