Linux Bash Script
今天我想分享如何用linux shell script 語法。
我們需要加這個在script裡面,然後檔案改成.sh
1 | #! /bin/bash |
- How to execute script
chmod 777 myscript.sh
./myscript.sh
1. variable
define variable
1 | text1=Hello |
output:
Hello CC
read and echo variable
read variable
read a
read vairable without skip new line:
read -p “Your Options: “ option
echo without new line
echo -n “message”
example:
1 | echo "======================IPERF===================" |
expression
var=$((3+9))
echo $var
output:
12
- example
1
2
3
4
5
6
7echo "Enter a numner"
read a
echo "Enter a numner"
read b
echo $sum
echo $a $b
sum=$((a+b))
2. condition
if elif else
Syntax
1 | if [[ condition ]] |
check file empty
if [[ -z "$tftp_dir" ]]
example
1 | read x |
3. Loop
for loop
loop number
1 | for i in {1..5} |
Looping with strings
1 | for X in hello world |
while loop
adding 1 ~100
1 | #!/bin/bash |
get arguments for scripts
1 | #!/bin/bash |
./filename value1 value2 value3