Basics of using bash, and shell tools for covering several of the most common tasks

Introduction

‍

Most shells have their own scripting language with variables, control flow and its own syntax.

‍

Shell scripting optimized for performing shell-related tasks.

‍

  • Creating command pipelines
  • Saving results into files
  • Reading from standard input are primitives in shell scripting

‍


‍

Catalog

‍

‍


‍

Assign variables in bash

‍

Syntax:

‍

  • Assign variables: variableName=value​
  • Access variables' value: $variableName​

‍

Note: variableName = value​ will not work if it is interpreted as calling the variableName​ program with arguments =​ and value​

Added: the space character ​ will perform argument splitting in shell.

‍


‍

Differences between '​ and "​

‍

Strings in bash can be defined with '​ and "​ delimiters, they are not equivalent.

‍

  • ​'​ : Only the literal strings
  • ​"​ : Will substitute variable values that strings contain

‍


‍

Use functions in bash

‍

mcd () {
mkdir -p "$1"
cd "$1"
}

‍

  • ​$0​ - Name of the script
  • ​$1​ to $9​ - Arguments to the script. $1​ is the first argument and so on.
  • ​$@​ - All the arguments
  • ​$#​ - Number of arguments
  • ​$?​ - Return code of the previous command
  • ​$$​ - Process identification number (PID) for the current script
  • ​!!​ - Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doing sudo !!​
  • ​$_​ - Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typing Esc​ followed by .​ or Alt+.​

‍


‍

Write the scripts

‍

We can type it directly in our shell, but we always prefer to write those commands into a file and execute it.

After writing it into a script(e.g. mcd.sh​), we could use source​ to execute and load it.

Next, give the arguments to the script (e.g. mcd test​), and it will work.

‍

bash manner

‍

‍

if

‍

if [ condition ]
then
commands
elif [ condition ]
then
commands
else
commands
fi

‍


‍

case

‍

case variable in
pattern1)
commands
;;
pattern2)
commands
;;
*)
commands
;;
esac

‍


‍

select

‍

select variable in list
do
commands
break
done

‍


‍

for loop

‍

for variable in list
do
commands
done

‍


‍

while loop

‍

while condition
do
commands
done

‍


‍

until loop

‍

until condition
do
commands
done

‍


‍

Stream

‍

  • output: STDOUT​ >​
  • errors : STDERR​ 2>​

‍


‍

Return code or exit status

‍

  • value of 0​ -> OK;
  • anything different from 0​ -> error occurred

‍


‍

Operator

‍

  • and operator: &&​
  • or operator: ||​
  • seperate commands: ;​
  • ​true​: 0 return code
  • ​false​: 1 return code
  • ​-ne​ : not equal

‍


‍

Get the output of a command as a variable

‍

Substitutions

‍

command substitution: $(CMD)​

process substitution: <(CMD)​

Execute CMD​ and place the output in a temporary file and substitute the <()​ with that file’s name.

‍

​/dev/null​ : You can write sth. to it, but all the written things will be discarded.

‍


‍

No need to give arguments one by one

‍

Sometimes, you don't need to give arguments one by one, with the usage of globbing(e.g. *​, ?​, {}​) .

In general, you can just use it by way of using the small regex.

Also, you can use another language(e.g. Python​) to interact with the Shell.

‍


‍

The usage of shebang

‍

We can use the shebang​ (e.g. #!/usr/local/bin/python​) to let the Shell know how to run this program.

But, python may not be installed at this path, and you may can't know where they actually live in your machine, we can use env​ to use the PATH​ environment variable to resolve such a kind of question.(e.g. #!/usr/bin/env python​)

‍

As writing bash​ scripts can be tricky and unintuitive, we can use tools like shellcheck​ to help us to find errors in our sh/bash scripts.

‍


‍

Differences between shell functions and scripts

‍

  • Language

    • Shell functions: The same language as the shell
    • Scripts: Any language(So it's important to include a shebang for scripts)
  • Load

    • Shell functions: Loaded once (Slightly fast to load, but whenever you change them you will have to reload their definition)
    • Scripts: Loaded whenever they are executed
  • execute

    • Shell functions: In the current shell environment(Can modify environment variables)

    • Scripts: In their own process(Can't modify environment variables)

      Scripts will be passed by value environment variables that have been exported using export​

  • As with any programming language, functions are a powerful construct to achieve modularity, code reuse, and clarity of shell code

  • Often shell scripts will include their own function definitions.

‍


‍

Shell Tools

‍

Finding how to use commands

‍

  • ​man​ provides a manual page (called manpage) for a command you specify.
  • ​-h​ or --help​ flags
  • ​:help​ or ?​(For interactive tools such as the ones based on ncurses)
  • ​tldr​ focuses on giving example use cases of a command so you can quickly figure out which options to use.

‍


‍

Finding files

‍

  • find​

    • ​-name​

    • ​-path​ pattern

    • ​-type​

      • ​d​ : directory
      • ​f​ : file
    • ​-mtime​

    • ​-exec​

  • ​fd​

    A simple, fast, and user-friendly alternative to find​.

  • ​locate​

    ​locate​ uses a database that is updated using updatedb​

    • In most systems, updatedb​ is updated daily via cron​.
    • A more in-depth comparison between find​ and locate​ can be found here.

‍


‍

Finding code(the content of the file)

‍

  • ​grep​

    ​grep​ is an incredibly valuable shell tool that we will cover in greater detail during the data wrangling lecture.

    • ​-R​: go through the entire directory

      • Many grep​ alternatives have been developed, including ack, ag, and rg.

      • ​riggrep​ : more colorful

        • ​-u​: not ignore hidden files
        • ​-t​: type
        • ​-C​: show lines around the matched
        • ​--files-without-match​
        • ​--stats​

‍


‍

Finding shell commands(already used)

‍

  • ​up​

  • ​history​

    • ​history | grep find​
  • ​fzf​ bindings.

‍


‍

Directory Navigation

‍

  • tree​
  • ​broot​
  • ​nnn​

‍

‍


‍

Shortcut key

‍

  • Ctl-A

    Moves cursor to beginning of line of text (on the command-line).
  • Ctl-B

    Backspace (nondestructive).
  • Ctl-C

    Break. Terminate a foreground job.
  • Ctl-D

    Log out from a shell (similar to exit).

    EOF (end-of-file). This also terminates input from stdin.

    When typing text on the console or in anxterm window, Ctl-D erases the character under the cursor. When there are no characters present, Ctl-D logs out of the session, as expected. In an xterm window, this has the effect of closing the window.
  • Ctl-E

    Moves cursor to end of line of text (on the command-line).
  • Ctl-F

    Moves cursor forward one character position (on the command-line).
  • Ctl-G

    BEL. On some old-time teletype terminals, this would actually ring a bell. In an xterm it might beep.
  • Ctl-H

    Rubout (destructive backspace). Erases characters the cursor backs over while backspacing.
  • Ctl-I

    Horizontal tab.
  • Ctl-J

    Newline (line feed). In a script, may also be expressed in octal notation -- '\012' or in hexadecimal -- '\x0a'.
  • Ctl-K

    Vertical tab.

    When typing text on the console or in anxterm window, Ctl-K erases from the character under the cursor to end of line. Within a script, Ctl-K may behave differently, as in Lee Lee Maschmeyer's example, below.
  • Ctl-L

    Formfeed (clear the terminal screen). In a terminal, this has the same effect as the clear command. When sent to a printer, a Ctl-L causes an advance to end of the paper sheet.
  • Ctl-M

    Carriage return.
  • Ctl-N

    Erases a line of text recalled from history buffer [8] (on the command-line).
  • Ctl-O

    Issues a newline (on the command-line).
  • Ctl-P

    Recalls last command from history buffer (on the command-line).
  • Ctl-Q

    Resume (XON).

    This resumes stdin in a terminal.
  • Ctl-R

    Backwards search for text in history buffer (on the command-line).
  • Ctl-S

    Suspend (XOFF).

    This freezes stdin in a terminal. (Use Ctl-Q to restore input.)
  • Ctl-T

    Reverses the position of the character the cursor is on with the previous character (on the command-line).
  • Ctl-U

    Erase a line of input, from the cursor backward to beginning of line. In some settings, Ctl-U erases the entire line of input, regardless of cursor position.
  • Ctl-V

    When inputting text, Ctl-V permits inserting control characters.
  • Ctl-W

    When typing text on the console or in an xterm window, Ctl-W erases from the character under the cursor backwards to the first instance of whitespace. In some settings, Ctl-W erases backwards to first non-alphanumeric character.
  • Ctl-X

    In certain word processing programs, Cuts highlighted text and copies to clipboard.
  • Ctl-Y

    Pastes back text previously erased (with Ctl-U or Ctl-W).
  • Ctl-Z

    Pauses a foreground job.

    Substitute operation in certain word processing applications.

    EOF (end-of-file) character in the MSDOS filesystem.

‍


‍

Exercises

‍

  1. ​ls -laht --color​

    1. ​ls -a​
    2. ​ls -h​
    3. ​ls -lh​
    4. ​ls -lt​
    5. ​ls --color​
  2. bash functions marco​ and polo​

    1. ​marco.sh​

      #!/bin/bash
      marco(){
      path_dir=$(pwd)
      export path_dir
      echo "set $path_dir"
      }
    2. ​polo.sh​

      #!/bin/bash
      polo(){
      cd "$path_dir"
      echo -n 'cd to ' && echo "$path_dir"
      }
  3. ​runUFail​

    1. ​fail.sh​

      #!/usr/bin/env bash
      
      n=$(( RANDOM % 100 ))
      
      if [[ n -eq 42 ]]; then
      echo "Something went wrong"
      >&2 echo "The error was using magic numbers"
      exit 1
      fi echo "Everything went according to plan"
    2. ​runUFail​

      #!/bin/bash
      
      runUFail(){
      ./fail.sh > output.txt 2> error.txt
      code=$?
      fail_times=0
      while [ $code -ne 1 ]
      do
      fail_times=$((fail_times+1))
      ./fail.sh >> output.txt 2>> error.txt
      code=$?
      done
      echo "fail times:$fail_times"
      echo "output.txt:" && cat output.txt
      echo "error.txt:" && cat error.txt
      }
  4. ​xargs​ command which will execute a command using STDIN as arguments.

    ​touch ex{0,1,2,3,4,5,asb,'a b','bb'}.html​

    ​find -path "*html" -type f -print0 | ls | xargs -I {} zip output.zip {}​

  5. ​find . -type f -mtime -1 | ls -lt​

‍


‍

References

‍

‍

Basics of using bash, and shell tools for covering several of the most common tasks的更多相关文章

  1. 什么是shell? bash和shell有什么关系?

    什么是shell? bash和shell有什么关系? 博客分类: Linux   什么是Shell?      shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序 ...

  2. 单行bash、shell、perl命令

    主题:单行经典bash.shell.perl命令 作者:luomg 摘要: 会陆陆续的写自己工作中的常用有意思的命令,争取你能看完后就能搞定常见操作, 且尽量自少提供基本shell.perl的实现方式 ...

  3. bash: ./device/nexell/tools/build.sh: 权限不够

    /bin/bash: build/tools/diff_package_overlays.py: 鏉冮檺涓嶅 i686-linux-gcc: error trying to exec 'cc1': ...

  4. bash和shell的关系

    bash是borne again shell的缩写,它是shell的一种,Linux上默认采用的是bash. shell脚本中的方法带不带function的区别,例如: function foo () ...

  5. bash反弹shell检测

    1.进程 file descriptor 异常检测 检测 file descriptor 是否指向一个socket 以重定向+/dev/tcp Bash反弹Shell攻击方式为例,这类反弹shell的 ...

  6. shell 编程 && bash 简介(shell 变量、shell操作环境、数据流重导向、管线命令、shell script)

    如何学习一门编程语言 数据类型 运算符 关键字 1 认识BASH 这个shell linux是操作系统核心,用户通过shell与核心进行沟通,达到我们想要的目的.硬件.核心.用户之间的关系: 原理:所 ...

  7. 用bash反弹shell

    用bash反弹shell 受害主机:linux系统 攻击机:需要安装netcat(nc) 受害主机执行:ifconfig  ## 查看受害主机ip 攻击机执行:nc -lvp 19999 ## 在攻击 ...

  8. 什么是shell? bash和shell有什么关系?

    什么是Shell?      shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序.你在提示符下输入的每个命令都由shell先解释然后传给Linux内核.      ...

  9. 修改shell 将当前shell(默认是bash B SHELL )改为csh C SHELL

     在修改当前shell时,用命令: usermod -s  /bin/csh   home     home 为 你所想要改变的用户地址     此处home 为家目录,一般自己创建的用户都会在家目录 ...

  10. 认识bash这个shell

    我们通过shell将我们输入的命令与内核通信,好让内核可以控制硬件来正确无误地工作bash是我们Linux默认的shell 用户界面(Shell,application)--------核心(Kern ...

随机推荐

  1. trim-all-strings-elements-in-a-complex-object

    package com.xxx.common.util; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.Strin ...

  2. [转]基于图像的三维模型重建4——增量SFM

    内容 几种BA的形式 同时优化相机和三维点 优化相机 只优化三维点 单目相机 增量运动恢复结构(Incremental SFM) 运动恢复结构的几个问题 几种BA的形式 数学模型 n个三维点和m个相机 ...

  3. Eclipse生成javadoc方法与错误解决

    1.Eclipse生成javadoc方法与错误解决 2.Eclipse自动生成文件注释以及使用javadoc命令自动生成API文档 3.maven配置生成java doc文档中文乱码问题解决方案 4. ...

  4. [LC593]有效的正方形-Valid Square

    题目描述 给定2D空间中四个点的坐标 p1, p2, p3 和 p4,如果这四个点构成一个正方形,则返回 true . 点的坐标 pi 表示为 [xi, yi] .输入 不是 按任何顺序给出的. 一个 ...

  5. 从BIOS+MBR迁移到UEFI+GPT 并修复Ubuntu Grub2 UEFI引导

    之前在虚拟机里使用了默认配置安装了Ubuntu16.04,由于需要扩充磁盘空间不得不将磁盘从MBR分区表转换到GPT分区表. 简单介绍一下思路:首先通过Windows下的DiskGenius软件备份U ...

  6. 在 .NET 9 中使用 Scalar 替代 Swagger

    前言 在.NET 9发布以后ASP.NET Core官方团队发布公告已经将Swashbuckle.AspNetCore(一个为ASP.NET Core API提供Swagger工具的项目)从ASP.N ...

  7. RocketMQ原理—1.RocketMQ整体运行原理

    大纲 1.RocketMQ整体运行原理的介绍顺序 2.RocketMQ生产者是如何发送消息的 3.Broker是如何持久化接收到的消息到磁盘上 4.基于DLedger技术的Broker主从同步原理 5 ...

  8. JavaScript 中的组合继承 :ES5 与 ES6 中最近似的写法

    JavaScript 的继承写法较多,在此并不一一讨论,仅对最常用的组合式继承做一个说明: 组合式继承主要利用了原型链继承和构造函数继承. 一.ES5 中的写法 function Person(nam ...

  9. oracle下批量增加序列值

    感谢renjixinchina分享 原文链接http://blog.itpub.net/15747463/viewspace-751593/ oracle下批量增加序列值.批量滚动序列 declare ...

  10. Jenkins执行Shell脚本超时错误处理指南

    Jenkins执行Shell脚本超时错误处理指南 在使用Jenkins进行自动化测试时,经常会遇到需要执行Shell脚本的情况.然而,当Shell脚本执行时间过长,超过了Jenkins配置的超时限制时 ...