bash

اسکریپت نویسی bash

echo 'hello dudes!'

githubwebsite

حلقه‌ها

دستور seq

  • دستور seq دنباله ای از اعداد را ایجاد می‌کند. مثلا برای اعداد ۱ تا ۵ می‌نویسیم
seq 1 5
1
2
3
4
5
  • این‌جوری می‌شه گام هم براش تعریف کرد که دوتا دو تا بره جلو مثلاً
seq 1 2 5
1
3
5

حلقهٔ for

for i in $( seq 1 5 )
do
  echo $i
done
1
2
3
4
5

تفاوت while و until

حلقهٔ while تا زمانی که شرط «درست باشه» ادامه پیدا می‌کنه

حلقهٔ until تا وقتی که شرط «درست بشه» ادامه پیدا می‌کنه،

  • یعنی قراره false باشه همیشه وقتی true شد اون موقع تموم می‌شه

تفاوت while و until

COUNTER=0
while [ $COUNTER -lt 5 ]; do
    echo counter is $COUNTER
    let COUNTER+=1 
done
counter is 0
counter is 1
counter is 2
counter is 3
counter is 4

تفاوت while و until

COUNTER=10
until [ $COUNTER -lt 5 ]; do
    echo counter is $COUNTER
    let COUNTER-=1 
done
counter is 10
counter is 9
counter is 8
counter is 7
counter is 6
counter is 5

دستور‌های خط فرمان لینوکس

tr

  • فرض کنید فایل sample.txt با متن داخلی زیر را در اختیار داریم
cat ./sample.txt
April is the cruelest month, breeding
lilacs out of the dead land, mixing
memory and desire, stirring
dull roots with spring rain

tr

  • با دستور tr می‌تونیم یه سری پترن‌ها را به پترن‌های دیگه ترجمه کنیم
cat sample.txt |  tr aeoui AEOUI
AprIl Is thE crUElEst mOnth, brEEdIng
lIlAcs OUt Of thE dEAd lAnd, mIxIng
mEmOry And dEsIrE, stIrrIng
dUll rOOts wIth sprIng rAIn
  • اینجا، هر حرف صدا داری که با حروف کوچیک نوشته شده باشه را به حروف بزرگ تبدیل می‌کنیم

class: inverse, farsi

tr

  • می‌تونیم بدون cat هم از tr استفاده کنیم. اینجوری خروجی رو توی stdout می‌بینیم
tr aeoui AEOUI  < sample.txt
AprIl Is thE crUElEst mOnth, brEEdIng
lIlAcs OUt Of thE dEAd lAnd, mIxIng
mEmOry And dEsIrE, stIrrIng
dUll rOOts wIth sprIng rAIn

tr

  • یا همون دستور قبلی رو استفاده کنیم ولی خروجی رو توی یه فایل بنویسیم
tr aeoui AEOUI  < sample.txt > output.txt
cat output.txt
AprIl Is thE crUElEst mOnth, brEEdIng
lIlAcs OUt Of thE dEAd lAnd, mIxIng
mEmOry And dEsIrE, stIrrIng
dUll rOOts wIth sprIng rAIn