如何使用bash触摸和ln数组元素?

时间:2016-05-07 01:35:44

标签: bash

我创建了以下代码:

#!/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

问题:

  • mkdir -p命令未创建该文件夹。
  • touch命令无法创建根据数组命名的文件 元素。其中一个数组元素包含两个单词,它们之间有空格。我根据Khushneet的答案创建了for循环以处理这些数组元素。
  • 我无法在当前目录或子目录“folderA”中创建触摸文件及其目标的软链接。

问题:

  • 如何解决这3个问题。

  • 什么是IFS以及如何使用它?

感谢。

4 个答案:

答案 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

<强>数目:

  1. 说mkdir -p $ rdir“folderA”命令没有创建是错误的 文件夹“folderA”。该文件夹创建为 $ HOME /斌/测试/〜/斌/测试/ folderA。但是,这不是意图。 需要它作为$ HOME / bin / Test / folderA。错误引起的,因为 由@sjsam指出“〜不会在双引号内展开”和 变量$ HOME应该用于表示主目录。
  2. 问题2&amp; 3由于“如上所述的拼写错误”而发生 @shelter,@ Jonathan Leffler和@sjsam,以及过分热心的使用 “”。通过将“”的使用限制为字符串而不是使用它 附上$ variable和$ {array}足以让脚本获得 工作
  3. 反斜杠\通常在命令行中用于处理空间 诸如“Apple \ Pie”中的文件名是多余的,即“Apple Pie” 足够,因为使用了IFS =“”。
  4. 用于执行脚本的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>
    

    我要感谢所有答案贡献者的快速回复。 :)