Deadmarshal

لینوکس و برنامه نویسی

Deadmarshal

لینوکس و برنامه نویسی

1. اسکریپت بش شل و اولین برنامه (Hello World)
اول باید بفهمیم مفسر بش در کجا قرار گرفته است. خطوط زیر را در خط فرمان تایپ نمایید:
$ which bash


ویرایش گر متنی مورد علاقه خود را باز نمایید و فایلی با نام hello_world.sh بسازید. خط های زیر را در این فایل بنویسید:

نکته: تمام اسکریپت های بش این آموزش با shebang (!#) آغاز می شوند که به عنوان کامنت خوانده نمی شود. اولین خط همچنین می تواند جایی برای معرفی مکان shell شما باشد که در اینجا bin/bash/ است.

این اولین اسکریپت بش ما است:
#!/bin/bash
# declare STRING variable
STRING="Hello World"
# print variable on a screen
echo $STRING
به مسیری که فایلتان قرار دارد رفته و آن را به صورت فایل اجرایی در آورید:
$ chmod +x hello_world.sh

حال آماده هستید تا اولین اسکریپت بش خود را اجرا نمایید:
./hello_world.sh


2. پشتیبان گیری از اسکریپت بش
#!/bin/bash
tar -czf myhome_directory.tar.gz /home/linuxconfig

3.متغیر ها
در این مثال یک متغیر می سازیم و آن را با دستور echo بر روی صفحه نمایش می دهیم. 
#!/bin/bash
STRING="Hello World!!!"
echo $STRING

اسکریپت پشتیبان گیری شده شما و متغیر ها:
#!/bin/bash
OF=myhome_directory_$(date +%y%m%d).tar.gz
tar -czf $OF /home/linuxconfig

3.1.متغیر های محلی در مقابل سراسری
#!/bin/bash
#Define bash global variable
#This variable is global and can be used anywhere in this bash script
VAR="global variable"
function bash {
#Define bash local variable
#This variable is local to bash function only
local VAR="local variable"
echo $VAR
}
echo $VAR
bash
#Note that the global variable did not change
#"local" is bash reserved word
echo $VAR

4.رد کردن آرگومان ها (arguments) در اسکریپت بش

#!/bin/bash
#Use predefined variables to access passed arguments
#echo arguments to the shell
echo $1 $2 $3 ' -> echo $1 $2 $3'

#We can also store arguments from bash command line in special array
args=("$@")
#echo arguments to the shell
echo ${args[0]} ${args[1]} ${args[2]} ' --> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'

#Use $@ to print all the arguments at once
echo $@ ' -> echo $@'

#Use $# variable to print out
#Number of arguments passed to the bash script
echo number of arguments passed: $# ' -> echo number of acguments passed: $#'


/arguments.sh Bash Scripting Tutorial

5.اجرای فرمان های شل با بش
#!/bin/bash
#Use backticks " ` ` " to execute shell command
echo `uname -o`
#Executing shell command without backticks
echo uname -o

6.خواندن ورودی کاربر
#!/bin/bash
echo -e "Hi, please type the word: \c "
read word
echo "The word you entered is: $word"
echo -e "Can you please type two words? "
read word1 word2
echo "Here is your input: \"$word1\" \"$word2\" "
echo -e "How do you feel about bash scripting? "
# read command now stores a reply into the default build-in variable $REPLY
read
echo "You said $REPLY, I'm glad to hear that! "
echo -e "What are your favorite colours? "
# -a makes read command read into an array
read -a colours
echo "My favorite colours are also ${colours[0]}, ${colours[1]}, and ${colours[2]}:-)"

7. فرمان فرمان تله (trap)
#!/bin/bash
#Bash trap command
trap bashtrap INT
#Bash clear screen command
clear;
#Bash trap command is executed when CTRL+C is pressed:
#Bash prints message => executing bash trap subrutine!
bashtrap()
{
echo "CTRL+C detected !....executing bash trap !"
}
#for loop from 1/10 to 10/10
for a in `seq 1 10`; do
echo "$a/10 to Exit."
sleep1;
done
echo "Exit Bash Trap Example!!!"

8. آرایه ها
8.1 تعیین آرایه های بش ساده