shell script

shell script(for 사용)

ttttu_0324 2024. 1. 14. 21:16

[root@localhost mytest]# vi result.sh
#!/bin/bash

count=0

exec 2> /dev/null

for file in $@
do
        if [ -f $file ]; then
         wc -l  $file >> result2.txt
         let count=count+1     #let 하는 이유는 안하면 count 값만 나오는게 아니라 count+1이렇게 나옴
        elif [ -d $file ]; then
            for subfile in $file/*
            do
               wc -l $subfile >> result2.txt
               let count=count+1
            done
        fi
done

echo " Total count : $count"

실행 결과 값

for문을 사용

${val}     --> val값
$()    --> 괄호안에 있는 것을 실행 한다라는 뜻
$#   --> 갯수
$@  --> 배열전체 각각의 문자를 출력

!#/bin/bash

foe file in $@        --> (골뱅이)에 들어온 모든 파일을하나씩 읽으면서 
do
        echo file       --> 출력(echo)을 할 것이다
done


[root@localhost ~]# ./myread.sh foo.txt myread.sh
foo.txt
myread.sh



-------------
$file 이 파일 밑에 저장되어있는 파일들이 디렉토리 이면 밑에 있는 do 를 실행

exec는 리다이렉트 할 수 있는 기능이 있음
0 -> 표준입력
1-> 표준출력
2-> 표준 에러출력
exec 2> /dev/null     -->화면에 출력되는 오류 메시지는  /dev/null로 보낸다 # 이 밑에서부터는 오류메시지가 /dev/null로 보내진다

모두 몇개의 파일을 처리했는지 확인하기 위해서 count변수를 선언

'shell script' 카테고리의 다른 글

shell script(tar)  (0) 2024.01.15
shell script(case)  (0) 2024.01.15
shell script(condition 예제)  (0) 2024.01.14
shell script (read -p)  (0) 2024.01.14
shell script (if조건문, condition종류)  (1) 2024.01.13