0

How can I accept multiple-line input into a same script. Or in other words to process multiple files with the following script:

#!/bin/bash
echo 'Enter file names (wild cards OK)'
read input_source
if test -f "$input_source"
then 
sort $var | uniq -c | head -10
fi

3 Answers 3

1

Add a for loop:

#!/bin/bash
echo 'Enter file names (wild cards OK)'
read files
for input_source in $files ; do
    if test -f "$input_source" ; then 
        sort $var | uniq -c | head -10  # You probably should include $input_source somewhere
    fi
done
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! And if I want to repeat the sort $input_source | uniq -c | head -10 for the input files over and over until the user indicates end-of-file. This is done by entering the single character Ctrl-d as a file name. Any ideas ??
1

Use a while loop:

while read input_source 
do
# Do something with $input_source
done

Comments

1

Just cat all the files that match the input pattern-:

#!/bin/bash
echo 'Enter file names (wild cards OK)'
read input_source
cat $input_source | sort | uniq -c | head -10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.