وبلاگ رسانگار
با ما حرفه ای باشید

سرور مجازی NVMe

30 نمونه دستور اسکریپت Bash – یادگیری بش اسکریپت با نمونه

0 21,380
زمان لازم برای مطالعه: 12 دقیقه

اسکریپت نویسی بش (Bash) می‎تواند برای موارد زیادی از جمله اجرای دستور Shell ، اجرای چندین دستور با هم ، سفارشی سازی وظایف مدیریتی ، اجرای تسک ها بصورت اتوماتیک و … استفاده شود به همین دلیل دانش برنامه نویسی Bash و پایه آن برای هر کاربر سیستم عامل لینوکس مهم است ،
این مقاله به شما کمک میکند تا ایده پایه ای برنامه نویسی بش آشنا شوید ،بسیاری از عملیات رایج در اسکریپت نویسی Bash با مثتالهای ساده را توصیح خواهیم داد
سرفصلهای زیر را در این مقاله مرور خواهیم کرد.

ایجاد و اجرای اولین برنامه BASH – Hello World

شما میتوانید اسکریپت Bash را با اجرای یک فایل Bash اجرا کنید ، دستور زیر را ر ترمینال سیستم خود اجرا کنید تا یک خروجی بسیار ساده دریافت کنید ، خروجی دستور زیر “Hello World” خواهد بود

$ echo "Hello World"

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

ادیتور محبوب خود را باز کنید ، ما در اینجا از nano استفاده می‎کنیم ، اسم فایل ا First.sh میگذاریم

nano First.sh

دستورات زیر را به فایل اضافه کنید

#!/bin/bash
echo "Hello World"

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

اجرای فایل اسکریپت بش از دو طریق امکان پذیر است ، یی با اجرای دستور و دیگری ست کردن دسترسی اجرا یا execute روی فایل bash و اجرای فایل است ، هر دو روش را اینجا نشان خواهم داد

$ bash First.sh

یا

$ chmod a+x First.sh
$ ./First.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

استفاده از دستور echo

شما میتوانید دستور echo را با گزینه های مختلفی اجرا کنید ، برخی گزینه های کاربری آن را در در مثال زیر ذکر کردیم ، وقتی از “echo ” بدون هیچ آپشنی استفاده کنید یک خط جدید بصورت پیشفرض اضافه می‎شود ، گزینه ‘-n’ برای نمایش هر متنی بدون یک خط جدید و -e برای حذف پک اسلش یا \ از کاراکترهای خروجی استفاده می شود ، یک فایل bash جدید به اسم echo_example.sh ایجاد کنید و اسکریپت زیر را در آن اضافه کنید

#!/bin/bash
echo "Printing text with newline"
echo -n "Printing text without newline"
echo -e "\nRemoving \t backslash \t characters\n"

فایل را با دستور bash اجرا کنید

$ bash echo_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

comment کردن کد در Bash Script

علامت ‘#’ برای کامنت کردن یک اسکریپت استفاده می شوأ ، یک فایل جدید با نام comment_script.sh ایجاد کنید و اسکریپت زیر را به آن اضافه کنید ، که حاوی یک خط کامنت شده است

#!/bin/bash
# Add two numeric value
((sum=25+35))
#Print the result
echo $sum

 

فایل را با دستور bash اجرا کنید

$ bash comment_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

کامنت کردن چند خط در Bash Scripting

شما میتوانید چندین خط از کد را در بش اسکریپتینگ کامنت کنید ،راه ساده ای که در مثال زیر به آن اشاره شده است ، یک فایل جدید با نان multiline_comment.sh ایجاد کنید و اسکریپت زیر را در آن کپی کنید، اسکریپت زیر برای محاسبه مربع عدد 5 می باشد

You can use multi line comment in bash in various ways. A simple way is shown in the following example. Create a new bash named, ‘multiline-comment.sh’ and add the following script. Here, ‘:’ and “ ’ ” symbols are used to add multiline comment in bash script. This following script will calculate the square of 5.

#!/bin/bash
: '
The following script calculates
the square value of the number, 5.
'
((area=5*5))
echo $area
فایل را دستور bash اجرا کنید
 
$ bash multiline-comment.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

برای اطلاعات بیشتر در خصوص کامنت کردن کد در bash به لینک زیر مراجعه کنید

https://linuxhint.com/bash_comments/

بازگشت به فهرست

استفاده از حلقه While در Bash Script

یک فایل بش با اسم while_example.sh ایجاد کنید تا ببنید که حلقه while به چه شکل کار می کند ،  در این مثال حلقه  While 5 بار تکرار می شود ، مقدار متغیر count به ازای هر گام به اندازه 1 واخد افزایش پیدا می کند و بعد از رسیدن مقدار متغیر count به 5 حلقه پایان می یابد

#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
 

فایل را با دستو زیر اجرا کنید

$ bash while_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

میتوانید از طریق لینک زیر در مورد دستور while در اسکریپت نویسی Bash بیشتر یاد بگیرید .

https://linuxhint.com/bash-while-loop-examples/

بازگشت به فهرست

استفاده از حلقه FOR در Bash Script

اصول تعریف حلقه در مثال زیر آورده شده است ، یک فایل به اسم for_examamples.sh ایجاد کنید و اسکریپت زیر را به آن اضافه کنید ، حلقه for زیر برای 10 بار تکرار شده و همه مقادیر متغیر را نمایش میدهد ، counter در ی یک خط خواهد بود

#!/bin/bash
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n"

فایل را با دستوز زیر اجرا کنید

$ bash for_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

میتوانید از دستور حلقه for برای کاربردهای مختلف به روشهای مختلف در اسکریپت خود استفاده کنید ، لینک زیر را برای اطلاعات بیشتر درخصوص حلقه for بررسی کنید

https://linuxhint.com/bash-for-loop-examples/

بازگشت به فهرست

گرفتن ورودی از کاربر در Bash Script

دستور read برای گرفتن ورودی از کاربر در bash استفاده می شود ، یک فایل به اسم user_input.sh ایجاد کنید و اسکریپت زیر را برای گرفتن ورودی از کاربر به آن اضافه کنید ، در اینجا یک رشته از کاربر گرفته می شود و مقدار آن با ترکیب آن با رشته های دیگر نمایش میدهد.

#!/bin/bash
echo "Enter Your Name"
read name
echo "Welcome $name to Rasanegar"

فایل را با دستور زیر اجرا کنید

$ bash user_input.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

میتوانید از طریق لینک زیر اطلاعات بیشتری در خصوص دستور read دریافت کنید

https://linuxhint.com/bash-script-user-input/

بازگشت به فهرست

استفاده از شرط if در Bash Script

شما میتوانید از شرط if در یک یا چند شرط استفاده کنید ، شروع شرط با if و پایان آن با fi می باشد ، یک فایل به اسم simple_if.sh ایجاد کنید و اسکریپت زیر را برای شناخت عملکرد if در اسکریپت نویسی بش قرار دهید ، تعریف شرط زیر 10 را به یک متغیر به اسم n تخصیص میدهد و اگر مقدار $n کمتر از 1- باشد اعلام میکند که این یک عدد 1 رقمی است ، در غیر این صورت خروجی آن این خواهد بود که این یک عدد 2 رقمی است ، برای مقایسه از -lt در اینجا استفاده شده است ، همچنین می‎توانید از -eq بر ای “برابر است با” و از -ne برای نا برابر است با و از   -gt برای “بزرگتر از ” استفاده کنید.

#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi

فایل را با دستور زیر اجرا کنید

$ bash simple_if.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

پیشنهاد می‌کنیم بخوانید:  بهترین راه برای یادگیری اسکریپت Bash چیست؟

استفاده از تعریف IF و منطق AND در Bash Scripting

انواع مختلف شرطهای منطقی می‎تواند با تعریف شرط if با 1 و چند شرط استفاده شود ، اینکه چطور چند حالت محنلف به همراه منطق AND را تعریف کنید در  نمونه اسکریپت زیر آورده شده است. ” && ” برای اعمال منطق AND در تعریف IF استفاده می شود ، یک فایل به نام if_with_AND.sh برای بررسی خروجی کد زیر ایجاد کنید ، در این نمونه کد ، مقادیر متغیرهای  username و password  از ورودی کاربر گرفته می شود و با مقدار admin و مقدار secret مقایسه می شود ، اگر هر دو یکی باشند خروجی valid user نمایش داده می شود در غیر این صورت invalid user نمایش داهد می شود

!/bin/bash

echo "Enter username"
read username
echo "Enter password"
read password

if [[ ( $username == "admin" && $password == "secret" ) ]]; then
echo "valid user"
else
echo "invalid user"
fi

فایل را با دستور زیر اجرا کنید

$ bash if_with_AND.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

استفاده از تعریف IF و منطق OR در Bash Scripting

” || ” برای تعریف منطق OR در شرط if استفاده می شود ، یک فایل به اسم if_wiht_OR.sh ایجاد و کد زیر را در آن کپی کنید ، مقدار n از ورودی کاربر گرفته می شود ، اگر مقدار برابر با 15 یا 45 باشد باشد خروجی برابر با “You won the game”,در غیر این صورت You lost the game نمایش داده می شود

#!/bin/bash

echo "Enter any number"
read n

if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi

با دستور زیر آن را اجرا کنید

$ bash if_with_OR.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

استفاده از تعریف شرط IF در Shell Scripting

 

The use of else if condition is little different in bash than other programming language. ‘elif’ is used to define else if condition in bash. Create a file named, ‘elseif_example.sh’ and add the following script to check how else if is defined in bash script.

#!/bin/bash

echo "Enter your lucky number"
read n

if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize"

else
echo "Sorry, try for the next time"
fi

با دستور زیر آن را اجرا کنید

$ bash elseif_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

استفاده از تعریف Case

شرط case به عنوان معادل شرط if-elseif-else استفاده می شود ، برای شروع و پایان بلوک این شرط از case و esac استفاده می کنیم ، یک فایل جدید به نام case_example.sh  ایجاد می کنیم و متن زیر را در آن قرار می‏‎دهیم ، خروجی کد زیر مانند دستور قبلی خواهد بود .

#!/bin/bash

echo "Enter your lucky number"
read n
case $n in
101)
echo echo "You got 1st prize" ;;
510)
echo "You got 2nd prize" ;;
999)
echo "You got 3rd prize" ;;
*)
echo "Sorry, try for the next time" ;;
esac

با دستور زیر آن را اجرا کنید

$ bash case_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

گرفتن آرگومان از خط فرمان

بش اسکریپت میتواند آرگومان ها را مانند دیگر زیان های برامه نویسی از خط فرمان بخواند ، به عنوان مثال $1 و $2 متغیرهایی هستند که برای گرفتن ورودی خطهای آرگومان اولی و دومی استفاده می کنیم ، یک فایل با نام command_line.sh ایجاد کنید و اسکریپت زیر را در آن اضافه کنید ، دو آرگومان توسط اسکریپت زیر خوانده می شود و مجموع اعداد آرگومان را در خروجی نمایش میدهد

Bash script can read input from command line argument like other programming language. For example, $1 and $2 variable are used to read first and second command line arguments. Create a file named “command_line.sh” and add the following script. Two argument values read by the following script and prints the total number of arguments and the argument values as output.

#!/bin/bash
echo "Total arguments : $#"
echo "1st Argument = $1"
echo "2nd argument = $2"

با دستور زیر آن را اجرا کنید

$ bash command_line.sh Linux Hint

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

می‎توانید در لینک زیر اطلاعات بیشتری در خصوص آروگمان ها در بش اسکریپتینگ بدست بیاورید

https://linuxhint.com/command_line_arguments_bash_script/

بازگشت به فهرست

گرفتن آرگومان از خط فرمان با استفاده از نام ها

میخواهید بدانید جطور می‎توانید آرگومان های خط فرمان را به همراه نام آنها بخوانید ، اسکریپت زیر این کار را برای شما انجام میدهد یک فایل به اسم command_line_names.sh ایجاد و کنید و کد زیر را در آن قرار دهید ،در اینجا دو آرگومان X و Y توسط این اسکریپت خواندهمی شود و و مجموع X , Y روی صفحه پرینت می‎شود

#!/bin/bash
for arg in "$@"
do
index=$(echo $arg | cut -f1 -d=)
val=$(echo $arg | cut -f2 -d=)
case $index in
X) x=$val;;

Y) y=$val;;

*)
esac
done
((result=x+y))
echo "X+Y=$result"

فایل را با دستور bash اجرا کنید و دو آرگومان را در خط فرمان به آن پاس بدهید

$ bash command_line_names X=45 Y=30

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

ترکیب متغیرهای رشته‎ای

شما به سادگی می‎توانید دو رشته را که در متیغر ذخیره شده است را در Bash ادغام کنید ، یک فایل به اسم string -combine.sh بسیازید و اسکریپت زیر را در آن اضافه کنید ، در کد زیر می‎بینید که چطور دو رشته با قرار دادن دو متغیر کنار هم با اپراتور + با هم ادغام می‎شوند


#!/bin/bash

string1="Linux"
string2="Hint"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good tutorial blog site"
echo $string3

فایل را با دستور bash اجرا کنید

$ bash string_combine.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

دریافت زیررشته از یک رشته:

مانند دیگر زیان ها برنامه نویسی ، بش هم هیچ تابع داخلی برای جداکردن مقدار از داده رشته ندارد ، اما شما میتوانید عملکرد substring را با روش دیگری انجام دهید ، این روش در اسکریپت زیر نمایش داده شده است ، برای تست این اسکریپت یک فایل به اسن substring_example.sh ایجاد کنید و مد زیر را در آن قرار دهید ، در کد زیر 6 نشان گر نقطه شروع جایی است که substring آغاز می شود و 5 نشانگر طول آن زیر رشته است.

#!/bin/bash
Str="Learn Linux from LinuxHint"
subStr=${Str:6:5}
echo $subStr

فایل را با دستور bash اجرا کنید

$ bash substring_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

جمع دو عدد:

شما میتوانید عملیات حسابی را در اسکریپت نویسی bash به چند روش انجام دهید ، اینکه چطور می‎توانید دو عدد صحیح را در در بش با دو پرانتز (()) جمع کنید در اسکریپت زیر نمایش داده شده است ، یک فایل به اسم add_numbers.sh ایجاد کنید و کد زیر را به آن اضافه کنید ، دو عدد صحیح از کاربر گرفته می‎شود و نتیجه جمع نشان داده می‎شود.

#!/bin/bash
echo "Enter first number"
read x
echo "Enter second number"
read y
(( sum=x+y ))
echo "The result of addition=$sum"

پیشنهاد می‌کنیم بخوانید:  چگونه از یک Bash while Loop خارج شویم؟

فایل را با دستور bash اجرا کنید

$ bash add_numbers.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

در لینک زیر میتوانید در خصوص عملیات حسابی و عملگرهای آن در بش اسکریپت بیشتر آشنا شوید

https://linuxhint.com/bash_arithmetic_operations/

بازگشت به فهرست

ایجاد تابع

در کد نمونه زیر یک تابع ساده ایجاد و آنر ا فراخوانی می‎کنیم ، یک فایل به اسم sunction_example.sh ایجاد کنید و کد زیر را به آن اضافه کنید ، شما می توانید تابع را بدون استفاده از پرانتر با استفاده از اسم فراخوانی کنید ‎

#!/bin/bash
function F1()
{
echo 'I like bash programming'
}

F1

فایل را با دستور bash اجرا کنید

$ bash function_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

ایجاد تابع با پارامترها

در اسکریپت بَش نمیتوانید پارامترهای تابع یا آرگومان ها را در زمان تعریف تابع بشناسانید ، اما می‎توانید از پارمترها با استفاده از متغیرهای دیگر در تابع استفاده کنید ، اگر دو مقدار دز زمان فراخوانی تابع فراخوانی شود ، پس متغیرهای $1 و $2 برای خواندن مقادیر استفاده می شود ، یک فایل به اسم function_parameters.sh ایجاد کنید و کد زیر را در آن اضافه کنید ، در اینجا تابع Rectangle_Ara محیط یک مستطیل را بر اساس مقادیر پارامترها محاسبه می کند

#!/bin/bash

Rectangle_Area() {
area=$(($1 * $2))
echo "Area is : $area"
}

Rectangle_Area 10 20

فایل را با دستور bash اجرا کنید

$ bash function_parameter.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

ارجاع Return Value در Funtion

فانکشنهای بش میتوانند مقادیر عددی و رشته ای را پاس بدهند ، در مثل زیر اینکه چطور میتوانید یک مقدار رشته ای را از تابع پاس بدهید نمایش داده شده است ، یک فایل به اسم function_return.sh بسازید و اسکریپت زیر را به آن اضافه کنید ، تابع greeting() مقدار یک مقدار رشته ای به متغیر val برمیگرداند و بعد در خروجی

Bash function can pass both numeric and string values. How you can pass a string value from the function is shown in the following example. Create a file named, ‘function_return.sh’ and add the following code. The function, greeting() returns a string value into the variable, val which prints later by combining with other string.

#!/bin/bash
function greeting() {

str=”Hello, $name”
echo $str

}

echo “Enter your name”
read name

val=$(greeting)
echo “Return value of the function is $val”

Run the file with bash command.

$ bash function_return.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

You can check the following link to know more about the use of bash function.

https://linuxhint.com/return-string-bash-functions/

بازگشت به فهرست

Make Directory:

Bash uses ‘mkdir’ command to create a new directory. Create a file named ‘make_directory.sh’ and add the following code to take a new directory name from the user. If the directory name is not exist in the current location then it will create the directory, otherwise the program will display error.

#!/bin/bash
echo “Enter directory name”
read newdir
`mkdir $newdir`

Run the file with bash command.

$ bash make_directory.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

Make directory by checking existence:

If you want to check the existence of directory in the current location before executing the ‘mkdir’ command then you can use the following code. ‘-d’ option is used to test a particular directory is exist or not. Create a file named, ‘directory_exist.sh’ and add the following code to create a directory by checking existence.

#!/bin/bash
echo “Enter directory name”
read ndir
if [ -d “$ndir” ]
then
echo “Directory exist”
else
`mkdir $ndir`
echo “Directory created”
fi

Run the file with bash command.

$ bash directory_exist.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

You can check the following link to know more about directory creation.

https://linuxhint.com/bash_mkdir_not_existent_path/

بازگشت به فهرست

Read a File:

You can read any file line by line in bash by using loop. Create a file named, ‘read_file.sh’ and add the following code to read an existing file named, ‘book.txt’.

#!/bin/bash
file=’book.txt’
while read line; do
echo $line
done < $file

Run the file with bash command.

$ bash read_file.sh

Run the following command to check the original content of ‘book.txt’ file.

$ cat book.txt

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

You can check the following link to know the different ways to read file.

https://linuxhint.com/read_file_line_by_line_bash/

بازگشت به فهرست

Delete a File:

‘rm’ command is used in bash to remove any file. Create a file named ‘delete_file.sh’ with the following code to take the filename from the user and remove. Here, ‘-i’ option is used to get permission from the user before removing the file.

#!/bin/bash
echo “Enter filename to remove”
read fn
rm -i $fn

Run the file with bash command.

$ ls
$ bash delete_file.sh
$ ls

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

Append to File:

New data can be added into any existing file by using ‘>>’ operator in bash. Create a file named ‘append_file.sh’ and add the following code to add new content at the end of the file. Here, ‘Learning Laravel 5’ will be added at the of ‘book.txt’ file after executing the script.

#!/bin/bash

echo “Before appending the file”
cat book.txt

echo “Learning Laravel 5”>> book.txt
echo “After appending the file”
cat book.txt

Run the file with bash command.

$ bash append_file.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

Test if File Exist:

You can check the existence of file in bash by using ‘-e’ or ‘-f’ option. ‘-f’ option is used in the following script to test the file existence. Create a file named, ‘file_exist.sh’ and add the following code. Here, the filename will pass from the command line.

#!/bin/bash
filename=$1
if [ -f “$filename” ]; then
echo “File exists”
else
echo “File does not exist”
fi

Run the following commands to check the existence of the file. Here, book.txt file exists and book2.txt is not exist in the current location.

$ ls
$ bash file_exist.sh book.txt
$ bash file_exist.sh book2.txt

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

Send Email:

You can send email by using ‘mail’ or ‘sendmail’ command. Before using these commands, you have to install all necessary packages. Create a file named, ‘mail_example.sh’ and add the following code to send the email.

#!/bin/bash
Recipient=”admin@example.com”
Subject=”Greeting”
Message=”Welcome to our site”
`mail -s $Subject $Recipient <<< $Message`

Run the file with bash command.

$ bash mail_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

Get Parse Current Date:

You can get the current system date and time value using `date` command. Every part of date and time value can be parsed using ‘Y’, ‘m’, ‘d’, ‘H’, ‘M’ and ‘S’. Create a new file named ‘date_parse.sh’ and add the following code to separate day, month, year, hour, minute and second values.

#!/bin/bash
Year=`date +%Y`
Month=`date +%m`
Day=`date +%d`
Hour=`date +%H`
Minute=`date +%M`
Second=`date +%S`
echo `date`
echo “Current Date is: $Day-$Month-$Year”
echo “Current Time is: $Hour:$Minute:$Second”

Run the file with bash command.

$ bash date_parse.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

بازگشت به فهرست

Wait Command:

wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. If no process id or job id is given with wait command then it will wait for all current child processes to complete and returns exit status. Create a file named ‘wait_example.sh’ and add the following script.

#!/bin/bash
echo “Wait command” &
process_id=$!
wait $process_id
echo “Exited with status $?”

Run the file with bash command.

$ bash wait_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

You can check the following link to know more about wait command.

بازگشت به فهرست

Sleep Command:

When you want to pause the execution of any command for specific period of time then you can use sleep command. You can set the delay amount by seconds (s), minutes (m), hours (h) and days (d). Create a file named ‘sleep_example.sh’ and add the following script. This script will wait for 5 seconds after running.

#!/bin/bash

echo “Wait for 5 seconds”
sleep 5
echo “Completed”

Run the file with bash command.

$ bash sleep_example.sh

30 نمونه دستور اسکریپت Bash - یادگیری بش اسکریپت با نمونه

You can check the following link to know more about sleep command.

https://linuxhint.com/sleep_command_linux/

بازگشت به فهرست

Hope, after reading this article you have got a basic concept on bash scripting language and you will be able to apply them based on your requirements.

 

3.9/5 (89 رای)
دیدگاه شما در خصوص مطلب چیست ؟

آدرس ایمیل شما منتشر نخواهد شد.

لطفا دیدگاه خود را با احترام به دیدگاه های دیگران و با توجه به محتوای مطلب درج کنید