1

I have been doing a bash programming assignment for Uni and everything is working, except the Automark software (which is blackbox) is giving me a 0/5.. I contacted my Uni lecturer and he replied that:

"There appears to be some confusion about what "reading from standard input" means. This means you read from the keyboard of your terminal NOT from the command line.... This error is in histo (previous program). It also appears to be in histoplot. (program I'm talking about here)"

Now I have NO IDEA WHAT THIS MEANS. I have looked all over the internet, and I thought the my code is reading from standard input.

Isn't that what "While Read Line" does? My program gets data and turns it into a Histoplot that looks like this:

 1 1
 2 3
 3 2
 4 1

and turns it into this:

 1 #
 2 ###
 3 ##
 4 #

Here is my code (Sorry there's so much):

 #!/bin/bash

 addWordcount=0
 customHash=0
 customHashchoice="#"

 scale=0


 for i in $*
 do
         if [[ $i = "-c" ]]
     then
     addWordcount=1 
     elif [[ $i = "-p" ]] 
     then
     customHash=1

     elif [[ $i = "-s" ]]   
     then
     scale=1
         fi
 done
 for x in $*
 do 
 if [ -f $x ] ; then
         while read line    
         do     
             lineAmnt=${line% *}
             hashNo=${line##* }
             for ((i=0; i<hashNo; i++))

            do
            hashes+="#" 
            #hashNumber=$(expr $hashNumber - 1)
            done    
        printf "%3s" $lineAmnt 

        if [[ $addWordcount -eq 1 ]] 
        then
            printf "%5s"$hashNo
        fi

        printf " $hashes\n"
        hashes=""   
    done < $x
     fi
 done
1
  • while read line does read from stdin. stdin is not always (or even usually) a keyboard, but many people conflate the two. Do not allow yourself to fall into the trap of thinking that stdin is a keyboard. Commented Apr 26, 2014 at 2:09

1 Answer 1

2

while read line does read from stdin, but you have used a redirection. By writing while read line; do ... done < $x, you are making the file named in $x be the stdin of the while read loop instead of reading from stdin of your bash script. That is, that script's stdin is not the same as the stdin of the while loop.

Just omit the redirection.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your input! What do you mean by "Omit the redirection"? remove the "< $x" third to last line?
@BarneyChambers: Yes. < $x is an instance of a redirection, thanks to the <; more specifically, it is an input redirection (by contrast, redirecting stdout to a file with > outfile is an instance of output redirection).

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.