开发者

Bash: need help with my bash script - Typeset

开发者 https://www.devze.com 2023-03-16 12:45 出处:网络
I am reading some other developer script and I run across something I dont quite understand. Please开发者_开发问答 help

I am reading some other developer script and I run across something I dont quite understand. Please开发者_开发问答 help

typeset -u DOC_RET_CODE=`grep ^${PRNT_JOB_NAME}${SEQ_NUM} ${INPUT_FILE} |cut -c273-276`
if [ "${DOC_RET_CODE}" = "GOOD" ]

I look up typeset - u and it seems like it generate read-only variable, but not sure what it doing there. For grep, I usually pipe an input like ls | grep test, but grep by itself like this, I am not so sure. I know cut -c273-276, but 4 characters out from position 273-276. So what exactly does this script do?


The back-tick command (which would be better enclosed in $(...)) is grepping for a line starting with the print job name and sequence number from the input file, and then the 'cut' command is collecting columns 273-276 (4 characters). The upper-case version of this value (typeset -u) is assigned to $DOC_RET_CODE. The test line checks whether the document return code is GOOD and does something (not shown) if it is ... and maybe something else if the status is not good.


> help typeset
typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
    Set variable values and attributes.

    Obsolete.  See `help declare'.
> help declare
declare: declare [-aAfFgilrtux] [-p] [name[=value] ...]
    Set variable values and attributes.
    …
    Options which set attributes:
      -u        to convert NAMEs to upper case on assignment

In other words, this is making everything (the result of the grep|cut pipe) uppercase to avoid a tr a-z A-Z and allow a simple comparison against GOOD.

For your other question, grep is being run against a filename ${INPUT_FILE}. You can run that command as is (after manually substituting the variables)


It's not by itself; it's passed the argument ${INPUT_FILE}, and it will read that file instead of its standard input. The "useless use of cat" version would be cat ${INPUT_FILE} | grep ....

Note that, per the earlier answer, bash has decided to drop compatibility and deprecate typeset. typeset is largely compatible between ksh, bash, and zsh.

0

精彩评论

暂无评论...
验证码 换一张
取 消