Linux 多个文件的脚本,然后创建一个mvfile脚本,将文件移动到目录。我认为我犯了一个错误
我试着看看这行的语法错误是否有效我希望你想要这样的东西-Linux 多个文件的脚本,然后创建一个mvfile脚本,将文件移动到目录。我认为我犯了一个错误,linux,bash,shell,Linux,Bash,Shell,我试着看看这行的语法错误是否有效我希望你想要这样的东西- 创建_文件脚本: ext=.gif dir=$photo // not sure to see if directory exist yet if [ -d "$photo"];then echo "file exists" if [ ! od "$photo"]; then mkdir photo #// make directory in case it does no
创建_文件
脚本:
ext=.gif
dir=$photo // not sure to see if directory exist yet
if [ -d "$photo"];then
echo "file exists"
if [ ! od "$photo"]; then
mkdir photo #// make directory in case it does not exist
fi
for file in "*.gif"; do
my $file $dir
done
#!/bin/bash
n=$1
for ((i=1;i<=n;++i))
do
touch "photo${i}.gif"
done
你没有一路走掉,只是有一点走掉了。给您带来问题的是,您正在分配
dir=$photo
,然后测试$photo
,而不是$dir
(虽然效果应该是一样的,因为您将在mv$file$dir
命令中使用dir
,所以您最好测试$dir
)。由于您使用的是mv
,其中第二个参数是目录,因此您可以在sngle命令中将所有gif
文件移动到新位置:
#!/bin/bash
FILES=`ls -1 | grep ".gif"`
DIR="photos"
if [ -e $DIR ]
then
echo "$DIR exists"
else
mkdir $DIR
mv $FILES $DIR
fi
这就是你所需要的。现在要在脚本中设置它:
mv *.gif $dir
或简短版本:
dir="$photo" # Always quote string variables!
if [ -d "$dir" ]; then # test if "$dir" (quoted) exists
echo "dir: '$dir' exists" # provide output showing success (not reqd)
mv *.gif "$dir" # move all .gif files in $PWD to "$dir"
else
echo "dir: '$dir' not found" # provide feedback on failure
fi
$photo
的值是多少?值?我试图将$photo设置为目录,我想我搞砸了…好吧,你认为dir=$photo
应该做什么?看看目录是否存在?但你没有在任何地方为photo
变量分配任何内容。哦,天哪,一个doe,一个亲爱的,要跟踪的许多dir
。非常感谢。固定的。
dir="$photo" # Always quote string variables!
if [ -d "$dir" ]; then # test if "$dir" (quoted) exists
echo "dir: '$dir' exists" # provide output showing success (not reqd)
mv *.gif "$dir" # move all .gif files in $PWD to "$dir"
else
echo "dir: '$dir' not found" # provide feedback on failure
fi
dir="$photo"; [ -d "$dir" ] && mv *.gif "$dir" || echo "dir: '$dir' not found"