標籤:shell 變數
一 變數定義
1 顯式定義 variable=XXX 沒有空格
2 read variable讀取
主要寫了如下兩行代碼,讀取name和namefamily
read -p "please input your name:" name
read -p "please input your whole name :"name family
運行下
[[email protected] shell]# ./var.sh
please input your name:liuliancao
your name is :liuliancao
please input your name:liu liancao
your name is :liu liancao
please input your whole name :liancao liu
your name is liancao,your family is liu
please input your whole name :liancao
your name is liancao,your family is
please input your whole name :liancao l iu
your name is liancao,your family is l iu
可見變數個數和所輸入的個數一樣,則分配正好,如果變數個數多了,則後面的變數為空白,變數個數少了,那麼最後面的是老大(吃最多)。
3 從檔案中讀取
[[email protected] shell]# read message < txt
[[email protected] shell]# echo $message
hello,i amliuliancao由於是文字重新導向,且txt文本行間有分行符號,所以此時只有一行,如果想持續就得使用迴圈
[[email protected] shell]# while read message
> do
> echo $message
> done < txt
hello,i am liuliancao
this is a test
4 命令替換
1 #!/bin/bash
2 #
time=`date +%c`
3 echo "now time is $time"
執行結果
[[email protected] shell]# ./com.sh
now time is Sat 09 May 2015 12:26:49 PM CST
5 位置參數
常見的有
$0 完整檔案名稱 basename可以取基本名字
$1 第一個變數內容,依次類推
[email protected] 所有變數以數組方式存 $* 所有變數,歸為大字串
6 刪除變數
unset 變數名稱
shell學習之變數