๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/๐ŸงLinux

[๋ฆฌ๋ˆ…์Šค] Shell for๋ฌธ ์‚ฌ์šฉ๋ฒ• (๋ฐฐ์—ด ๋ฐฉ์‹,ํŒŒ์ผ ์ฝ์–ด์„œ ์ฒ˜๋ฆฌ)

by _viper_ 2019. 5. 17.
๋ฐ˜์‘ํ˜•

 


shell์—์„œ ์ œ์ผ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” for๋ฌธ์˜ 3๊ฐ€์ง€ ์‚ฌ์šฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

๐Ÿ‘† ๋ฐฐ์—ด๋กœ ์ €์žฅํ•œ ๊ฐ’ ์ฝ์–ด์˜ค๋Š” ๋ฐฉ์‹

  • 24์‹œ๊ฐ„ ๋‘ ์ž๋ฆฌ ์ˆ˜๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•
HH_ARR=("00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23")
for hh in "${HH_ARR[@]}"; do
	echo $hh
done

 

โœŒ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹

  • cat ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ text ํŒŒ์ผ ๋‚ด์šฉ์„ 1์ค„์”ฉ ์ฝ์–ด์˜ด
for list in `cat text`; do
	echo "$list"
done

 

๐Ÿ‘Œ index ๊ฐ’ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹ 

  • ์ˆซ์ž 2์ž๋ฆฌ ์ถœ๋ ฅ
for (( i=0; i<10; i++ )) do
       echo "$i"
       hh=`printf "%02d" "$i"`    ## ์ˆซ์ž 2์ž๋ฆฌ ์ถœ๋ ฅ ๋ฐฉ๋ฒ•
       echo "$hh"
done