./script.sh arg1 arg2
arg1 → $1arg2 → $2$1 ~ $9 可用來取得前 9 個參數。腳本 4_args.sh:
#!/bin/bash
echo "Value of zero: $0" # 腳本名稱
echo "Value of one: $1" # 第一個參數
echo "Value of two: $2" # 第二個參數
echo "Value of three: $3" # 第三個參數
執行方式:
./4_args.sh Linux DevOps Test
輸出:
Value of zero: ./4_args.sh
Value of one: Linux
Value of two: DevOps
Value of three: Test
目標:讓 script 可重複使用,不固定 URL 或 ARTIFACT。
5_args_websetup.sh#!/bin/bash
# Variable Declaration
PACKAGE="httpd wget unzip"
SVC="httpd"
#URL='<https://www.tooplate.com/zip-templates/2098_health.zip>'
#ART_NAME='2098_health'
TEMPDIR="/tmp/webfiles"
# Installing Dependencies
echo "########################################"
echo "Installing packages."
echo "########################################"
sudo yum install $PACKAGE -y > /dev/null
echo
# Start & Enable Service
echo "########################################"
echo "Start & Enable HTTPD Service"
echo "########################################"
sudo systemctl start $SVC
sudo systemctl enable $SVC
echo
# Creating Temp Directory
echo "########################################"
echo "Starting Artifact Deployment"
echo "########################################"
mkdir -p $TEMPDIR
cd $TEMPDIR
echo
wget $1 > /dev/null
unzip $2.zip > /dev/null
sudo cp -r $2/* /var/www/html/
echo
# Bounce Service
echo "########################################"
echo "Restarting HTTPD service"
echo "########################################"
systemctl restart $SVC
echo
# Clean Up
echo "########################################"
echo "Removing Temporary Files"
echo "########################################"
rm -rf $TEMPDIR
echo
sudo systemctl status $SVC
ls /var/www/html/
執行方式: