Echo and grep issues

Is there an environment issue that would not allow the following not store and pass the value into this field:

underScorePresent=`echo $USER | grep "_" | wc -l`

It is running on a new redhat 6.5 OS. The value $USER is set to cpac. It is a vendor code and they are saying it is environment issue and not a code/script issue. Any thougths?

They mean that $USER is set by some shells only.
I have always used "${USER:-$LOGNAME}" therefore - display LOGNAME if USER is not set.
BTW you can simplify the shell code to

underScorePresent=`echo "${USER:-$LOGNAME}" | grep -c "_"`

(as well returns 0 or 1).
Or to this one

[[ "${USER:-$LOGNAME}" != *_* ]]; underScorePresent=$?

Or even to this one (returns 0 or a positive number)

underScorePresent=`expr x"${USER:-$LOGNAME}" : x".*_"`
1 Like