ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

【CO004】操作系统实践笔记3 —— Shell Script 语法速记

2021-02-24 18:30:18  阅读:260  来源: 互联网

标签:bin Shell string Script number echo CO004 mystr bash


笔者:YY同学

生命不息,代码不止。好玩的项目尽在GitHub


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 definition
mynum=1315  # number
mystr="hello world"  # string

# local variable definition
local mystr="abc"
mystr+=123  # Final restul is "abc123"

4. Retrieve Variable

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

5. Array

# define an array
myarray=()
myarray=("three" 1 "five" 0)

# set value
myarray[2]="eight"
myarray[5]="!"

# print different result
echo "${myarray[0]}"  # show the first item
echo "${myarray[@]}"  # get the whole array, most used in for loop
echo "${#myarray[@]}"  # get the amount of items
echo "${!myarray[@]}"  # get the index of items

6. String

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

7. Arithmetic

#!/bin/bash 
 
a=2
b=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
OperatorDescription
-eqReturns true if two numbers are equivalent
-neReturns true if two numbers are not equivalent
-ltReturns true if a number is less than another number
-gtReturns true if a number is greater than another number
-leReturns true if a number is less than or equal another number
-geReturns 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
-zCheck if a string is empty
-dCheck the existence of a directory
-eCheck the existence of a file
-rCheck the existence of a file and read permission
-wCheck the existence of a file and write permission
-xCheck the existence of a file and execute permission

9. Loop

#!/bin/bash 
 
A=("a" "b" "c")
 
# print 1 to 10
for i in {1..10}; do
  echo "${i}"
done
 
# print a to c
for 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 0
fi
 
counter=0
for 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 Separator
IFS='.'
for word in ${mystr[@]}; do
  echo $word
done
 
# restore  IFS 
IFS=" "$'\n'$'\t'
#!/bin/bash 
 
# use awk library
while read line; do
  echo "${line}" | awk -F',' '{print $1" "$3}'
done < data.csv

随时更新~

标签:bin,Shell,string,Script,number,echo,CO004,mystr,bash
来源: https://blog.csdn.net/qq_42950838/article/details/114028616

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有