0

In my script, I transform data to a more readable format. But I am having an issue. If no arguments are typed, just the script, I want it to come back "Please enter a filename" which I have done. But for some reason, it will not display that when tested. I did it by telling the system if argument is greater than 0, execute, otherwise, echo the message.

*I am not looking for advice on the substitution code. For specific reasons, it cannot be changed, so please do not give advice on that piece.

for myvar
do

        if [ $# -gt 0 ]
        then

                        if [ -e "$myvar" ]
                        then :
                        else
                                echo "$myvar cannot be found."
                        exit
                        fi

                echo "This is the filname:" $myvar 

                cut -f1 -d, $myvar > social_security
                cut -f2 -d, $myvar > last_name
                cut -f3 -d, $myvar > first_name
                sed 's/^ *//' last_name > last_name_draft
                sed 's/^ *//' first_name > first_name_final
                sed 's/-//' social_security > social_security_draft
                sed 's/-//' social_security_draft > social_security_final
                sed 's/$/:/' last_name_draft > last_name_final
                paste -d\  first_name_final last_name_final social_security_final  > final_draft
                sed '1d' final_draft > final
                cat final

                echo -en '\n'   

        else
                echo "Please enter a filename."
        fi
done
  • you tags are wrong – mikeserv Jul 19 '15 at 17:04
  • 2
    @mikeserv I would appreciate a little more elaboration. – Tanner Di Bella Jul 19 '15 at 17:23
2

Think about the first line of your script: for myvar. How many times will this for loop be executed if there are no arguments?

  • Ya but regardless of the loop it should still display the else would it not? – Tanner Di Bella Jul 19 '15 at 17:43
  • The if/else is inside the for loop. So if the for loop is never executed, neither is the if or the else. – Jander Jul 19 '15 at 17:47
  • So the loop needs to be in the if/else – Tanner Di Bella Jul 19 '15 at 17:51
-1

Yes, you should first check if there are any arguments or not and then proceed.

if [ $# -gt 0 ]
then
    for myvar
    do
        if ! [ -e "$myvar" ]
            echo "$myvar cannot be found."
            exit
        fi

        echo "This is the filname:" $myvar 

        cut -f1 -d, $myvar > social_security
        cut -f2 -d, $myvar > last_name
        cut -f3 -d, $myvar > first_name
        sed 's/^ *//' last_name > last_name_draft
        sed 's/^ *//' first_name > first_name_final
        sed 's/-//' social_security > social_security_draft
        sed 's/-//' social_security_draft > social_security_final
        sed 's/$/:/' last_name_draft > last_name_final
        paste -d\  first_name_final last_name_final social_security_final  > final_draft
        sed '1d' final_draft > final
        cat final

        echo -en '\n'   
    done
else 
    echo "Please enter a filename."
fi

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.