Diewuxi

Belive tomorrow will be better, love science and technology, support communication and understanding, always ready for thought turn.

Blog / engineering_technology / computer / code_interpreter / Shell script 笔记

Blog


Article^ Parent

Shell script 笔记


Date: 2016-03-26 09:00:00
Description: Shell script 笔记。
Keywords: Shell script, Bash
Category: engineering_technology/computer/code_interpreter
Tag: shell
Link: https://www.diewuxi.com/blog/article/65.html

Shell script 笔记。

Bash excute procedure

GNU bash,版本 4.2.37(1)-release-(x86_64-pc-linux-gnu)

本地执行

    以 bash 执行
        交互的 login shell, 或非交互的非 login shell 带有 --login 选项
            /etc/profile
            ~/.bash_profile
            ~/.bash_login
            ~/.profile
            ~/.bash_logout

        交互的非 login shell
            /etc/bash.bashrc
            ~/.bashrc

        非交互的非 login shell
            环境变量 BASH_ENV 的展开值作为文件全名

    以 sh 执行(伪装 sh 启动方式, 遵循 POSIX 标准)
        交互的 login shell, 或非交互的非 login shell 带有 --login 选项
            /etc/profile
            ~/.profile

        交互的非 login shell
            环境变量 ENV 的展开值作为文件全名

        非交互的非 login shell
            不读取文件

通过网络执行

    以 bash 执行
        ~/.bashrc

    以 sh 执行(伪装 sh 启动方式, 遵循 POSIX 标准)
        不读取文件
                        

Test filetype

file type in Linux, I am not sure whether it is complete.

-f   regular
-d   directory
-c   character
-b   block
-h   symbolic link
-S   socket
-p   pipeline
                        

cat multi-line

    cat << EOF
Usage: $(basename "$(readlink -f "${0}")") {option}
option:
    start       Start.
    toggle      Toggle window above or below.
EOF
                        

tips

List files problem

for i in ./*
                        

There is mistake, when . has no files, that is ./* represent it self. So check if it exists as follows.

for i in ./*
do
    if test ! -e "${i}"
    then
        continue
    fi

    ...
done
                        

move or copy wild match

When copy or move all files include hidden files, use cp dir/* won't deal with hidden files, the right way is using cp dir.

Make environment varibale statement in shell script more safe

s/\([^\"]\)\$\(\w\+\)/\1\"\${\2}\"/g

$var --> "${var}"
                        

生成 help function item

sed -n -e "/^.* ()$/ s/^\(.*\) ()$/    --\1/p" <shell_script_file>
                        

turn

...
foo ()
...
bar ()
...
                        

to

    --foo
    --bar
                        

生成 function case

sed -n -e "/^.* ()$/ s/^\(.*\) ()$/    (--\1)\n        \1\n        ;;\n/p" <shell_script_file>
                        

turn

...
foo ()
...
bar ()
...
                        

to

    (--foo)
        foo
        ;;
    (--bar)
        bar
        ;;
                        

歌词文件名歌手歌曲名互换

for i in ./*; do new=$(echo "${i#./}" | sed -e "s/^ *\([^-]*[^ ]\) *- *\(.*[^ ]\) *\.lrc/\2__\1.lrc/"); mv "${i}" "${new}"; done
                        

挑选歌词复制到目标目录

for i in ./*; do i="${i#./}"; i="${i%.lrc}"; if ls -1 ../music/2020 | grep -q "${i}"; then if test -f "${i}.lrc"; then echo "copying ""${i}"".lrc ..."; cp "${i}".lrc ../music/2020; fi; fi; done
                        

Last modified: 2021-03-10

Comments [0]

There is no comments now.

Write comment(* is necessary, and email is not shown to public)


Diewuxi 2017--2024