博客
关于我
【CO004】操作系统实践笔记3 —— Shell Script 语法速记
阅读量:616 次
发布时间:2019-03-12

本文共 4419 字,大约阅读时间需要 14 分钟。

笔者:YY同学


PS:尽量不要使用空格,除非语法规定必须使用!!

1. Comment

# Single line comment: '  Multi-line comment This is the first comment  This is the second comment  This is the third comment  '

2. Print

echo "Hello World!"

3. Variable

# global variable definitionmynum=1315  # numbermystr="hello world"  # string# local variable definitionlocal mystr="abc"mystr+=123  # Final restul is "abc123"

4. Retrieve Variable

# use $variable_name, {} is used to show more clear boundarymynum=123code="abc"mystr=${code} # grow mystr from abc to “abc123" echo "Before: ${mystr}"mystr+=${mynum}echo "After: ${mystr}"

5. Array

# define an arraymyarray=()myarray=("three" 1 "five" 0)# set valuemyarray[2]="eight"myarray[5]="!"# print different resultecho "${myarray[0]}"  # show the first itemecho "${myarray[@]}"  # get the whole array, most used in for loopecho "${#myarray[@]}"  # get the amount of itemsecho "${!myarray[@]}"  # get the index of items

6. String

#!/bin/bash  mylongstr="this is a long string"  # initialization echo "My string: ${mylongstr}"  # print stringecho "" echo "Number of characters in string: ${#mylongstr}"  # print length of stringecho "" echo "Splitting my string like an array:"  # split string by space and printfor word in ${mylongstr[@]}; do    echo "${word}"doneecho ""

7. Arithmetic

#!/bin/bash  a=2b=3 mysum=$((a+b))  # arithmetic operation must use double parenthesis!!echo "Sum of a=${a} and b=${b} is ${mysum}"

8. Conditional Statement

#!/bin/bash A=1 if [ $((A)) -eq 0 ]; then    echo "A equals to 0"elif [ $((A)) -gt 0 ]; then    echo "A is greater than 0"else    echo "A is smaller than 0"fi
#!/bin/bash  mystr="This is an OS course" if [ -z "${mystr}" ]; then    echo "Ops... the string is empty."else    echo "The string is not empty."fi
#!/bin/bash  if [ -f example.txt ]; then    echo "File example.txt exists."else    echo "Ops... example.txt does not exist."fi
Operator Description
-eq Returns true if two numbers are equivalent
-ne Returns true if two numbers are not equivalent
-lt Returns true if a number is less than another number
-gt Returns true if a number is greater than another number
-le Returns true if a number is less than or equal another number
-ge Returns true if a number is greater than or equal another number
== Returns true if two strings are equivalent
!= Returns true if two strings are not equivalent
! Returns true if the expression is false
-z Check if a string is empty
-d Check the existence of a directory
-f Check if a file exists and is regular
-e Check the existence of a file
-r Check the existence of a file and read permission
-w Check the existence of a file and write permission
-x Check the existence of a file and execute permission

9. Loop

#!/bin/bash  A=("a" "b" "c") # print 1 to 10for i in {   1..10}; do    echo "${i}"done # print a to cfor char in ${A[@]}; do    echo "${char}"done
#!/bin/bash  A=0 # prints 0 to 9, with each number on a new line.while [ $((A)) -lt 10 ]; do    echo $((A))    (( A++ ))done

10. IO Argument

#!/bin/bash  echo "You called $0, with" if [ $# -eq 0 ]; then    echo "no arguments..."    exit 0fi counter=0for i in "$@"; do    (( counter++ ))    echo "Arg[${counter}]: ${i}"done
  1. The input arguments are obtainable from $@
  2. The number of arguments is obtainable from $#
  3. To specify the n-th argument, use $n, e.g. first argument is $1 while $0 is a special argument that stores the name of the script

11. Shell Command

#!/bin/bash  # capture shell command. output=$(ls)echo ""echo "Output of ls: ${output}"  # execute the command # where is the exit status? echo ""haha; echo "haha gives $?"; echo ""echo "hello_world"; echo "echo gives $?"
  1. Note that the syntax $(command) means executing command (which is different from $((expression)) for evaluating arithmetic expressions)
  2. The exit status of the last executed command is always stored in $?

12. Function

#!/bin/bash  # need a space between function name and '{' !function addition {         result=$(($1 + $2))} function main {       local a=1    local b=2    result=    addition ${a} ${b}    echo "${a}+${b}=${result}"} main  # need a start point

13. String Process

#!/bin/bash  mystr="name.email.phone.remarks" # use Internal Field SeparatorIFS='.'for word in ${mystr[@]}; do    echo $worddone# print: name email phone remarks# restore  IFS IFS=" "$'\n'$'\t'
#!/bin/bash  # use awk librarywhile read line; do    echo "${line}" | awk -F',' '{print $1" "$3}'done < data.csv

随时更新~

转载地址:http://nxwaz.baihongyu.com/

你可能感兴趣的文章
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>