Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从数组写入文件bash和新行_Bash_Macos - Fatal编程技术网

从数组写入文件bash和新行

从数组写入文件bash和新行,bash,macos,Bash,Macos,我正在尝试编写一个脚本,为Pashua生成一个模板文件,一个用于在osx上创建GUI的perl脚本 我想为阵列中的每个项目装箱一个实例,因此理想的输出是: AB1.type = openbrowser AB1.label = Choose a master playlist file AB1.width=310 AB1.tooltip = Blabla filesystem browser AB2.type = openbrowser AB2.label = Choose a master p

我正在尝试编写一个脚本,为Pashua生成一个模板文件,一个用于在osx上创建GUI的perl脚本

我想为阵列中的每个项目装箱一个实例,因此理想的输出是:

AB1.type = openbrowser
AB1.label = Choose a master playlist file
AB1.width=310
AB1.tooltip = Blabla filesystem browser

AB2.type = openbrowser
AB2.label = Choose a master playlist file
AB2.width=310
AB2.tooltip = Blabla filesystem browser
…对于阵列的其余部分,依此类推:

我现在用来写入文本文件的是:

count=1
saveIFS="$IFS"
IFS=$'\n'
array=($(<TEST.txt))
IFS="$saveIFS"
for i in "${array[@]}"; do declare AD$count="$i"; ((count++)); done
for i in "${array[@]}"; do echo "AD$count".type = openbrowser "AD$count".label = Choose   a master playlist file \n "AD$count".width=310 \n "AD$count".tooltip = Blabla filesystem browser \n" >> long.txt; done 

但是\n不会在文本文件中产生换行符,而且我很确定有一种更好的方法可以做到这一点,ideas?

要在echo语句中使用转义字符,必须使用echo-e。因此,您的代码应该如下所示:

for i in "${array[@]}"; do echo -e "AD$count".type = openbrowser "AD$count".label = Choose   a master playlist file \n "AD$count".width=310 \n "AD$count".tooltip = Blabla filesystem browser \n" >> long.txt; done 

要在echo语句中使用转义字符,必须使用echo-e。因此,您的代码应该如下所示:

for i in "${array[@]}"; do echo -e "AD$count".type = openbrowser "AD$count".label = Choose   a master playlist file \n "AD$count".width=310 \n "AD$count".tooltip = Blabla filesystem browser \n" >> long.txt; done 

最干净的方法可能是使用heredoc:

cat << EOF > out.txt
line 1
line 2
EOF

最干净的方法可能是使用heredoc:

cat << EOF > out.txt
line 1
line 2
EOF

第一个for循环创建了一组从未使用过的变量。您的第二个for循环在每次迭代中执行完全相同的操作,因为它实际上不使用$i或您创建的任何$ADn变量

由于您尚未显示文本文件中的内容,因此很难知道您正试图实现什么,但这里有一个尝试:

count=1
saveIFS="$IFS"
IFS=$'\n'
array=($(<TEST.txt))
IFS="$saveIFS"
for i in "${array[@]}"
do
    echo "AB${count}.type = openbrowser"
    echo "AB${count}.label = Choose a master playlist file"
    echo "AB${count}.width=310"
    echo "AB${count}.tooltip = Blabla filesystem browser"
    echo "some text with a line from the file: $i"
    (( count++ ))
done >> long.txt
但如果您正在执行类似操作,则不需要阵列:

count=1
while read -r i
do
    echo "AB${count}.type = openbrowser"
    echo "AB${count}.label = Choose a master playlist file"
    echo "AB${count}.width=310"
    echo "AB${count}.tooltip = Blabla filesystem browser"
    echo "some text with a line from the file: $i"
    (( count++ ))
done < TEST.txt >> long.txt

第一个for循环创建了一组从未使用过的变量。您的第二个for循环在每次迭代中执行完全相同的操作,因为它实际上不使用$i或您创建的任何$ADn变量

由于您尚未显示文本文件中的内容,因此很难知道您正试图实现什么,但这里有一个尝试:

count=1
saveIFS="$IFS"
IFS=$'\n'
array=($(<TEST.txt))
IFS="$saveIFS"
for i in "${array[@]}"
do
    echo "AB${count}.type = openbrowser"
    echo "AB${count}.label = Choose a master playlist file"
    echo "AB${count}.width=310"
    echo "AB${count}.tooltip = Blabla filesystem browser"
    echo "some text with a line from the file: $i"
    (( count++ ))
done >> long.txt
但如果您正在执行类似操作,则不需要阵列:

count=1
while read -r i
do
    echo "AB${count}.type = openbrowser"
    echo "AB${count}.label = Choose a master playlist file"
    echo "AB${count}.width=310"
    echo "AB${count}.tooltip = Blabla filesystem browser"
    echo "some text with a line from the file: $i"
    (( count++ ))
done < TEST.txt >> long.txt

从here doc读取替换扩展计数变量i:


从here doc读取替换扩展计数变量i: