将用户输入行附加到传递给tar的列表

时间:2018-05-29 22:24:37

标签: bash tar

您好我想在我的脚本中创建用户选择的动态文件列表。之后我想在tar中使用list表示。 (我想添加一些文件(由用户选择)到tar档案)

!#/bin/bash
while [ $A != "exit" ]; do
read FILE     #user types correct filename         

#files will be chosen via dialog menu but it isn't important now


echo "If you don't want to choose more files type 'exit'
read A
done

tar -cvf FILE1 FILE2 FILE3 ........

我认为我应该使用另一个循环,但我不知道如何将此循环与变量FILE内部循环(while)以及如何将此循环与tar连接

1 个答案:

答案 0 :(得分:0)

有关将内容逐行读入shell的指导,请参阅BashFAQ #1,有关数组的介绍,请参阅BashFAQ #5

#!/usr/bin/env bash

files=( )                          # initialize an empty array
while IFS= read -r line; do        # as long as there is input...
  [[ $line = exit ]] && break      # ...and it is not the word "exit"...
  files+=( "$line" )               # ...append that input to the array.
done

tar -czf file.tar.gz "${files[@]}" # then tack that array onto a tar command