0

I need to add new lines with specific information to one or multiple files at the same time.

I tried to automate this task using the following script:

for i in /apps/data/FILE*
do 
   echo "nice weather  20190830 friday" >> $i
done

It does the job yet I wish I can automate it more and let the script ask me for to provide the file name and the line I want to add.

I expect the output to be like

enter file name : file01
enter line to add : IWISHIKNOW HOWTODOTHAT 

Thank you everyone.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
TenEM
  • 127
  • 1
  • 8

3 Answers3

1

In order to read user input you can use

read user_input_file
read user_input_text
read user_input_line

You can print before the question as you like with echo -n:

echo -n "enter file name : "
read user_input_file

echo -n "enter line to add : "
read user_input_text

echo -n "enter line position : "
read user_input_line

In order to add line at the desired position you can "play" with head and tail

head -n $[$user_input_line - 1] $user_input_file > $new_file
echo $user_input_text >> $new_file
tail -n +$user_input_line $user_input_file >> $new_file
A. Rosano
  • 46
  • 4
1

Requiring interactive input is horrible for automation. Make a command which accepts a message and a list of files to append to as command-line arguments instead.

#!/bin/sh
msg="$1"
shift
echo "$msg" | tee -a "$@"

Usage:

scriptname "today is a nice day" file1 file2 file3

The benefits for interactive use are obvious -- you get to use your shell's history mechanism and filename completion (usually bound to tab) but also it's much easier to build more complicated scripts on top of this one further on.

The design to put the message in the first command-line argument is baffling to newcomers, but allows for a very simple overall design where "the other arguments" (zero or more) are the files you want to manipulate. See how grep has this design, and sed, and many many other standard Unix commands.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can use read statement to prompt for input,

read does make your script generic, but if you wish to automate it then you have to have an accompanying expect script to provide inputs to the read statement.

Instead you can take in arguments to the script which helps you in automation.. No prompting...

#!/usr/bin/env bash

[[ $# -ne 2 ]] && echo "print usage here" && exit 1

file=$1 && shift
con=$1


for i in `ls $file`
do
  echo $con >> $i
done

To use:

./script.sh "<filename>" "<content>"

The quotes are important for the content so that the spaces in the content are considered to be part of it. For filenames use quotes so that the shell does not expand them before calling the script.

Example: ./script.sh "file*" "samdhaskdnf asdfjhasdf"

Sam Daniel
  • 1,800
  • 12
  • 22
  • `\`ls $file\`` is doubly, no, triply wrong. Those are [useless backticks](http://www.iki.fi/era/unix/award.html#backticks) on a [useless `ls`](http://www.iki.fi/era/unix/award.html#ls) with [broken quoting.](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Aug 31 '19 at 08:42