萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

新手入門:Linux Shell 基礎知識與第一支腳本實作

一、查看可用 Shell

cat /etc/shells

二、編寫第一支 Shell 腳本

命名 firstscript,內容:

#! /bin/bash
# This is a test.
echo -n Your current directory is:
pwd
echo $HOME
echo Your current directory is:
pwd
# END.

三、執行 Shell 腳本

/bin/bash firstscript

找不到檔案時用:/bin/bash $(pwd)/firstscript

四、設定腳本可執行權限

chmod a+x firstscript

之後可直接 ./firstscript 執行。

五、Shell 互動操作範例

讀取使用者輸入並條件判斷:

#!/bin/sh
echo -n Please input your ID:
read id_var
echo -n Please input your password:
read password
echo User ID = $id_var
echo password = $password
if [ $password = "admin" ]; then
    echo "password is right"
else
    echo "password is wrong"
fi

自行在終端機輸入執行體驗互動效果。

copyright © 萬盛學電腦網 all rights reserved