[cheatsheet] bash

boychaboy·2023년 6월 9일
0

cheatsheet

목록 보기
1/8

스크립트 로그 남기기

bash script.sh 1> tmp.log 2>> tmp.log && tail -f tmp.log
  • stdout은 기록
  • stderror은 추가(append)

IF-ELSE

if [ ! -d "$HOME/file" ]; then
    echo "no file exits!"
fi
  • 특정 디렉토리에 폴더/파일이 존재하는지 확인
if [ $var == 'str' ]; then
    echo "string matches!"
fi
  • 변수와 문자열을 비교

PRINT

printf "\033[33mChecking Start...\033[0m\n"

  • 노란색으로 출력

printf vs echo

  • echo는 default로 newline을 보내고, printf는 보내지 않는다.
  • formatting 등등의 옵션이 printf에 더 많다.
  • reference

check os type

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # linux
elif [[ "$OSTYPE" == "darwin"* ]]; then
        # Mac OSX
else
        # Unknown.
fi

run another script in current script

sh another_scipt.sh

choice

read -p "Proceed (y/n [n])? " choice
case "$choice" in
    y|Y )
        echo "Yes"
        ;;
    n|N|"" )
        echo "No"
    esac
profile
no vim no code

0개의 댓글