#!/bin/bash
hello()
{
SRC=
#!/bin/bash
hello()
{
SRC=$1
DEST=$2
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo $SRC | grep '*' > /dev/null
if test `echo $?` -eq 0 ; then
for STAR in $SRC ; do
echo -en "$IP"
echo -en "\n\t ARG1=$STAR ARG2=$2\n\n"
done
else
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
fi
done
}
hello $1 $2
The above is the shell script which I provide source (SRC) & desitnation (DEST) path. It worked fine when I did not put in a SRC path with wild card ''. When I run this shell script and give ''.pdf or '*'as follows:
以上是我提供源(SRC)和desitnation(DEST)路径的shell脚本。当我没有使用外卡''放入SRC路径时,它工作正常。当我运行这个shell脚本并给出'.pdf或'*'时,如下所示:
root@ankit1:~/as_prac# ./test.sh /home/dev/Examples/*.pdf /ankit_test/as
I get the following output:
我得到以下输出:
192.168.1.6
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/home/dev/Examples/case_howard_county_library.pdf
The DEST is /ankit_test/as but DEST also get manupulated due to '*'. The expected answer is
DEST是/ ankit_test / as,但由于'*',DEST也会被删除。预期的答案是
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/ankit_test/as
So, if you understand what I am trying to do, please help me out to solve this BUG. I'll be grateful to you.
所以,如果您了解我要做的事情,请帮助我解决这个问题。我会感激你的。
Thanks in advance!!!
提前致谢!!!
I need to know exactly how I use '*.pdf' in my program one by one without disturbing DEST.
我需要确切地知道如何在我的程序中逐一使用'* .pdf'而不会打扰DEST。
9 个解决方案
#1
4
Your script needs more work. Even after escaping the wildcard, you won't get your expected answer. You will get:
您的脚本需要更多工作。即使在逃避通配符后,您也无法获得预期的答案。你会得到:
ARG1=/home/dev/Examples/*.pdf ARG2=/ankit__test/as
Try the following instead:
请尝试以下方法:
for IP in `cat /opt/ankit/configs/machine.configs`
do
for i in $SRC
do
echo -en "$IP"
echo -en "\n\t ARG1=$i ARG2=$DEST\n\n"
done
done
Run it like this:
像这样运行:
root@ankit1:~/as_prac# ./test.sh "/home/dev/Examples/*.pdf" /ankit__test/as
#2
3
The shell will expand wildcards unless you escape them, so for example if you have
shell将扩展通配符,除非你将它们转义,例如,如果你有
$ ls
one.pdf two.pdf three.pdf
and run your script as
并运行您的脚本
./test.sh *.pdf /ankit__test/as
it will be the same as
它会是一样的
./test.sh one.pdf two.pdf three.pdf /ankit__test/as
which is not what you expect. Doing
这不是你所期望的。干
./test.sh \*.pdf /ankit__test/as
should work.
#3
2
If you can, change the order of the parameters passed to your shell script as follows:
如果可以,请更改传递给shell脚本的参数的顺序,如下所示:
./test.sh /ankit_test/as /home/dev/Examples/*.pdf
That would make your life a lot easier since the variable part moves to the end of the line. Then, the following script will do what you want:
这将使您的生活变得更加容易,因为变量部分移动到行尾。然后,以下脚本将执行您想要的操作:
#!/bin/bash
hello()
{
SRC=$1
DEST=$2
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
done
}
arg2=$1
shift
while [[ "$1" != "" ]] ; do
hello $1 $arg2
shift
done
#4
1
You are also missing a final "done" to close your outer for loop.
你也错过了最后的“完成”以关闭你的外部for循环。
#5
1
OK, this appears to do what you want:
好的,这似乎做你想要的:
#!/bin/bash
hello() {
SRC=$1
DEST=$2
while read IP ; do
for FILE in $SRC; do
echo -e "$IP"
echo -e "\tARG1=$FILE ARG2=$DEST\n"
done
done < /tmp/machine.configs
}
hello "$1" $2
- You still need to escape any wildcard characters when you invoke the script
调用脚本时,仍需要转义任何通配符
- The double quotes are necessary when you invoke the
hello function, otherwise the mere fact of evaluating $1 causes the wildcard to be expanded, but we don't want that to happen until $SRC is assigned in the function
当您调用hello函数时,双引号是必需的,否则仅仅评估$ 1的事实会导致扩展通配符,但我们不希望在函数中分配$ SRC之前发生这种情况。
#6
1
Here's what I came up with:
这就是我想出的:
#!/bin/bash
hello()
{
# DEST will contain the last argument
eval DEST=\$$#
while [ $1 != $DEST ]; do
SRC=$1
for IP in `cat /opt/ankit/configs/machine.configs`; do
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
done
shift || break
done
}
hello $*
Instead of passing only two parameters to the hello() function, we'll pass in all the arguments that the script got.
我们将传递脚本获得的所有参数,而不是仅将两个参数传递给hello()函数。
Inside the hello() function, we first assign the final argument to the DEST var. Then we loop through all of the arguments, assigning each one to SRC, and run whatever commands we want using the SRC and DEST arguments. Note that you may want to put quotation marks around $SRC and $DEST in case they contain spaces. We stop looping when SRC is the same as DEST because that means we've hit the final argument (the destination).
在hello()函数内部,我们首先将最终参数赋值给DEST var。然后我们遍历所有参数,将每个参数分配给SRC,并使用SRC和DEST参数运行我们想要的任何命令。请注意,如果它们包含空格,您可能需要在$ SRC和$ DEST周围加上引号。当SRC与DEST相同时,我们停止循环,因为这意味着我们已经达到最终参数(目标)。
#7
1
For multiple input files using a wildcard such as *.txt, I found this to work perfectly, no escaping required. It should work just like a native bash app like "ls" or "rm." This was not documented just about anywhere so since I spent a better part of 3 days trying to figure it out I decided I should post it for future readers.
对于使用通配符(如* .txt)的多个输入文件,我发现这可以完美地工作,不需要转义。它应该像本机bash应用程序一样工作,如“ls”或“rm”。这并没有记录在任何地方,所以因为我花了3天的时间来试图解决这个问题,所以我决定将它发布给未来的读者。
Directory contains the following files (output of ls)
目录包含以下文件(ls的输出)
file1.txt file2.txt file3.txt
Run script like
像运行脚本一样
$ ./script.sh *.txt
Or even like
甚至喜欢
$ ./script.sh file{1..3}.txt
The script
#!/bin/bash
# store default IFS, we need to temporarily change this
sfi=$IFS
#set IFS to $'\n\' - new line
IFS=$'\n'
if [[ -z $@ ]]
then
echo "Error: Missing required argument"
echo
exit 1
fi
# Put the file glob into an array
file=("$@")
# Now loop through them
for (( i=0 ; i < ${#file[*]} ; i++ ));
do
if [ -w ${file[$i]} ]; then
echo ${file[$i]} " writable"
else
echo ${file[$i]} " NOT writable"
fi
done
# Reset IFS to its default value
IFS=$sfi
The output
file1.txt writable
file2.txt writable
file3.txt writable
The key was switching the IFS (Internal Field Separator) temporarily. You have to be sure to store this before switching and then switch it back when you are done with it as demonstrated above.
关键是暂时切换IFS(内部现场分离器)。您必须确保在切换之前存储它,然后在完成后将其切换回来,如上所示。
Now you have a list of expanded files (with spaces escaped) in the file[] array which you can then loop through. I like this solution the best, easiest to program for and easiest for the users.
现在,您在文件[]数组中有一个扩展文件列表(带有空格转义),然后您可以循环访问它们。我喜欢这个解决方案最好,最容易为用户编程和最简单。
#8
0
There's no need to spawn a shell to look at the $? variable, you can evaluate it directly.
没有必要生成一个shell来查看$?变量,您可以直接评估它。
It should just be:
它应该只是:
if [ $? -eq 0 ]; then
#9
0
You're running
./test.sh /home/dev/Examples/*.pdf /ankit_test/as
and your interactive shell is expanding the wildcard before the script gets it. You just need to quote the first argument when you launch it, as in
并且您的交互式shell在脚本获取之前扩展通配符。你只需要在启动它时引用第一个参数,如
./test.sh "/home/dev/Examples/*.pdf" /ankit_test/as
and then, in your script, quote "$SRC" anywhere where you literally want the things with wildcards (ie, when you do echo $SRC, instead use echo "$SRC") and leave it unquoted when you want the wildcards expanded. Basically, always put quotes around things which might contain shell metacharacters unless you want the metacharacters interpreted. :)
然后,在你的脚本中,引用“$ SRC”,你真正想要带有通配符的东西(即,当你做回声$ SRC,而是使用echo“$ SRC”)并在你想要扩展通配符时不加引号。基本上,除非你想要解释元字符,否则总是在可能包含shell元字符的东西周围加上引号。 :)
DEST=
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo $SRC | grep '*' > /dev/null
if test `echo $?` -eq 0 ; then
for STAR in $SRC ; do
echo -en "$IP"
echo -en "\n\t ARG1=$STAR ARG2=\n\n"
done
else
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
fi
done
}
hello
#!/bin/bash
hello()
{
SRC=$1
DEST=$2
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo $SRC | grep '*' > /dev/null
if test `echo $?` -eq 0 ; then
for STAR in $SRC ; do
echo -en "$IP"
echo -en "\n\t ARG1=$STAR ARG2=$2\n\n"
done
else
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
fi
done
}
hello $1 $2
The above is the shell script which I provide source (SRC) & desitnation (DEST) path. It worked fine when I did not put in a SRC path with wild card ''. When I run this shell script and give ''.pdf or '*'as follows:
以上是我提供源(SRC)和desitnation(DEST)路径的shell脚本。当我没有使用外卡''放入SRC路径时,它工作正常。当我运行这个shell脚本并给出'.pdf或'*'时,如下所示:
root@ankit1:~/as_prac# ./test.sh /home/dev/Examples/*.pdf /ankit_test/as
I get the following output:
我得到以下输出:
192.168.1.6
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/home/dev/Examples/case_howard_county_library.pdf
The DEST is /ankit_test/as but DEST also get manupulated due to '*'. The expected answer is
DEST是/ ankit_test / as,但由于'*',DEST也会被删除。预期的答案是
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/ankit_test/as
So, if you understand what I am trying to do, please help me out to solve this BUG. I'll be grateful to you.
所以,如果您了解我要做的事情,请帮助我解决这个问题。我会感激你的。
Thanks in advance!!!
提前致谢!!!
I need to know exactly how I use '*.pdf' in my program one by one without disturbing DEST.
我需要确切地知道如何在我的程序中逐一使用'* .pdf'而不会打扰DEST。
9 个解决方案
#1
4
Your script needs more work. Even after escaping the wildcard, you won't get your expected answer. You will get:
您的脚本需要更多工作。即使在逃避通配符后,您也无法获得预期的答案。你会得到:
ARG1=/home/dev/Examples/*.pdf ARG2=/ankit__test/as
Try the following instead:
请尝试以下方法:
for IP in `cat /opt/ankit/configs/machine.configs`
do
for i in $SRC
do
echo -en "$IP"
echo -en "\n\t ARG1=$i ARG2=$DEST\n\n"
done
done
Run it like this:
像这样运行:
root@ankit1:~/as_prac# ./test.sh "/home/dev/Examples/*.pdf" /ankit__test/as
#2
3
The shell will expand wildcards unless you escape them, so for example if you have
shell将扩展通配符,除非你将它们转义,例如,如果你有
$ ls
one.pdf two.pdf three.pdf
and run your script as
并运行您的脚本
./test.sh *.pdf /ankit__test/as
it will be the same as
它会是一样的
./test.sh one.pdf two.pdf three.pdf /ankit__test/as
which is not what you expect. Doing
这不是你所期望的。干
./test.sh \*.pdf /ankit__test/as
should work.
#3
2
If you can, change the order of the parameters passed to your shell script as follows:
如果可以,请更改传递给shell脚本的参数的顺序,如下所示:
./test.sh /ankit_test/as /home/dev/Examples/*.pdf
That would make your life a lot easier since the variable part moves to the end of the line. Then, the following script will do what you want:
这将使您的生活变得更加容易,因为变量部分移动到行尾。然后,以下脚本将执行您想要的操作:
#!/bin/bash
hello()
{
SRC=$1
DEST=$2
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
done
}
arg2=$1
shift
while [[ "$1" != "" ]] ; do
hello $1 $arg2
shift
done
#4
1
You are also missing a final "done" to close your outer for loop.
你也错过了最后的“完成”以关闭你的外部for循环。
#5
1
OK, this appears to do what you want:
好的,这似乎做你想要的:
#!/bin/bash
hello() {
SRC=$1
DEST=$2
while read IP ; do
for FILE in $SRC; do
echo -e "$IP"
echo -e "\tARG1=$FILE ARG2=$DEST\n"
done
done < /tmp/machine.configs
}
hello "$1" $2
- You still need to escape any wildcard characters when you invoke the script
调用脚本时,仍需要转义任何通配符
- The double quotes are necessary when you invoke the
hello function, otherwise the mere fact of evaluating $1 causes the wildcard to be expanded, but we don't want that to happen until $SRC is assigned in the function
当您调用hello函数时,双引号是必需的,否则仅仅评估$ 1的事实会导致扩展通配符,但我们不希望在函数中分配$ SRC之前发生这种情况。
#6
1
Here's what I came up with:
这就是我想出的:
#!/bin/bash
hello()
{
# DEST will contain the last argument
eval DEST=\$$#
while [ $1 != $DEST ]; do
SRC=$1
for IP in `cat /opt/ankit/configs/machine.configs`; do
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
done
shift || break
done
}
hello $*
Instead of passing only two parameters to the hello() function, we'll pass in all the arguments that the script got.
我们将传递脚本获得的所有参数,而不是仅将两个参数传递给hello()函数。
Inside the hello() function, we first assign the final argument to the DEST var. Then we loop through all of the arguments, assigning each one to SRC, and run whatever commands we want using the SRC and DEST arguments. Note that you may want to put quotation marks around $SRC and $DEST in case they contain spaces. We stop looping when SRC is the same as DEST because that means we've hit the final argument (the destination).
在hello()函数内部,我们首先将最终参数赋值给DEST var。然后我们遍历所有参数,将每个参数分配给SRC,并使用SRC和DEST参数运行我们想要的任何命令。请注意,如果它们包含空格,您可能需要在$ SRC和$ DEST周围加上引号。当SRC与DEST相同时,我们停止循环,因为这意味着我们已经达到最终参数(目标)。
#7
1
For multiple input files using a wildcard such as *.txt, I found this to work perfectly, no escaping required. It should work just like a native bash app like "ls" or "rm." This was not documented just about anywhere so since I spent a better part of 3 days trying to figure it out I decided I should post it for future readers.
对于使用通配符(如* .txt)的多个输入文件,我发现这可以完美地工作,不需要转义。它应该像本机bash应用程序一样工作,如“ls”或“rm”。这并没有记录在任何地方,所以因为我花了3天的时间来试图解决这个问题,所以我决定将它发布给未来的读者。
Directory contains the following files (output of ls)
目录包含以下文件(ls的输出)
file1.txt file2.txt file3.txt
Run script like
像运行脚本一样
$ ./script.sh *.txt
Or even like
甚至喜欢
$ ./script.sh file{1..3}.txt
The script
#!/bin/bash
# store default IFS, we need to temporarily change this
sfi=$IFS
#set IFS to $'\n\' - new line
IFS=$'\n'
if [[ -z $@ ]]
then
echo "Error: Missing required argument"
echo
exit 1
fi
# Put the file glob into an array
file=("$@")
# Now loop through them
for (( i=0 ; i < ${#file[*]} ; i++ ));
do
if [ -w ${file[$i]} ]; then
echo ${file[$i]} " writable"
else
echo ${file[$i]} " NOT writable"
fi
done
# Reset IFS to its default value
IFS=$sfi
The output
file1.txt writable
file2.txt writable
file3.txt writable
The key was switching the IFS (Internal Field Separator) temporarily. You have to be sure to store this before switching and then switch it back when you are done with it as demonstrated above.
关键是暂时切换IFS(内部现场分离器)。您必须确保在切换之前存储它,然后在完成后将其切换回来,如上所示。
Now you have a list of expanded files (with spaces escaped) in the file[] array which you can then loop through. I like this solution the best, easiest to program for and easiest for the users.
现在,您在文件[]数组中有一个扩展文件列表(带有空格转义),然后您可以循环访问它们。我喜欢这个解决方案最好,最容易为用户编程和最简单。
#8
0
There's no need to spawn a shell to look at the $? variable, you can evaluate it directly.
没有必要生成一个shell来查看$?变量,您可以直接评估它。
It should just be:
它应该只是:
if [ $? -eq 0 ]; then
#9
0
You're running
./test.sh /home/dev/Examples/*.pdf /ankit_test/as
and your interactive shell is expanding the wildcard before the script gets it. You just need to quote the first argument when you launch it, as in
并且您的交互式shell在脚本获取之前扩展通配符。你只需要在启动它时引用第一个参数,如
./test.sh "/home/dev/Examples/*.pdf" /ankit_test/as
and then, in your script, quote "$SRC" anywhere where you literally want the things with wildcards (ie, when you do echo $SRC, instead use echo "$SRC") and leave it unquoted when you want the wildcards expanded. Basically, always put quotes around things which might contain shell metacharacters unless you want the metacharacters interpreted. :)
然后,在你的脚本中,引用“$ SRC”,你真正想要带有通配符的东西(即,当你做回声$ SRC,而是使用echo“$ SRC”)并在你想要扩展通配符时不加引号。基本上,除非你想要解释元字符,否则总是在可能包含shell元字符的东西周围加上引号。 :)
#!/bin/bash
hello()
{
SRC=
#!/bin/bash
hello()
{
SRC=$1
DEST=$2
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo $SRC | grep '*' > /dev/null
if test `echo $?` -eq 0 ; then
for STAR in $SRC ; do
echo -en "$IP"
echo -en "\n\t ARG1=$STAR ARG2=$2\n\n"
done
else
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
fi
done
}
hello $1 $2
The above is the shell script which I provide source (SRC) & desitnation (DEST) path. It worked fine when I did not put in a SRC path with wild card ''. When I run this shell script and give ''.pdf or '*'as follows:
以上是我提供源(SRC)和desitnation(DEST)路径的shell脚本。当我没有使用外卡''放入SRC路径时,它工作正常。当我运行这个shell脚本并给出'.pdf或'*'时,如下所示:
root@ankit1:~/as_prac# ./test.sh /home/dev/Examples/*.pdf /ankit_test/as
I get the following output:
我得到以下输出:
192.168.1.6
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/home/dev/Examples/case_howard_county_library.pdf
The DEST is /ankit_test/as but DEST also get manupulated due to '*'. The expected answer is
DEST是/ ankit_test / as,但由于'*',DEST也会被删除。预期的答案是
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/ankit_test/as
So, if you understand what I am trying to do, please help me out to solve this BUG. I'll be grateful to you.
所以,如果您了解我要做的事情,请帮助我解决这个问题。我会感激你的。
Thanks in advance!!!
提前致谢!!!
I need to know exactly how I use '*.pdf' in my program one by one without disturbing DEST.
我需要确切地知道如何在我的程序中逐一使用'* .pdf'而不会打扰DEST。
9 个解决方案
#1
4
Your script needs more work. Even after escaping the wildcard, you won't get your expected answer. You will get:
您的脚本需要更多工作。即使在逃避通配符后,您也无法获得预期的答案。你会得到:
ARG1=/home/dev/Examples/*.pdf ARG2=/ankit__test/as
Try the following instead:
请尝试以下方法:
for IP in `cat /opt/ankit/configs/machine.configs`
do
for i in $SRC
do
echo -en "$IP"
echo -en "\n\t ARG1=$i ARG2=$DEST\n\n"
done
done
Run it like this:
像这样运行:
root@ankit1:~/as_prac# ./test.sh "/home/dev/Examples/*.pdf" /ankit__test/as
#2
3
The shell will expand wildcards unless you escape them, so for example if you have
shell将扩展通配符,除非你将它们转义,例如,如果你有
$ ls
one.pdf two.pdf three.pdf
and run your script as
并运行您的脚本
./test.sh *.pdf /ankit__test/as
it will be the same as
它会是一样的
./test.sh one.pdf two.pdf three.pdf /ankit__test/as
which is not what you expect. Doing
这不是你所期望的。干
./test.sh \*.pdf /ankit__test/as
should work.
#3
2
If you can, change the order of the parameters passed to your shell script as follows:
如果可以,请更改传递给shell脚本的参数的顺序,如下所示:
./test.sh /ankit_test/as /home/dev/Examples/*.pdf
That would make your life a lot easier since the variable part moves to the end of the line. Then, the following script will do what you want:
这将使您的生活变得更加容易,因为变量部分移动到行尾。然后,以下脚本将执行您想要的操作:
#!/bin/bash
hello()
{
SRC=$1
DEST=$2
for IP in `cat /opt/ankit/configs/machine.configs` ; do
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
done
}
arg2=$1
shift
while [[ "$1" != "" ]] ; do
hello $1 $arg2
shift
done
#4
1
You are also missing a final "done" to close your outer for loop.
你也错过了最后的“完成”以关闭你的外部for循环。
#5
1
OK, this appears to do what you want:
好的,这似乎做你想要的:
#!/bin/bash
hello() {
SRC=$1
DEST=$2
while read IP ; do
for FILE in $SRC; do
echo -e "$IP"
echo -e "\tARG1=$FILE ARG2=$DEST\n"
done
done < /tmp/machine.configs
}
hello "$1" $2
- You still need to escape any wildcard characters when you invoke the script
调用脚本时,仍需要转义任何通配符
- The double quotes are necessary when you invoke the
hello function, otherwise the mere fact of evaluating $1 causes the wildcard to be expanded, but we don't want that to happen until $SRC is assigned in the function
当您调用hello函数时,双引号是必需的,否则仅仅评估$ 1的事实会导致扩展通配符,但我们不希望在函数中分配$ SRC之前发生这种情况。
#6
1
Here's what I came up with:
这就是我想出的:
#!/bin/bash
hello()
{
# DEST will contain the last argument
eval DEST=\$$#
while [ $1 != $DEST ]; do
SRC=$1
for IP in `cat /opt/ankit/configs/machine.configs`; do
echo -en "$IP"
echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
done
shift || break
done
}
hello $*
Instead of passing only two parameters to the hello() function, we'll pass in all the arguments that the script got.
我们将传递脚本获得的所有参数,而不是仅将两个参数传递给hello()函数。
Inside the hello() function, we first assign the final argument to the DEST var. Then we loop through all of the arguments, assigning each one to SRC, and run whatever commands we want using the SRC and DEST arguments. Note that you may want to put quotation marks around $SRC and $DEST in case they contain spaces. We stop looping when SRC is the same as DEST because that means we've hit the final argument (the destination).
在hello()函数内部,我们首先将最终参数赋值给DEST var。然后我们遍历所有参数,将每个参数分配给SRC,并使用SRC和DEST参数运行我们想要的任何命令。请注意,如果它们包含空格,您可能需要在$ SRC和$ DEST周围加上引号。当SRC与DEST相同时,我们停止循环,因为这意味着我们已经达到最终参数(目标)。
#7
1
For multiple input files using a wildcard such as *.txt, I found this to work perfectly, no escaping required. It should work just like a native bash app like "ls" or "rm." This was not documented just about anywhere so since I spent a better part of 3 days trying to figure it out I decided I should post it for future readers.
对于使用通配符(如* .txt)的多个输入文件,我发现这可以完美地工作,不需要转义。它应该像本机bash应用程序一样工作,如“ls”或“rm”。这并没有记录在任何地方,所以因为我花了3天的时间来试图解决这个问题,所以我决定将它发布给未来的读者。
Directory contains the following files (output of ls)
目录包含以下文件(ls的输出)
file1.txt file2.txt file3.txt
Run script like
像运行脚本一样
$ ./script.sh *.txt
Or even like
甚至喜欢
$ ./script.sh file{1..3}.txt
The script
#!/bin/bash
# store default IFS, we need to temporarily change this
sfi=$IFS
#set IFS to $'\n\' - new line
IFS=$'\n'
if [[ -z $@ ]]
then
echo "Error: Missing required argument"
echo
exit 1
fi
# Put the file glob into an array
file=("$@")
# Now loop through them
for (( i=0 ; i < ${#file[*]} ; i++ ));
do
if [ -w ${file[$i]} ]; then
echo ${file[$i]} " writable"
else
echo ${file[$i]} " NOT writable"
fi
done
# Reset IFS to its default value
IFS=$sfi
The output
file1.txt writable
file2.txt writable
file3.txt writable
The key was switching the IFS (Internal Field Separator) temporarily. You have to be sure to store this before switching and then switch it back when you are done with it as demonstrated above.
关键是暂时切换IFS(内部现场分离器)。您必须确保在切换之前存储它,然后在完成后将其切换回来,如上所示。
Now you have a list of expanded files (with spaces escaped) in the file[] array which you can then loop through. I like this solution the best, easiest to program for and easiest for the users.
现在,您在文件[]数组中有一个扩展文件列表(带有空格转义),然后您可以循环访问它们。我喜欢这个解决方案最好,最容易为用户编程和最简单。
#8
0
There's no need to spawn a shell to look at the $? variable, you can evaluate it directly.
没有必要生成一个shell来查看$?变量,您可以直接评估它。
It should just be:
它应该只是:
if [ $? -eq 0 ]; then
#9
0
You're running
./test.sh /home/dev/Examples/*.pdf /ankit_test/as
and your interactive shell is expanding the wildcard before the script gets it. You just need to quote the first argument when you launch it, as in
并且您的交互式shell在脚本获取之前扩展通配符。你只需要在启动它时引用第一个参数,如
./test.sh "/home/dev/Examples/*.pdf" /ankit_test/as
and then, in your script, quote "$SRC" anywhere where you literally want the things with wildcards (ie, when you do echo $SRC, instead use echo "$SRC") and leave it unquoted when you want the wildcards expanded. Basically, always put quotes around things which might contain shell metacharacters unless you want the metacharacters interpreted. :)
然后,在你的脚本中,引用“$ SRC”,你真正想要带有通配符的东西(即,当你做回声$ SRC,而是使用echo“$ SRC”)并在你想要扩展通配符时不加引号。基本上,除非你想要解释元字符,否则总是在可能包含shell元字符的东西周围加上引号。 :)
DEST=