1

I am running a bash script with its parameters/arguments as files. From inside the script, how do I get the contents of those files?

I run the script with this command line:

sh script_name animal.txt stars.txt

animal.txt contains "giraffe".

From the script, how do I get the contents (giraffe) of animal.txt? I am very new to bash scripts, so any help is appreciated, thanks.

Hauke Laging
  • 90,279
eltigre
  • 1,215
  • If it's a bash script why do you call it as sh ... and not as bash ...? – Hauke Laging Jan 20 '15 at 03:34
  • I didn't know that there was a difference. My testing up until this point has been OK using sh. I'll start using bash though. Anyway, could you help with my problem? – eltigre Jan 20 '15 at 03:36
  • Certain features don't work if bash is called as sh. As long as you don't use the bash-specific features (which is typical for beginners) both ways work. – Hauke Laging Jan 20 '15 at 03:40

3 Answers3

2
for f do exec <"$f"
    : handle stdin
done

A non-interactive shell will treat any redirection from a file that cannot be read or that does not exist when associated w/ a special builtin as a fatal error and exit immediately with a meaningful diagnostic message written to stderr. So either your parameters are valid, readable files and the above statement will do nothing useful until you replace the : ... part w/ something useful, or the user has provided an invalid parameter and the script exits meaningfully.

When you do...

for var in ...; do : compound command list on "$var"; done

The in ...; bit is an optional statement in the syntax which enables you to substitute a parameter set for the default set - which is your argument list.

So...

for var do : compound command list on "$var"; done

...is probably what you're looking for, here.

for f do exec <"$f"
    : now do some stuff w/ stdin
done

...where the shell will iteratively assign to stdin and validate as readable files any arguments provided your script for all commands that follow the exec statement until all parameters are exhausted and the for loop is done. If it cannot do this then it will format and print your error message for you in a standard form to which the user is already accustomed.

mikeserv
  • 58,310
0
first_file="$1"
test -f "$first_file" || exit 2
file_content="$(<"$first_file")"
echo "$file_content"
Hauke Laging
  • 90,279
  • So I assume file_contents will be a string. Would concatenating it like: a=$file_content$anotherstring work? – eltigre Jan 20 '15 at 03:55
  • @eltigre Yes. But even though it is not necessary in this case you should get used to quote everything: a="${file_content}${anotherstring}" – Hauke Laging Jan 20 '15 at 04:08
  • Note that file_content will contain the content of $first_file stripped of all trailing newline characters provided there is no NUL characters in there. – Stéphane Chazelas Jan 20 '15 at 15:03
-1

You can do it:

#!/bin/bash
while read line ;
    echo "$line"
    #######
    #######
    ########
done < "$1"

description: each $line is a content of complete line of your file.

usage : ./script.sh file.txt
Hauke Laging
  • 90,279
PersianGulf
  • 10,850