我创建了以下代码:
#!/bin/bash
rdir="~/bin/Test/"
echo $rdir
echo $rdir"folderA"
/bin/mkdir -p $rdir"folderA"
# files is an array. () is used to enclose array elements and
# each array element enclosed by "".
files=(
"Apple"
"Apple\ Pie")
IFS="" # What is this for?
for f in ${files[@]}
do
echo "$rdir${f}"
/bin/touch "$rdir${f}"
/bin/ln -s "$rdir${f}" "$rdir${f}"_linktarget"
/bin/ln -s "$rdir${f}" "$rdirfolderA"${f}"_linktarget"
done
我将此代码称为test2.sh,并在执行后得到以下结果:
$ ./test2.sh
~/bin/Test/
~/bin/Test/folderA
./test2.sh: line 22: unexpected EOF while looking for matching `"'
./test2.sh: line 25: syntax error: unexpected end of file
$ ls -a
~ . .. test2.sh
问题:
问题:
如何解决这3个问题。
什么是IFS以及如何使用它?
感谢。
答案 0 :(得分:1)
我可以发现很少的语法错误..(希望这有帮助)
/bin/mkdir -p $rdir"folderA"
- >更改为/bin/mkdir -p "${rdir}folderA"
...然后是以下sym-links,再次进行类似的更改
/bin/ln -s "$rdir${f}" "$rdir${f}"_linktarget"
/bin/ln -s "$rdir${f}" "$rdirfolderA"${f}"_linktarget"
- >改为:
/bin/ln -s "${rdir}${f}" "${rdir}${f}_linktarget"
/bin/ln -s "${rdir}${f}" "${rdir}folderA${f}_linktarget"
...基本上$ var和$ {var}之间存在细微差别,尤其是。在字符串连接中使用时。
答案 1 :(得分:1)
这些将是您的最终脚本(基于Shellcheck)。
#!/bin/bash
rdir="$HOME/bin/Test/"
# The original statment was rdir="~/bin/Test/"
# You need to use $HOME, as ~ doesn't expand inside double quotes
echo "$rdir" #enclosed the whole variable in double quotes
rdirfolderA="$rdir"/folderA #added this line
/bin/mkdir -p "$rdirfolderA"
#ihave put $rdir inside quotes to prevent word splitting and globbing
# files is an array. () is used to enclose array elements and
# each array element enclosed by "".
files=(
"Apple"
"Apple\ Pie")
#It is recommended to store the old IFS like this
IFS_old="$IFS"
IFS="" # What is this for? Sets the field separator to null
#you need this because you don't have a separating character between the
#strings in the array
for f in "${files[@]}" #Double quote array expansions to avoid re-splitting elements.
do
echo "$rdir${f}"
/bin/touch "$rdir${f}"
/bin/ln -s "$rdir${f}" "$rdir${f}"_linktarget # Removed the additional double-quote at the end.
/bin/ln -s "$rdir${f}" "$rdirfolderA/${f}"_linktarget
done
#Restore the old IFS
IFS="$IFS_old"
#Do something else with your script.
答案 2 :(得分:1)
#!/bin/bash
rdir="~/bin/Test/"
echo $rdir
echo $rdir"folderA"
/bin/mkdir -p ${rdir}"folderA"
# files is an array. () is used to enclose array elements and
# each array element enclosed by "".
files=(
"Apple"
"Apple\ Pie"
"Mango")
IFS="" # What is this for?
for f in ${files[@]}
do
echo "${rdir}${f}"
/bin/touch "${rdir}${f}"
/bin/ln -s $rdir${f} $rdir${f}"_linktarget"
/bin/ln -s ${rdir}${f} ${rdirfolderA}${f}"_linktarget"
done
lrwxrwxrwx 1 monk monk 16 May 7 00:33 Apple_linktarget -> ~/bin/Test/Apple
lrwxrwxrwx 1 monk monk 21 May 7 00:33 Apple\ Pie_linktarget -> ~/bin/Test/Apple\ Pie
lrwxrwxrwx 1 monk monk 16 May 7 00:33 Mango_linktarget -> ~/bin/Test/Mango
答案 3 :(得分:0)
工作脚本:
#!/bin/bash
rdir=$HOME"/bin/Test/"
echo $rdir
echo $rdir"folderA"
/bin/mkdir -p $rdir"folderA"
# files is an array. () is used to enclose array elements and
# each array element enclosed by "".
files=(
"Apple"
"Apple Pie")
IFS=""
for f in ${files[@]}
do
echo $rdir${f}
/bin/touch $rdir${f}
/bin/ln -s $rdir${f} $rdir${f}"_linktarget"
/bin/ln -s $rdir${f} $rdir"folderA/"${f}"_linktarget"
done
<强>数目:强>
用于执行脚本的Bash版本:
$ bash --version
GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
我要感谢所有答案贡献者的快速回复。 :)