• Chapter1 Beginning Bash


    1.1 Decoding the Prompt

    root  is all-powerful and can do anything on a typical Unix or Linux system.

    ~ mean you are in your home directory.

    $ indicates this is a regular user, not root.

    1.2 Showing Where You Are

    pwd stands for print working directory and takes two options.

    -L displays your logical path

    -P is the default.P displays your physical location.

    1.3 Finding and Running Commands

    type,which,apropos,locate,slocate,find and ls commands.

    1. [root@MaxwellDBA tmp]# type which
    2. which is a function
    3. which ()
    4. {
    5. ( alias;
    6. eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
    7. }
    8. [root@MaxwellDBA tmp]# type ls
    9. ls is aliased to `ls --color=auto'
    10. [root@MaxwellDBA tmp]# type -a ls
    11. ls is aliased to `ls --color=auto'
    12. ls is /usr/bin/ls
    13. [root@MaxwellDBA tmp]# which which
    14. which ()
    15. {
    16. ( alias;
    17. eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
    18. }
    19. [root@MaxwellDBA tmp]#

    1.4 Getting Information About Files

    Use the ls,stat,file,or find commands.

    1. [root@MaxwellDBA tmp]# touch /tmp/sample_file
    2. [root@MaxwellDBA tmp]# ls /tmp/sample_file
    3. /tmp/sample_file
    4. [root@MaxwellDBA tmp]# ls -l /tmp/sample_file
    5. -rw-r--r-- 1 root root 0 Jul 21 13:30 /tmp/sample_file
    6. [root@MaxwellDBA tmp]# stat /tmp/sample_file
    7. File: /tmp/sample_file
    8. Size: 0 Blocks: 0 IO Block: 4096 regular empty file
    9. Device: fd01h/64769d Inode: 8579 Links: 1
    10. Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
    11. Access: 2022-07-21 13:30:59.014117361 +0800
    12. Modify: 2022-07-21 13:30:59.014117361 +0800
    13. Change: 2022-07-21 13:30:59.014117361 +0800
    14. Birth: 2022-07-21 13:30:59.014117361 +0800
    15. [root@MaxwellDBA tmp]# file /tmp/sample_file
    16. /tmp/sample_file: empty
    17. [root@MaxwellDBA tmp]# file -b /tmp/sample_file
    18. empty
    19. [root@MaxwellDBA tmp]# echo '#!/bin/bash -' > /tmp/sample_file
    20. [root@MaxwellDBA tmp]#
    21. [root@MaxwellDBA tmp]# file /tmp/sample_file
    22. /tmp/sample_file: Bourne-Again shell script, ASCII text executable
    23. [root@MaxwellDBA tmp]# file -b /tmp/sample_file
    24. Bourne-Again shell script, ASCII text executable
    25. [root@MaxwellDBA tmp]#

    1.5 Showing All Hidden(dot) Files in the Current Directory

    1. [root@MaxwellDBA /]# ls -a
    2. . .. .autorelabel bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
    3. [root@MaxwellDBA /]# ls -d .[!.]*
    4. .autorelabel
    5. [root@MaxwellDBA /]# ls -d .??*
    6. .autorelabel
    7. [root@MaxwellDBA /]# ls -d .[!.]* .??* | sort -u
    8. .autorelabel
    9. [root@MaxwellDBA /]#

    1.6 Using Shell Quoting

    1. [maxwell@MaxwellDBA ~]$ echo 'A coffee is $5 for' "$USER" '?!'
    2. A coffee is $5 for maxwell ?!
    3. [maxwell@MaxwellDBA ~]$ echo "A coffee is \$5 for $USER?\!"
    4. A coffee is $5 for maxwell?\!
    5. [maxwell@MaxwellDBA ~]$ echo "A coffee is \$5 for $USER?! "
    6. A coffee is $5 for maxwell?!
    7. [maxwell@MaxwellDBA ~]$ WRONG
    8. -bash: WRONG: command not found
    9. [maxwell@MaxwellDBA ~]$ # wRONG
    10. [maxwell@MaxwellDBA ~]$ echo "$USER won't pay $5 for coffee."
    11. maxwell won't pay for coffee.
    12. [maxwell@MaxwellDBA ~]$ # work
    13. [maxwell@MaxwellDBA ~]$ echo "USER won't pay \$5 for coffee."
    14. USER won't pay $5 for coffee.
    15. [maxwell@MaxwellDBA ~]$ #Also works
    16. [maxwell@MaxwellDBA ~]$ echo 'I won'\''t pay $5 for coffee.'
    17. I won't pay $5 for coffee.
    18. [maxwell@MaxwellDBA ~]$

    1.7 Using or Replacing Built-ins and External Commands

    Use the type and which commands to see if a given command exists and whether it is built-in or external.

    1. [maxwell@MaxwellDBA ~]$ type cd
    2. cd is a shell builtin
    3. [maxwell@MaxwellDBA ~]$ type awk
    4. awk is /usr/bin/awk
    5. [maxwell@MaxwellDBA ~]$ which cd
    6. /usr/bin/cd
    7. [maxwell@MaxwellDBA ~]$ which awk
    8. /usr/bin/awk
    9. [maxwell@MaxwellDBA ~]$

    1.8 Determining If You Are Running Interactively

    Use the following case statement:

    $- is a string listing of all the current shell option flags,

    1. #!/usr/bin/env bash
    2. # cookbook filename: interactive
    3. case "$-" in
    4. *i*) # Code for interactive shell here
    5. ;;
    6. *) # Code for non-interactive shell here
    7. ;;
    8. esac
  • 相关阅读:
    【分布式系统】分布式选举之Bully算法
    GaussDB SQL基础语法示例-数组表达式
    RustDay03——记录刷完Rust100题
    C# Onnx Yolov8 Seg 分割
    Linux logrotate 使用详解
    uniapp uni-combox 数据源使用对象,选择后获取对应项的ID,可指定自定义的balbel,value
    解决所有二叉树路径问题
    前端回流与重绘:概念及触发条件
    .NET 8上进行PDF合并
    AMD发布22.11.1驱动,支持《使命召唤:战区2.0》
  • 原文地址:https://blog.csdn.net/u011868279/article/details/125861063