define“$ key”在脚本中使用它,但也存储它

时间:2014-07-11 18:28:48

标签: bash

如何使用bash在另一个文件中创建特定行?喜欢

echo "Please input the days you want to keep "
$key= ?
touch .beebrc; keep="$key"

其中文件“.beebrc”有一行'keep = x',并且在主脚本中创建了“$ key”。

但是如何定义“$ key”呢?并将其作为位置/第8行的新行写入“.beebrc”?完整的功能是 -

function trim {
    echo;
    read -t "$temi" -n1 -p ""$bldgrn" Do you want to delete some of your download history? [y/n/q/r]          $(tput sgr0)" ynqr ;
    case "$ynqr" in
        [Yy]) echo
          read -t "$temi" -n3 -p ""$bldgrn" Please input the days you want to keep $(tput sgr0)" key  ## ask
if test -e .beebrc && grep -q "^keep=" .beebrc 2>/dev/null ; then
sed -i "s/^keep=.*/keep=$key/" .beebrc
 else
echo "keep=$key" >> .beebrc
 #fi
          cd /home/$USER/.get_iplayer
          eval "$player" --trim-history "$key"; cd; ques;
          #echo;;
    [Nn]) ques;;
        [Qq]) endex;;
        [Rr]) exec "$beeb";;
    * ) echo ""$bldgrn" Thank you $(tput sgr0)";;
    esac
     fi
 };

这有助于定义所有内容吗? (对不起,应该先把它放进去)

3 个答案:

答案 0 :(得分:0)

也许:

read -p "Please input the days you want to keep: " key  ## Ask.
echo "keep=\"$key\"" > .beebrc                          ## Store.

答案 1 :(得分:0)

使用read将用户输入捕获到变量中,然后将其写入文件。

例如:

echo "Please input the days you want to keep "
read key
echo $key > .beebrc

答案 2 :(得分:0)

#!/bin/bash
read -p "Please input the days you want to keep: " key
if test -e .beebrc && grep -q "^keep=" .beebrc 2>/dev/null ; then
    sed -i "s/^keep=.*/keep=$key/" .beebrc
else
    echo "keep=$key" >> .beebrc
fi

这个脚本:

  1. 提示输入并将值存储在$ key
  2. 测试.beebrc是否存在以及一行开头" keep ="存在于其中。如果是这样,请使用keep = $ key
  3. 替换keep =行
  4. 否则追加新行/使用keep = $ key创建文件。
  5. 这将需要添加验证,因为不应信任用户输入。 (this answer might help