我在为作业分配错误后遇到一个问题,因为它似乎可以在我自己的测试中运行。我将在下面发布脚本和错误。
Bash脚本
#!/bin/bash
for file in $1
do $file | grep \.old$ > /dev/null
if [ "$file" != 0 ]
then mv $file $file.old
else $file
fi
done
Files created in 'testdir:
file1 file2.old file3old file4.old
Execution of 'oldfiles testdir':
'oldfiles testdir' incorrectly produced some output or errors:
for file in $1
do $file | grep \.old$ > /dev/null
if [ "$file" != 0 ]
then mv $file $file.old
else $file
fi
done
+ for file in '$1'
+ grep '.old$'
+ /var/tmp/uli101.npkrause.assign4.8938.testdir
/home/npkrause/scripts/oldfiles: line 4: /var/tmp/uli101.npkrause.assign4.8938.testdir: Is a directory
+ '[' /var/tmp/uli101.npkrause.assign4.8938.testdir '!=' 0 ']'
+ mv /var/tmp/uli101.npkrause.assign4.8938.testdir /var/tmp/uli101.npkrause.assign4.8938.testdir.old
Files in 'testdir' after 'oldfiles' was run:
ls: cannot access /var/tmp/uli101.npkrause.assign4.8938.testdir: No such file or directory
Filenames don't seem to be correct
Please hit <Enter> to continue with the Assignment
最佳答案
dir=$1
for file in "$dir"/* ; do
[[ $file == *.old ]] || mv -- "$file" "$file.old"
done
引用$ dir和$ file的所有用法(双方括号中的除外),以防止名称中出现空格或元字符。
使用“ mv-”可防止以“-”开头的目录名。
关于linux - 将文件移动到同一目录中具有旧扩展名的文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26641126/