개발/Linux

Bash Shell Positional Parameters (위치 매개변수)

달리초이 2023. 1. 2. 10:44

 

- 위치 매개변수

- 입력하는 argument들은 $0, $1, $2와 같은 변수에 저장되어 script에 전달

- 10번째 argument 부터는 ${10} 처럼 중괄호를 사용

name of shell script : $0
first argument : $1
second argument : $2
Number of arguments in $#
List of all parameters in $@, $*

- Special shell variables

로그인 shell의 PID : $$
현재 작업 디렉토리 : $PWD
부모 프로세스 ID : $PPID
# passwd 파일을 현재 위치의 pass 파일로 복사하는 명령어
# $0 $1 $2
cp /etc/passwd ./pass
cat > parameter-exam1.sh
#!/bin/bash
#: Usage        : parameter-exam1.sh arg1 arg2 arg3
echo "The script name: $0"
echo "The first argument: $1"
echo "The second argument: $2"
echo "The number of arguments: $#"
echo "the list of arguments: $@"
echo "the list of arguments: $*"

chmod +x parameter-exam1.sh
cat > parameter-exam2.sh
#!/bin/bash
#: Usage        : parameter-exam2.sh directory_name
#: Author       : "daeil choi" <daeil.choi@classact.co.kr>
echo "[$1 Directory ]"
echo "========================================"
date +%Y-%m-%d
echo "========================================"
du -sh $1 2> /dev/null
echo

chmod +x parameter-exam2.sh

 

- 문제풀이

다음 조건에 맞는 shell script를 작성하시오.

첫번째 아규먼트로 입력한 디렉토리의 모든 파일 목록을 /tmp/날짜.txt 파일에 저장하기

cat > lab2.sh
#!/bin/bash
ls $1 > /tmp/$(date +%Y%m%d).txt

chmod +x lab2.sh

 

 

 

- 출처 : 따배셸

6. Positional Parameters

https://youtu.be/Y88jOdaBs4Q

728x90
반응형