• Designing Program Output
    IoT 과정 2017. 7. 6. 09:51

    Variable Attributes(typeset)

    typse -L10 var_name


    typeset -L10 var_name="value string"

    Attribute/typeset option/sample

    Left Justify    -L       "string    "
    Right Justify  -R       "    string"
    Lower Case   -l        "string"
    Upper Case   -u       "STRING"

    Display Formatted Data

    Formatted:    print   "$var"
    Unformatted: print    $var
    Conversion:   printf  "%.10s"  "$var"      //built-in cmd인 print와 다르게 external cmd인 printf는 c언어의 printf와 같다. man printf 커맨드로 메뉴얼 페이지에서 확인

    #! Formatted, Unformatted의 예..

    $ var='this is a test string'
    $ print $var
    this is a test string
    $ print "$var"
    this is a test string
    $ var='this           is     a   test        string'
    $ print $var
    this is a test string                             //여러 공백들이 하나로 대체된다.
    $ print "$var"
    this           is     a   test        string      //의도한 string이 출력된다
    $ cp file1                           file2         //shell은 공백의 중복을 하나의 공백으로 대체하므로 이러한 명령도 의도대로 잘 사용된다. 물론 의도한 공백을 넣은 string을 출력할 땐 

    #! printf의 예...

    # printf.sh
    # conversion at output time

    var="posix shell programming"
    # notice, printf does not produce an automatic linefeed

    printf "%.5s" "$var"
    # output is converted to a 5 character string
    # output: "posix"$

    printf "%30s"  "$var"
    # output is extended to right justified 30 char string
    # no linefeed before the next shell prompt '$'
    # output: "posix shell programming       "$
    printf "%-30s"  "$var"
    # output is extended to left justified 30 char string
    # output: "       posix shell programming"$

    printf "\n%.11s\n" "$var"
    # output is trimmed to 11 characters, linefeed added

    printf "Interpreter:%11s\n" "$var"
    # output includes some added text, then 11 characters


    UNIX Command Reference Section 
    알아도 한 번 더 보고 갈만큼 유용

    cut   : Extract data
    grep : Search/extract
    tail   : Show last n lines
    wc   : General counter
    sort  : Sorts data
    tr     : Translate/Squees;Change old to new
    pr    : Print files

    !! 아래는 처음보지만 흥미로운 기능


    cursor 이동, 텍스트 줄긋기, 드래그 한 것 처럼 하이라이팅 할 때, 로그인 페이지에서 비밀번호 칠 때처럼 글자 안보이게 입력하기(echo 해제) 등과 같은 기능들을 수행할 수 있다.

    Command to enable/disable the capnames

    Cursor Positioning

    one common capnames to control the cursor position
    home : Moves cursor to the top left corner of the screen (home position)
    cup x y : Sets cursor position; needs ros (x) and column (y) down from the home position
    cuu1, cud1, cub1, cuf1 : Sets cursor {up, down, left, forward} one line

    Some useful shell variables
    COLUMNS : Sets the number of columns in the current window
    LINES : Sets the number of rows in the current window

     $ cat middle.sh
            # middle.sh
            # print in the middle of the window
            # use an alias to save typing later
            alias go="tput cup"

            back="$(tput cub1)"
            tput clear

            # find the middle of the screen (bc is a calculator tool)
            mid_col="$(print $COLUMNS/2 | bc)"
            mid_row="$(print $LINES/2 | bc)"

            # position the cursor in the middle of the screen
            go $mid_row $mid_col

            # backup half the width of the text string
            print -n "${back}${back}${back}"
            print -n "MIDDLE"

            # move to the last line in the window
            go $LINES 1


    'IoT 과정' 카테고리의 다른 글

    Branches and Logic Testing  (0) 2017.07.06
    Shell Arithmetic  (0) 2017.07.06
    Designing Program Output  (0) 2017.07.06
    User Input  (0) 2017.07.05
    Variable  (0) 2017.07.05
    Shell Scripting  (0) 2017.07.05

    댓글 0

Designed by Tistory.