0

How can I create a specific line in another file using bash please? Like

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

where the file ".beebrc" has a line 'keep= x' and "$key" is created in the main script.

But how do I define "$key" please? And write it into ".beebrc" as a new line at position/line 8? The full function is -

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
 };

Does this help in defining it all? (Sorry, should've put it in at first)

boudiccas
  • 297
  • 3
  • 13
  • possible duplicate of [How do I prompt for input in a Linux shell script?](http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script) – Reinstate Monica Please Jul 11 '14 at 18:32
  • Not really, I'm looking for a number, not a "y, n, q", and also to write the number into another file – boudiccas Jul 11 '14 at 18:34

3 Answers3

0

Perhaps:

read -p "Please input the days you want to keep: " key  ## Ask.
echo "keep=\"$key\"" > .beebrc                          ## Store.
konsolebox
  • 72,135
  • 12
  • 99
  • 105
0

Use read to capture user input into a variable, and then write it to your file.

For example:

echo "Please input the days you want to keep "
read key
echo $key > .beebrc
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

This script:

  1. Prompts for input and stores the value in $key
  2. Tests if .beebrc exists and that a line beginning "keep=" exists in it. If so, replace the keep= line with keep=$key
  3. Otherwise append a new line/create the file with keep=$key.

This will need validation added because user input should not be trusted. (this answer might help)

Andrew
  • 361
  • 1
  • 3