shell 教程一:變數,字串,傳參
寫如下內容:
vi hello.sh
注意這時的.sh檔案沒有執行許可權,要更改為可執行狀態
[email protected]:~/test_shell$ cat hello.sh
#!/bin/bash
echo "hello shell!"
執行hello.sh
[email protected]:~/test_shell$ ls -l hello.sh
-rw-rw-r--1 linux linux 33Dec1901:14 hello.sh
[email protected]:~/test_shell$chmod +x hello
.sh[email protected]:~/test_shell$ ls -l hello.sh
-rwxrwxr-x 1 linux linux 33Dec1901:14 hello.sh
練習:
[email protected]:~/test_shell$ ./hello.sh
hello shell!
二,shell變數 1,定義變數 定義變數時,變數名不加美元符號
[email protected]:~/shell_test$ vi shell.sh
[email protected]:~/shell_test$ /bin/sh shell.sh
hello world!
[email protected]
:~/shell_test$ ./shell.shbash:./shell.sh:Permission denied
[email protected]:~/shell_test$ chmod +x shell.sh
[email protected]:~/shell_test$ ./shell.sh
hello world!
[email protected]:~/shell_test$ ls -l shell.sh
-rwxrwxr-x 1 linux linux 33Dec1906:56 shell.sh
[email protected]:~/shell_test$ cat shell
.sh#!/bin/bash
echo "hello world!"
注意,變數名和等號之間不能有空格,這可能和你熟悉的所有程式語言都不一樣。同時,變數名的命名須遵循如下規則:
- 首個字元必須為字母(a-z,A-Z)。
- 中間不能有空格,可以使用下劃線(_)。
- 不能使用標點符號。
- 不能使用bash裡的關鍵字(可用help命令檢視保留關鍵字)。
- 例如:
-
輸出:[email protected]:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
echo ${your_name}
[email protected]:~/shell_test$ ./shell.sh
hello world!
LiuJing
LiuJing
-
your_name="xiao ming"
[email protected]:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
echo ${your_name}
for skill in C C++JavaPthonAndorid;do
echo "I am good at ${skill}Language!"
done
[email protected]:~/shell_test$ ./shell.sh
hello world!
LiuJing
LiuJing
I am good at CLanguage!
I am good at C++Language!
I am good at JavaLanguage!
I am good at PthonLanguage!
I am good at AndoridLanguage!
如果不給skill變數加花括號,寫成echo "I am good at $skillScript",直譯器就會把$skillLanguage!當成一個變數(其值為空),程式碼執行結果就不是我們期望的樣子了。
推薦給所有變數加上花括號,這是個好的程式設計習慣。
下面證明其為空:
2,修改已定義過的變數 已定義的變數,可以被重新定義, 但注意,第二次賦值的時候不能寫$your_name="alibaba",使用變數的時候才加美元符($)。
[email protected]:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
echo ${your_name}
for skill in C C++JavaPthonAndorid;do
echo "I am good at $skillLanguage!"
done
[email protected]:~/shell_test$ ./shell.sh
hello world!
LiuJing
LiuJing
I am good at !
I am good at !
I am good at !
I am good at !
I am good at !
修改如下:
3,只讀變數
[email protected]:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
your_name="xiao niu"
echo $your_name
[email protected]:~/shell_test$ ./shell.sh
hello world!
LiuJing
xiao niu
[email protected]:~/shell_test$ cat shell.sh
#!/bin/bash
echo "hello world!"
your_name="Liu Jing"
echo $your_name
readonly your_name
your_name="xiao niu"
echo $your_name
[email protected]:~/shell_test$ ./shell.sh
hello world!
LiuJing
./shell.sh: line 9: your_name: readonly variable
LiuJing
刪除變數
使用 unset 命令可以刪除變數。變數被刪除後不能再次使用。unset 命令不能刪除只讀變數。
語法:
相關推薦
shell 教程一:變數,字串,傳參
一,hello shell vi hello.sh 寫如下內容: [email protected]:~/test_shell$ cat hello.sh #!/
Spring Boot基礎教程 ( 一 ) :基礎專案構建,引入web模組,完成一個簡單的RESTful API
簡介 在您第1次接觸和學習Spring框架的時候,是否因為其繁雜的配置而退卻了?在你第n次使用Spring框架的時候,是否覺得一堆反覆黏貼的配置有一些厭煩?那麼您就不妨來試試使用Spring Boot來讓你更易上手,更簡單快捷地構建Spring應用! Spring Boot
Python基礎(一)--- Python簡介,特點,程式碼規範,變數,運算子,條件迴圈語句,元組,字串,列表,字典,檔案操作
一、Python簡介 ----------------------------------------------------------- 1.Python是一種解釋型,面向物件,動態資料型別的高階程式語言 2.像Perl語言一樣, Python 原始碼同樣遵循 GPL(GNU
一:springCloud服務發現者,服務消費者(方誌朋《史上最簡單的 SpringCloud 教程》專欄讀後感)
註冊服務中心 new–>project–>spring Initializr—>(next)…—>Dependencies(Cloud Discovery Eureka Server) 在入口類處加註解@EnableEurekaServ
詳解Shell腳本:sed命令工具 ,awk命令工具
概念 roc 自動 剪切 一位數 工作 shadow 粘貼 amp -----------------------------概述----------------------------------- Linux文本處理工具(以下都是命令和工具,而且只是作為展示,不能作為
Shiro入門一:基於java專案,初識Shiro
在看了兩天的《跟我學Shiro》之後,針對大佬的部落格原始碼來梳理一下對Shiro的簡單理解。 《跟我學Shiro》pdf上傳兩次都失敗了,上傳成功之後再貼出來分享給需要的同學。 首先是shiro-permission.ini檔案的配置: 裡面包含了使用者和許可權 [use
Java程式設計師從笨鳥到菜鳥之(一)開發環境搭建,基本語法,字串,陣列
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Python學習-第1課(變數,字串,運算子,迴圈)
學習前準備~ 環境安裝: Anaconda3+PyCharm python文件 python概述: 一、Hello world及註釋 二、變數 1. 命名規則: 變數命名可以包含數字,大小寫字母,下劃線 數字不可以在開頭 一般在pytho
Numpy 整理筆記(一):陣列的建立,形狀和計算
What’s numpy? 一個在Python中做科學計算的基礎庫,重在數值計算,也是大部分PYTHON科學計算庫的基礎庫,多用於在大型、多維陣列上執行數值運算 1. 建立陣列 import numpy as np # 1. 建立陣列 # 呼叫numpy下面的array方法
MATLAB學習筆記一:MATLAB基礎知識,陣列基礎知識
1. Schedule Week Topic/Event Notes 1 簡介 2 MATLAB計算機與矩陣操作 3 結構化程式與自定義函式 4 變數與檔案存取 5 初階繪圖 6 進階繪圖 2. MATLAB
Python學習之旅_01day:變數和常量,變數輸入,縮排,if條件,記憶體回收機制
1.變數和常量1.1 變數是為了儲存程式運算過程中的一些中間結果,為了方便日後呼叫1.2 變數存在一定的描述性,讓大眾一看就知道該變數的用途 1.3書寫方式 變數的命名規則 1. 要具有描述性 2. 變數名只能_,數字,字母組成,不可以是空格或特殊字元(#?<.,¥$*!~) 3. 不能以中文為變數名
1.問題一:在oracle中,連續十次嘗試登陸不成功,那麼此賬戶將會被鎖定(lock)。當使用被鎖定的賬戶登入時,系統會報錯:ORA-28000: the account is locked。
解決思路: 以管理員的身份登入,即以sys或者system登入資料庫; 使用命令解鎖使用者:alter user 這裡是使用者名稱 account unlock; 重置密碼:alter user 這裡是使用者名稱 identified by 這裡是重置的密碼; 重新登入即可。
Python 排序演算法[一]:令你茅塞頓開,卻又匪夷所思
閱讀本文可以幫助你解開以下疑惑:演算法是什麼?演算法難不難?怎麼才能夠在短時間內熟悉業內的經典演算法呢?這些演算法用 Python 實現會是什麼樣的?它們的耗時會跟時間複雜度相關嗎? 神馬是演算法? 演算法(Algorithm)是指解題方案的準確而完整的描述,是一系列解決問題的清晰
前端演算法:給定一個字串,找到它中的第一個非重複字元並返回它的索引
給定一個字串,找到它中的第一個非重複字元並返回它的索引。如果它不存在,則返回-1。 例子: s =‘leetcode’ 返回0。 s =‘loveleetcode’ 返回2。 注意:您可以假設該字串僅包含小寫字母。 <script> var
perl語言:分割路徑字串,獲取檔名
#獲取path變數內的A1_0.htk檔名 $path="C:\Users\ABC\Desktop\ivector\data\A1_0.htk"; @Name = split(/\\/,$path);
【shell】workon:未找到命令,(永久為其它使用者設定虛擬環境)
問題出現原因: 在預設登入使用者環境下,virtualenvwrapper.sh檔案會被source自動執行,也就有了一系列虛擬環境命令。 檢視bashrc內容: # bashrc在終端啟動之前會被執行 vim ~/.bashrc 檢視virtualenvwrapper.
題目描述:給定一個字串,求出其所有可能的字元組合. 比如:abc 其所有組合是:a,b,c,ab,ac,bc,abc
/* 題目描述:給定一個字串,求出其所有可能的字元組合. 比如:abc 其所有組合是:a,b,c,ab,ac,bc,abc 是遞迴的思想:首先你拿到的是abc,你可以依次打印出去掉c,b,a的組合:ab,ac,bc 然後遞迴用這些子串去調動這個函式,比如ab:依次去掉b,
[shell]shell運算(數字[加減乘除,比較大小],字串,檔案)
算術運算首先這裡所說的算術運算是指加法(+), 減法(-),乘法(*),整除(/), 求餘(%)四種運算,可以通過新增括號改變優先順序,並且只能對整數進行運算(Linux Bash Shell預設不支援
80x86 組合語言程式設計:輸入數字串,排序、並求最大和最小
;從鍵盤輸入一個ENTER結束的十進位制數字串, 按ASCII碼值降序排列這個數字串,;舉報|2013-12-10 21:36鼬風行 | 分類:組合語言 | 瀏覽61次;並另起一行,顯示出ASCII碼
JBoss 系列七十一:jBPM 6 釋出,快速安裝,新功能介紹
概述 jBPM 6.0 最終版已與上月底釋出,本文主要從兩個方面去介紹jBPM 6.0 : 快速安裝新功能介紹 快速安裝 快速安裝包括如下三步: 下載安裝包 安裝 到jbpm-installer目錄下執行ant命令: ant install.demo ant命令執