So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the file are either odd or even as it checks each number on each line. For all I know my script structure is bad, but the code I have below does not produce a desired result.

if [ -f $exist ]; then
 echo "file exist processing"
 echo "file does not exist"
while read exist
if [ $((exist % 2)) -eq 0 ]; then
 echo "even"
echo "odd"

Try this:

while read number
    if [ $((number % 2)) -eq 0 ]; then 
        echo even
        echo odd 
done < "$exist"

read reads into variables on it's command line. So you have to feed the file data into the loop so read can get it. Here we do that with the < "$exist" after the done of the while loop.

The read command doesn't have a manpage because it's a shell builtin. To see how it works, type man sh (hint, search for readonly because that's a lot easier to find.)


for the most part you don't have to worry about stuff like:

if    [ -e "$file" ]
then  echo exists
else  echo not exists

when people do that kind of stuff they're really just spinning their wheels. it also can be problematic in that there's little to connect between the behavior and output format of one script versus another. and it's all wasted effort anyway because:

sh -c 'exec <not_exist; echo can i\?' my_zero

my_zero: 1: my_zero: cannot open not_exist: No such file

a scripted POSIX shell dies with cause written to stderr when a special shell-builtin is the object of any failed shell-redirection. the special builtins are:

: continue break exec set . shift unset
times trap exit export readonly eval return

their special status can be demoted by calling them via command:

sh -c 'command exec <not_exist; echo can i\?' my_zero

my_zero: 1: my_zero: cannot open not_exist: No such file
can i?

and so the most straight-forward and effective strategy to dealing robustly with read/write files is to put the file directly on the file-descriptor with which you mean to interact, and the let the shell handle any/all error output as necessary. it will just happen if it should and if you write your scripts with an eye for allowing actions to speak for themselves.

sh -c '
        for   f
        do    exec   <"$f"
              printf "\n%-5s%-$((${#f}+2))s%s\n" === "$f" ===
    cat_fname a[1-9]/a[1-9].txt not_exist /dev/fd/0'

===  a1/a1.txt  ===
1 1 1 11 1 1 1 1

===  a2/a2.txt  ===
2 2 2 12 2 2 2 2

===  a3/a3.txt  ===
3 3 3 13 3 3 3 3

===  a4/a4.txt  ===
4 4 4 14 4 4 4 4

===  a5/a5.txt  ===
5 5 5 15 5 5 5 5

===  a6/a6.txt  ===
6 6 6 16 6 6 6 6

===  a7/a7.txt  ===
7 7 7 17 7 7 7 7

===  a8/a8.txt  ===
8 8 8 18 8 8 8 8

===  a9/a9.txt  ===
9 9 9 19 9 9 9 9
sh: 4: cannot open not_exist: No such file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.