Lab 6
Lab 6
در آزمایشات قبلی با شل لینوکس کار کردیم و دستوراتی را اجرا کردیم .در این آزمایش با ویژگیهای پیشرفته
شل و اسکریپت نویسی در شل آشنا خواهیم شد.
همچنین میتوان متغیرهای محیطی 1تعریف کرد که عالوه بر جلسه شل فعلی در تمامی پروسسهایی که از
این شل نیز مشتق میشوند وجود دارند .برای تعریف متغیر محیطی از دستور exportاستفاده میشود.
با استفاده از دستور envیا printenvمیتوان لیستی از متغیرهای محیطی را مشاهده کنید.
$ printenv
$ env
1
environment variable
1
:بعضی از متغیرهای محیطی پرکاربرد به شرح زیر است
#PATH: Specifies the directories in which the shell looks for executable
files.
$ echo $PATH
#HOME: Indicates the current user’s home directory.
$ echo $HOME
#USER: Contains the username of the current user.
$ echo $USER
#SHELL: Shows the path to the current user’s shell.
$ echo $SHELL
#PWD: Represents the current working directory.
$ echo $PWD
#LANG: Defines the language and locale settings.
$ echo $LANG
#EDITOR: Specifies the default text editor.
$ echo $EDITOR
#LOGNAME: Contains the login name of the user.
$ echo $LOGNAME
#OLDPWD: Stores the previous working directory.
$ echo $OLDPWD
#TERM: Defines the type of terminal to emulate when running the shell.
$ echo $TERM
2
هر دستوری که در بش اجرا میشود یک حالت خروجی 2دارد که صفر نشان میدهد دستور به درستی اجرا
شده است و عدد غیر صفر نشان وجود خطایی در اجرای دستور است .متغیر ویژه $? 3مقدار حالت خروجی
آخرین دستور اجرا شده را نگه میدارد.
$ ls path/to/nonexistfile
?$ echo $
2
در آزمایشات قبلی دیدیم که مقادیر * و ? در دستورات شل به اسم فایلهای موجود در مسیر اجرای دستور
بسط مییابند و سپس دستور اجرا میشود .عالوه بر اینها نماد های دیگری نیز در شل وجود دارند که بسط
مییابند و در ادامه چند نمونه میبینیم.
2
Exit status
3
Special variable
3
#Brace Expansion
#Generates arbitrary strings.
$ echo a{b,c,d}e
abe ace ade
$ echo {1..5}
12345
$ echo {a..e}
abcde
#Tilde Expansion
#Expands the tilde ~ to the home directory.
$ echo ~
/home/username
$ echo ~root
/root
#Parameter and Variable Expansion
#Expands the value of variables.
$ name="John"
$ echo $name
John
$ echo ${name}
John
4
#Command Substitution
#Replaces the command with its output.
$ echo $(date)
Current date and time
$ files=`ls`
$ $files
List of files in the current directory
#Arithmetic Expansion
#Performs arithmetic operations.
$ echo $((3 + 5))
8
$ echo $((2 * 3))
6
#Filename Expansion (Globbing)
#Matches filenames using patterns.
$ echo *.txt
List of all .txt files in the current directory
$ echo file{1..3}.txt
file1.txt file2.txt file3.txt
.اسکریپت بش فایلهایی هستند که حاوی دستوراتی هستند که توسط بش سطر به سطر اجرا میشود
شروع میشود که مفسر شلی که باید این فایل را اجرا#!/bin/sh فایلهای اسکریپت بش معموال با عبارت
.کند نشان میدهد
5
#!/bin/bash
# Ask for the user's name
"echo "Enter your name:
read name
# Greet the user
"echo "Hello, $name! Welcome to the Bash scripting world.
فایل باال را در script.shذخیره و با bash script.shمیتوانید این اسکریپت را اجرا کنید .تمام قابلیتهای
معمول در زبانهای برنامه نویسی در اسکریپت بش نیز موجود است که در ادامه با چند مثال با آنها آشنا
میشویم.
توابع در بش
برای تعریف یک تابع در بش معموال از کلمه functionاستفاده میکنیم .البته استفاده از functionاختیاری
است ولی برای خوانایی بهتر است استفاده شود.
#Using the function keyword:
{ )(function my_function
# Commands to be executed
"echo "Hello from my_function
}
my_function
برای دادن ورودی به یک تابع از پارامترهای مکانی 4استفاده میشود .این پارامترها به شکل $1و $2و یا
} ${1به کار برده میشوند.
4
Positional Parameters
6
#!/bin/bash
#!/bin/bash
# Define the function
# Define the function
add_numbers() {
greet() {
local sum=$(( $1 + $2 ))
echo "Hello, $1!"
echo "The sum is: $sum"
}
}
# Call the function with an argument
# Call the function with arguments
greet "Alice"
add_numbers 3 5
#!/bin/bash
function my_func() {
global_var="Hello, World!"
}
$ my_func
$ echo $global_var
Hello, World!
عبارتهای شرطی
یا نماد ][ در بش استفاده میشود و همچنینtest بودن یک عبارت از دستورFalse یاTrue برای بررسی
. را نیز به کاربرد-eq و-lt و-gt میتوان اپراتورهای مختلفی نظیر && و || و
#!/bin/bash
#Simple If Statement
#Checks if a condition is true.
if [ $1 -gt 10 ]; then
echo "The number is greater than 10."
fi
7
#!/bin/bash
#If-Else Statement
#Provides an alternative action if the condition is false.
if [ $1 -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
#!/bin/bash
#Nested If Statements
if [ $1 -gt 10 ]; then
if [ $1 -lt 20 ]; then
echo "The number is between 11 and 19."
else
echo "The number is 20 or greater."
fi
else
echo "The number is 10 or less."
fi
#!/bin/bash
a=5
b=10
if [ $a -lt $b ] && [ $b -gt 0 ]; then
echo "Both conditions are true."
fi
if [ $a -gt $b ] || [ $b -gt 0 ]; then
echo "At least one condition is true."
fi
8
#!/bin/bash
#Using Logical Operators
if [ $1 -gt 10 ] && [ $1 -lt 20 ]; then
echo "The number is between 11 and 19."
else
echo "The number is outside the range of 11 to 19."
fi
#!/bin/bash
#If-Elif-Else Statement
if [ $1 -gt 10 ]; then
echo "The number is greater than 10."
elif [ $1 -eq 10 ]; then
echo "The number is exactly 10."
else
echo "The number is less than 10."
fi
9
حلقهها
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
#!/bin/bash
counter=1
while [ $counter -le 5 ]
do
echo "Counter: $counter"
((counter++))
done
10
#!/bin/bash
counter=1
] until [ $counter -gt 5
do
"echo "Counter: $counter
))((counter++
done
#!/bin/bash
))for ((i = 0 ; i < 5 ; i++
do
"echo "Number: $i
done
توجه :در اسکریپت نویسی در Bashرعایت فاصلهگذاری و یا عدم فاصله گاها مهم است.
فعالیتها:
11
)8هر زمانی که یک شل را باز میکنیم در ابتدای کار بعضی فایلهای تنظیمات توسط شل اجرا میشوند .از این
طریق میتوان تغییراتی را که می خواهیم دائمی باشند را بر روی شل تنظیم کنیم .یکی از فایل ها .bashrc
است .این فایل را باز کرده و چند متغیر محیطی را در انتهای آن تعریف کنید.
12