Post

Shell Script

linuxsig.org/files/bash_scripting.html

Table 1: Built-in shell variables.

  
VariableUse
$#Stores the number of command-line arguments that were passed to the shell program.
$?Stores the exit value of the last command that was executed.
$0Stores the first word of the entered command (the name of the shell program).
$*Stores all the arguments that were entered on the command line ($1 $2 …).
”$@”Stores all the arguments that were entered on the command line, individually quoted (“$1” “$2” …).

변수 선언 할당 시 = 앞뒤로 공백이 없어야 함. 덧셈 뺄셈에서도 띄어쓰기가 상당히 중요하다. 쉘 스크립트에서는 공백에 유의.

shell script에서 유저 인풋 입력받기

1
2
3
4
5
6
7
8
echo -n "Enter password:"
stty -echo # 입력할 때 echoing 끄기
read pw
stty echo # echo 다시 켜준다
echo    # 입력 끝나면 줄바꿈


echo $pw

SSH passphrase 입력 자동으로 처리하기

1
2
3
ssh-add ~/ssh\_keys/mine
ssh-add -l # 목록보기
ssh-add -d ~/ssh\_keys/mine

https://unix.stackexchange.com/questions/90853/how-can-i-run-ssh-add-automatically-without-a-password-prompt

날짜 계산하기

1
date "--date=20220101 -d +10days" +%Y%m%d
예제 (이전 날짜 파일 읽어와서 작업 수행하고 그 다음 날짜 저장)
1
2
3
4
5
6
7
#!/bin/bash
saved_date_path=/home/user/files/tests/saved_date
start_date=`cat $saved_date_path`
end_date=`date "--date=$start_date -d +$(expr $1 - 1)days" +%Y%m%d`
python3 /home/user/scripts/datetest.py $start_date $end_date
next_date=`date "--date=$end_date -d +1days" +%Y%m%d`
echo next_date : $next_date
This post is licensed under CC BY 4.0 by the author.