Linux Bash Tutorial

燕和裕
2023-12-01

Main References

Quick Intro:

https://linuxconfig.org/bash-scripting-tutorial-for-beginners

Reference by Modules:

Bash Introduction | What is Bash? - Javatpoint

Unix / Linux - Special Variables

Common Functionalities

Text, Especially Filename and Dirpath Manipulation:

linux通配符和正则表达式_韩帅平的博客-CSDN博客_linux正则表达式

linux shell 中 %% *的含义_lhc_执笔画江山的博客-CSDN博客_shell%%

linux 获取文件名的后缀,Linux shell 之 提取文件名和目录名的一些方法_何政达的博客-CSDN博客

Linux shell string 操作_wangjicong_215的博客-CSDN博客_linux shell string

File Manipulation

Linux Bash脚本检查文件/文件夹是否存在_yaoxinghuo的专栏-CSDN博客_bash 存在文件

Modular Programming

linux shell脚本中调用另一个shell脚本_whatday的专栏-CSDN博客_linux sh调用另一个sh

Interactive Input

User Input - Bash Scripting Tutorial

Pasring Shell Script Arguments

Internal Variables ==> official doc, all reserved variables ==> attention to "positional parameters"

https://www.baeldung.com/linux/use-command-line-arguments-in-bash-script

Remote Manipulation

使用linux 的shell脚本进行sftp文件上传与下载(转)_EverNoob的博客-CSDN博客_linux的sftp脚本

Linux sftp命令_书香水墨的博客-CSDN博客_linux sftp 

SSH: Execute Remote Command or Script - Linux - ShellHacks 

Special Characters

1. for most POSIX strings, simply use escape

2. for embedded passwd, (e.g. for git pull), use URL or 'percent encoding': GIT: Calling git clone using password with special character | Fabian Lee : Software Engineer

!   #   $    &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

 

Quick Start Notes

Variable

  • declare/initialize:
    • var_name = value
      • no explicit type declaration
      • all capitals by convention
        • lower case words and phrases are usually used for commands
    • var_name = $(some command)
      • take the result from some command and assign to var_name
  • refer: echo $var_name
    • echo var_name ==> print "var_name"
  • value substitution: ${var_name}

I/O

  • Interactive:
    • read var_name
  • get stdin:
    • /dev/stdin
    • use the file to get access to stdin; use with pipeline "|"
  • shell script calling arguments:
    •  see the positional parameters, usually $0 for the commands_interpretor/script_name, and $1+ for the arguments 
  • redirecting: Input Output Redirection in Linux/Unix Examples
    • >/< for output/input redirecting
      • >> for "append to destination" instead of overwritting as > would

Script Tips 

  • get current running script location:
    • #!/bin/bash 
      # Absolute path to this script, e.g. /home/user/bin/foo.sh
      SCRIPT=$(readlink -f "$0")
      # Absolute path this script is in, thus /home/user/bin
      SCRIPTPATH=$(dirname "$SCRIPT")
      echo $SCRIPTPATH
  •  source vs. ./execute https://blog.csdn.net/maxzcl/article/details/122139734 
    • source operate within the current shell, while ./script will open a new sub_shell
    • !!!! the variables defined and modified (particularly those exported to system or modified systems variables) in script would only be available after termination when using "source" !!!!
    • and sometimes ./ is more portable, since its meaning is unmistakably simple. 
 类似资料:

相关阅读

相关文章

相关问答