티스토리 뷰

IoT 과정

HPUX - Shell Programming

gaelim 2017. 6. 23. 12:05
반응형

shell programming

$ cat a.c

main(){

  for(;;);

} //needs compile, not a shell script


$ cat myprog

# this is the program myprog

date

ls -F 

//shell script


shell script and child process


$ color=blue

$ cat color1

echo You are now running program: color1

echo the value of the variable color is: $color

$ color1

You are now running program: color1

the value of the variable color is:

$ export color        //환경변수화
$ color1
You are now running program: color1
the value of the variable color is: blue


$ cat color4

echo There are $# command line arguments, 인수의 수 

echo They are $*

echo The first command line argument is $1

$ color4 red white blue black

There are 4 command line arguments

They are red white blue black

The first command line argument is red


shift command

Shifts all strings in * left n position
{
$ cat color5
orig_args=$*
echo There are $# command line arguments
echo They are $*
echo Shifting two arguments
shift 2
echo There are $# command line arguments
echo They are $*
echo Shifting two arguments
shift 2; final_args=$*
echo Original arguments are: $orig_args
echo Final arguments are: $final_args
$ color5 A B C D E F G
There are 7 command line arguments
They are A B C D E F G
Shifting two arguments
There are 5 command line arguments
They are C D E F G
Shifting two arguments
Original arguments are: A B C D E F G
Final arguments are: E F G
}

read command

{

$ cat color6

echo This program prompts for user input

echo "Please enter your favorite two colors -> \c"   // 자동 newline 방지 "\c"

read color_a color_b

echo The colors you entered are: $color_b $color_a


$ color6
This program prompts for user input
Please enter your favorite two colors -> black white
The colors you entered are: white black

$ color6
This program prompts for user input
Please enter your favorite two colors -> red blue green  //입력 2개를 초과
The colors you entered are: blue green red                   //color_a=red, color_b=blue green

}

echo and Escape character

\a : Alert character
\b : backspace [ text cursor]
\c  : suppresses the terminating newline
\f
\n  :new Line
\r
\t  :Tab character
\\ :backslash
\nnn

Additional Techiques

#!/usr/bin/"shell_name" define shell script


Debugging mode

$ sh -x color5 A B C D E F G

+ orig_args=A B C D E F G                          //code를read한 부분

+ echo There are 7 command line arguments

There are 7 command line arguments            //terminal에서 표시되는 부분

+ echo They are A B C D E F G

They are A B C D E F G

+ echo Shifting two arguments

Shifting two arguments

+ shift 2

+ echo There are 5 command line arguments

There are 5 command line arguments

+ echo They are C D E F G

They are C D E F G

+ echo Shifting two arguments

Shifting two arguments

+ shift 2

+ final_args=E F G

+ echo Original arguments are: A B C D E F G

Original arguments are: A B C D E F G

+ echo Final arguments are: E F G

Final arguments are: E F G


Exercise

echo welcome $(whoami)

date

who

echo "\n\n"

who am i | tr -s " " | cut -f1,2 -d" "

echo bye bye


========


grep user /etc/passwd |pr -t -6

echo enter your name:

read usr_name

grep $usr_name /etc/passwd | cut -f6 -d:| ls




반응형

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

HPUX - Shell Programming - Loops  (0) 2017.06.23
HPUX - Shell Programming - Branches  (0) 2017.06.23
HPUX - Process Control  (0) 2017.06.22
HPUX - Pipes  (0) 2017.06.22
HPUX - Input and Output Redirection  (0) 2017.06.22