네트워크/리눅스

리눅스 배쉬 셀, 스크립트 강의 3 - 링크드인, Kevin Dankwardt

윤맨1 2021. 11. 1. 09:45

*Title : The typeset and declare commands for variable

 

*Local Variables and Typeset

Variables can be created in a function that will not be available outside of it.

The typeset command makes variables local, can provide a type, or can provide formatting.

 

 

typeset i x

# x must be an integer

Arithmetic is faster for variables defined to be integers.

Let allows for convenient arithmetic:

let x++; let y=x**2; let x=x*3; let x*=5, ...

 

*The Declare Command

declare l uppercase values in the variable are converted to lowercase.

declare u lowercase values in the variable are converted to uppercase.

declare r variable is made read-only.

declare a MyArray will make MyArray an indexed array.

declare A MyArray2 will make MyArray2 an associated array.

 

명령어 실습)

cat typeset.sh

 

bash typeset.sh

clear

 

cat declare.sh

 

bash declare.sh

 

*Title : Looping with for/while sequences are reading input

 

*The Read Command

Read a line into a variable or multiple variable

read a b-reads first word into a and the rest into b.

Convinient for a while loop.

 

*While Loops

while

command list l

do

command list

done

# loops while command list l succeeds

 

 

*While Loops

while

((x<10))

do

echo loop $x; date >data.$x

((x=x+1))

done

 

 

*While Loops

while

read a b

do

echo a is $a b is $b

done <data_file

 

 

*While Loops

ls l | while

read a b c d

do

echo owner is $c

done

 

*For Loops

for <var> in <list>

do

command list

done

 

 

*For Loops

for I in dog cat elephant

do

echo I is $i

done

 

 

*For Loops

*seq 1 5

# prints 1 2 3 4 5

*for num in ‘seq 1 5’

# loops over 1 2 3 4 5

*generate sequences with {A..Z}, {1..10}, etc.

 

*For Loops

for d in $(<data_file)

# loops over space/new line

# separated data in data_file

 

for j in *.c

# making a list with file globbing

 

for f in $(find . -name *.c)

# using a command to generate a list

 

 

명령어 실습)

cat read .sh

 

cat data_file

./read.sh

 

cat while.sh

./while.sh

 

cat while2.sh

 

ls l /etc | head

 

nl for.sh

 

./for.sh

 

*Title : Defining Function and using return and exit

 

*Bash Functions

Give a name to a sequence of statements that will execute within the shell, npt in a new process.

 

function NAME {

function body . . .

}

 

Commonliy used to organize code in a shell program

 

function printhello {

echo Hello

}

 

printhello

# shell memorizes the function like it’s

# a new command

 

*The Return Command

Functions return when there are no more statements or when a return statement is executed.

 

function myfunc {

echo starting

return

echo this will not be executed

}

 

Functions produce results by writing output like commands do.

 

hvar=$(printhello)

 

*The Exit Command

exit <VALUE> sets the exit status, represented by $? to <VALUE>.

exit terminates the shell process

exit in a function terminates the whole shell program, not just the function.

 

 

명령어 실습)

function printhello {

>echo hello

>}

 

printhello

 

bash

printhello

 

exit

 

printhello

 

export f printhello

printhello

 

cat func.sh

./func.sh

 

cat func2.sh

./func2.sh

 

echo $?

 

*Title : Using file descriptors, file redirection, pipes, and here documents

 

*Redirection and Pipes

Processes normally have three files open:

0 => stdin, 1 => stdout, 2=> stderr

command > stdout-here 2> stderr-here < stdin-from-here

command &> file

# file gets stdout and stderr from command, file is created or overwritten

 

command | command2

# command2’s stdin comes from #command’s stdout

 

command 2>&1 | command2

# gets stdout and stderr from command

 

command |& command2

# alternative way for command2 to

# get command’s stdout and stderr

# as its stdin

 

command >> file

# appends stdout of command to

# end of file

 

command &>> file

# appends stdout and stderr of

# command to end of file

 

* Here Documents: <<

Here documents are a way to embed input for standard input inside of a script.

They avoid having to create a new file just to hold some input values.

 

sort <<END

cherry

banana

apple

orange

END

 

*Open and Close File Descriptors

exec N< myfile

# opens file descriptor N for

# reading from file myfile

 

exec N> myfile

# opens file descriptor N for

# writing to myfile

 

exec N<> myfile

# opens file descriptor N for

# reaing & writing with myfile

 

exec N>&- or exec N<&-

#close file descriptor N

 

Use lsof to see what file descriptors for a process are open

exec 7>/tmp/myfile7

lsof p $$

# $$ is shell’s PID

 

명령어 실습)

cat redirect.sh

 

find /etc name grub |& grep grub

 

cat redirect.sh

 

wc l grub.out

 

wc l errs.out

 

wc l both.out

 

echo hi >myfile

cat myfile

: hi 가 출력됨

 

echo bye >myfile

cat myfile

: bye 가 출력됨

 

echo cheese >>myfile

cat myfile

: cheese 가 밑으로 추가되어 출력됨

 

cat here.sh

./here.sh

 

cat openclose.sh

./openclose.sh |& more

 

*Title : Control-flow case statements and if-then-else with the test command

 

*The Case Statement

 

case expression in

pattern 1 )

command list ;;

pattern 2 )

command list ;;

...

esac

 

==================================

 

case $ans in

yes|YES|y|Y|y.x ) echo “will do!”;;

n*|N*) echo “will NOT do!”;;

*) echo “Oops!”;;

esac

 

==================================

 

*The If-Then-Else Statement

 

if

command list # last result is used

then

command list

[else

command list]

fi

 

===================================

 

if

grep q important myfile

then

echo myfile has important stuff

else

echo myfile does not have important stuff

fi

 

====================================

 

*Tests in Bash

The builtin test is used to check various conditions and set the return code with the result.

Loops and conditions often use the result of test.

An alternative to test is [[ ]] or (( )).

 

*Test Example

*

if

test f afile

*

if [[ -f bfile ]]

*

if

test $x gt 5

 

*Test Operator

[[ ex1 eq ex2 ]] [[ ex1 ne ex2 ]]

[[ ex1 lt ex2 ]] [[ ex1 gt ex2 ]]

[[ ex1 le ex2 ]] [[ ex1 ge ex2 ]]

 

===================================

 

((ex1 == ex2)) ((ex1 != ex2))

((ex1 < ex2)) ((ex1 > ex2))

((ex1 <= ex2)) ((ex1 >= ex2))

((ex1 && ex2)) ((ex1 || ex2))

 

===================================

 

*More tests

test d X

# success if X is a directory

test f X

# success if X is a regular file

test s X

# success if X exists and not empty

test x X

# success if you have x permission on X

test w X

# success if you have w permission on X

test r X

# success if you have r permission on X

 

명령어 실습)

cat case.sh

./case.sh

 

cat compare.sh

 

bash compare.sh

 

fi에 대한 설명

 

bash ifthen.sh