我正在尝试做一个 if 语句:
如果此目录中的文件具有 .PDF 扩展名,则执行此操作,否则执行此操作...
我很难弄清楚如何在 bash 中做到这一点。我在这里检查过:https://stackoverflow.com/questions/3856747/check-whether-a-certain-file-type-extension-exists-in-directory
首先,但列出的解决方案要么不起作用,要么给出我不知道如何修复的错误。
下面附上我的脚本。我稍微修改了我在上一个问题中正在处理的脚本:仅更改文件的扩展名
脚本如下:
#!/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
~
**编辑1:**
根据下面 Ipor Sircer 的回答,我将脚本更改为以下内容:
#!/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
我收到以下错误:
你好,这是家庭作业的任务 1
./Shell1.sh: 第 12 行: [: 缺少 `]'
mv:无法统计']':没有这样的文件或目录
没有该类型的文件...
编辑2
根据 Eric Renouf 的评论,修复间距给了我以下脚本:
#!/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
但是,如果我运行两次,我会看到以下内容:
你好,这是家庭作业的任务 1
mv:无法统计“Task1/*.PDF”:没有这样的文件或目录
Task1/*.PDF 已更新
为什么我看不到其他内容,echo
因为文件夹中不再有 .PDF 类型的文件?
答案1
shopt -s nullglob
set -- Task1/*.PDF
if (( $# > 0 )); then
printf 'There are %d PDF files in "Task1"\n' "$#"
else
echo 'There are no PDF files in "Task1"'
fi
会将位置参数设置为子目录set
中可用的各个 PDF 文件的名称。Task1
这与您使用 调用脚本相同./script Task1/*.PDF
。
保存位置参数的数量$#
,我们测试它是否大于零。如果是,我们会报告可用的 PDF 文件的数量。否则我们报告没有。您可以选择做任何您想做的事情来代替这两个操作。
如果任务只是将.PDF
任何此类文件的后缀小写,则无需首先检查是否存在:
shopt -s nullglob
for fpath in Task1/*.PDF; do
mv "$fpath" "${fpath%.PDF}.pdf"
done
设置shell 选项可确保如果没有任何文件与该模式匹配,nullglob
我们不会得到*
in值。fname
答案2
ls -1 *.PDF|xargs -l1 bash -c 'mv $0 ${0%.PDF}.pdf'
答案3
你可以使用:
[ -e *.PDF ]
代替
[ [ -n $(echo *.PDF) ] ]
例如
[ -e *.PDF ] && echo 'yes'