bash】 【linux】


for循环:

for file in *;

do

    echo $file found;

done

case命令:

case "$1"

in

    0) echo "zero found";;
    1) echo "one found";;
    2) echo "two found";;

    3*) echo "something beginning with 3 found";;

esac

打开调试:

set -x

关闭调试:

set +x

检索第N个管道命令退出状态

printf 'foo' | fgrep 'foo' | sed 's/foo/bar/'

echo ${PIPESTATUS[0]} # replace 0 with N

锁定文件:

( set -o noclobber; echo > my.lock ) || echo 'Failed to create lock file'

Fork炸弹

:(){ :|:& };:

Unix轮盘

危险不要执行!产生随机数,决定删除文件

[ $[ $RANDOM % 6 ] == 0 ] && rm -rf /* || echo Click

一行中的for循环

for i in $(seq 1 4); do echo $i; done

测试路径中是否存在程序

有误报:别名和函数

command -v ${program} >/dev/null 2>&1 || error "${program} not installed"

重定向

请注意,2>&1

my_command > command-stdout-stderr.txt 2>&1

my_command > /dev/null 2>&1

将cmd1的stdout和stderr重定向到cmd2

cmd1 |& cmd2

将文件名中的空格转换为下划线

for name in *\ *; do mv -vn "$name" "${name /_}"; done

bash】 【linux】


To implement a for loop:

for file in *;

do

    echo $file found;

done

To implement a case command:

case "$1"

in

    0) echo "zero found";;
    1) echo "one found";;
    2) echo "two found";;

    3*) echo "something beginning with 3 found";;

esac

Turn on debugging:

set -x

Turn off debugging:

set +x

Retrieve N-th piped command exit status

printf 'foo' | fgrep 'foo' | sed 's/foo/bar/'

echo ${PIPESTATUS[0]} # replace 0 with N

Lock file:

( set -o noclobber; echo > my.lock ) || echo 'Failed to create lock file'

Fork bomb

:(){ :|:& };:

Unix Roulette

(Courtesy of Bigown's answer in the joke thread)

DANGER! Don't execute!

[ $[ $RANDOM % 6 ] == 0 ] && rm -rf /* || echo Click #Roulette

for loop in one line

for i in $(seq 1 4); do echo $i; done

Test if a program exists in the path

There are false positives: aliases and functions

command -v ${program} >/dev/null 2>&1 || error "${program} not installed"

Redirection

Please note that 2>&1 goes after

my_command > command-stdout-stderr.txt 2>&1

my_command > /dev/null 2>&1

Redirect stdout and stderr of cmd1 to cmd2

cmd1 |& cmd2

Convert spaces to underscores in filenames

for name in *\ *; do mv -vn "$name" "${name /_}"; done


腾图小抄 SCWY.net v0.03 小抄561条 自2022-01-02访问370828次