Linux 命令 exit, 退出当前 shell

Linux命令之exit – 退出当前shell 本文链接: http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。 常用参数 格式: exit n 退出。设置退出码为n。 (Cause the shell to exit with a status of n.) 格式: exit 退出。退出码不变,即为最后一个命令的退出码。 (If n is omitted, the exit status is that of the last command executed. ) 格式: $? 上一个命令的退出码。 格式: trap “commands” EXIT 退出时执行commands指定的命令。 ( A trap on EXIT is executed before the shell terminates.) 退出码 (exit status,或exit code) 的约定: 0表示成功 (Zero - Success) 非0表示失败 (Non-Zero - Failure) ...

Created 26266-12-26 · Updated 9096-10-09 · 2 min · 380 words · -

Linux 命令选项, 参数,括号, 可选, 必选

Linux 命令选项, 参数,括号, 可选, 必选 https://blog.csdn.net/raoqin/article/details/8875089 []: 内的内容意思是: 可写可不写 例如: /home下就一个list 文件,使用ls -help中的 Usage: ls [OPTION]… [FILE]… {}: 那就必须要在{}内给出的选择里选一个。 <>: 表示必选 在数据库中 Transact-SQL 语法规则 Transact-SQL 引用中的语法关系图使用下列规则。 规范 用于 大写 Transact-SQL 关键字。 斜体 Transact-SQL 语法中用户提供的参数。 | (竖线) 分隔括号或大括号内的语法项目。只能选择一个项目。 [ ] (方括号) 可选语法项目。不必键入方括号。 {} (大括号) 必选语法项。不要键入大括号。 [ ,…n ] 表示前面的项可重复 n 次。每一项由逗号分隔。 [ …n ] 表示前面的项可重复 n 次。每一项由空格分隔。 加粗 数据库名、表名、列名、索引名、存储过程、实用工具、数据类型名以及必须按所显示的原样键入的文本。 <标签> ::= 语法块的名称。此规则用于对可在语句中的多个位置使用的过长语法或语法单元部分进行分组和标记。适合使用语法块的每个位置由括在尖括号内的标签表示: <标签>。 命令行语法字符 在命令行语法中,某些字符与格式有着特殊的意义与含义。 本文档中的命令行语法使用以下字符格式: 方括号 [] 方括号 ( [ ] ) 表示里面的元素 (参数、值或信息) 是可选的。 您可以选择一个或多个条目,也可以不选。 不要将方括号本身也输入到命令行中。 ...

Created 19196-03-19 · Updated 9096-10-09 · 1 min · 185 words · -

tr command

tr command # 转大写 echo 'hello' | tr '[:lower:]' '[:upper:]' # 转小写 echo 'HELLO' | tr '[:upper:]' '[:lower:]' # 删除文本中的换行符 tr -d '\n' < input.txt > output.txt # 删除空行 cat file | tr -s "\n" > new_file tr -s "[\012]" < plan.txt tr -s ["\n"] < plan.txt 什么是 tr 命令? tr, translate 的简写 \NNN character with octal value NNN (1 to 3 octal digits) \\ backslash \a audible BEL \b backspace \f form feed \n new line \r return \t horizontal tab \v vertical tab CHAR1-CHAR2 all characters from CHAR1 to CHAR2 in ascending order [CHAR*] in SET2, copies of CHAR until length of SET1 [CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0 [:alnum:] all letters and digits [:alpha:] all letters [:blank:] all horizontal whitespace [:cntrl:] all control characters [:digit:] all digits [:graph:] all printable characters, not including space [:lower:] all lower case letters [:print:] all printable characters, including space [:punct:] all punctuation characters [:space:] all horizontal or vertical whitespace [:upper:] all upper case letters [:xdigit:] all hexadecimal digits [=CHAR=] all characters which are equivalent to CHAR https://blog.csdn.net/jeffreyst_zb/article/details/8047065 通过使用tr,您可以非常容易地实现 sed 的许多最基本功能。您可以将 tr 看作为 sed的 (极其) 简化的变体: 它可以用一个字符来替换另一个字符,或者可以完全除去一些字符。您也可以用它来除去重复字符。这就是所有 tr所能够做的。 ...

Created 14146-02-14 · Updated 9096-10-09 · 6 min · 1089 words · -

mktemp: 安全创建临时文件与目录

mktemp mktemp 用来安全地创建临时文件或临时目录,并把路径打印到标准输出。名字里带随机段,避免多个进程同时写死 /tmp/foo 时互相踩踏或被抢占。 脚本里需要临时落盘时,优先用 mktemp,不要手写固定路径。 基本用法 不传参数时,默认在 $TMPDIR(未设置则为 /tmp)下创建文件,模板类似 tmp.XXXXXXXXXX: tmp=$(mktemp) echo "path=$tmp" echo hello > "$tmp" rm -f "$tmp" 输出示例: /tmp/tmp.aB3kX9pQ2r 自定义模板 模板最后一个路径分量里至少要有 3 个连续的 X,X 会被替换成随机字符: # file under /tmp f=$(mktemp /tmp/myapp.XXXXXX) # with suffix f=$(mktemp --suffix=.log /tmp/myapp.XXXXXX) 创建临时目录 -d / --directory 创建目录而不是文件: workdir=$(mktemp -d) # or workdir=$(mktemp -d /tmp/build.XXXXXX) 权限:文件一般为 u+rw,目录一般为 u+rwx(再受 umask 约束)。 指定父目录 -p DIR / --tmpdir[=DIR]:相对某个目录解释模板。未写 DIR 时用 $TMPDIR,再否则 /tmp: f=$(mktemp -p /var/tmp myapp.XXXXXX) d=$(mktemp -d --tmpdir="$HOME/tmp" work.XXXXXX) 脚本里的常见写法 创建后记得清理。可用 trap 在退出时删除: ...

Created 27276-08-27 · Updated 27276-08-27 · 1 min · 132 words · -

scp

scp scp 可以在两个 linux 主机间复制文件; # 复制目录 -r scp -r local_folder remote_username@remote_ip:remote_folder ash: /usr/libexec/sftp-server: not found This is a consequence(结果/后果) of your client machine using a very recent OpenSSH release (9.0 - check https://www.openssh.com/txt/release-9.0 62 for more info), which changes the scp program to use the SFTP protocol under the hood, which vanilla OpenWrt/dropbear installations do not support. To work around the problem on the client side, use the new -O (that is an uppercase letter “o”) switch when invoking scp, which will cause it to fall back to the legacy behavior. ...

Created 8086-01-08 · Updated 8086-01-08 · 1 min · 205 words · -

shell 进制转换

shell 进制转换 16进制转换成10进制 printf %d 0xF echo $((16#F)) 10进制转换成16进制 printf %x 15 echo "obase=16;15"|bc 三、10进制转换成8进制 printf %o 9 11 四、8进制转换成10进制 echo $((8#11)) 9 五、同理二进制转换成10进制 echo $((2#111)) 7 六、10进制转换成二进制 echo “obase=2;15”|bc 1111 https://blog.csdn.net/rheostat/article/details/8057405

Created 24246-03-24 · Updated 24246-03-24 · 1 min · 32 words · -

shell下批量替换文件名中的空格

shell下批量替换文件名中的空格 http://blog.csdn.net/dliyuedong/article/details/14229121 rename 's/ /_/g' * rename 's/\(/_/g' *

Created 22226-09-22 · Updated 22226-09-22 · 1 min · 9 words · -

bash

bash # -n 选项代表只检查语法,不执行脚本。 bash -n foo.sh # -n 或 --noexec 选项告诉 Bash:只读取并检查脚本的语法是否正确,但不会实际执行脚本中的任何命令。 bash -c 用法: bash -c "cmd string" 通常使用shell去运行脚本,两种方法 bash xxx.sh,另外一种就是bash -c “cmd string” 对于bash xxx.sh, 首先bash 会在当前目录去寻找xxx.sh,如果找到,就直接运行,找不到则按照环境变量$PATH的指定路径,按顺序去找,如果找到,则执行,找不到则报错。 shell 脚本的参数 $0 就是要执行的 shell 脚本 xxx.sh, $1 就是后面紧跟 xxx.sh 的参数,$2 $3依次类推 而对于bash -c “cmd string” 首先我们看看官方的说明解释 -c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with $0. ...

Created 6066-01-06 · Updated 6066-01-06 · 1 min · 174 words · -

执行 Shell 脚本, fork, exec, source, shell 执行

执行 Shell 脚本, fork, exec, source, shell 执行 cx ./xxx.sh sh xxx.sh 用户可以用任何编辑程序来编写Shell程序。因为Shell程序是解释执行的,所以不需要编译成目的程序。按照Shell编程的惯例,以bash 为例,程序的第一行一般为"#!/bin/bash",其中 # 表示该行是注释,叹号 ! 告诉Shell运行叹号之后的命令并用文档的其余部分作为输入,也就是运行/bin/bash并让/bin/bash去执行Shell程序的内容。 执行Shell程序的方法有3种。 sh Shell 程序文件名 这种方法的命令格式为: sh <foo.sh> 这实际上是调用一个新的bash命令解释程序,而把Shell程序文件名作为参数传递给它。新启动的Shell将去读指定的文件,可执行文件中列出的命令,当所有的命令都执行完后结束。该方法的优点是可以利用Shell调试功能。 bash < foo.sh 格式为: bash< Shell程序名 这种方式就是利用输入重定向,使Shell命令解释程序的输入取自指定的程序文件。 用 chmod 命令使 Shell 程序成为可执行的,"./Shell文件名" 一个文件能否运行取决于该文档的内容本身可执行且该文件具有执行权。对于Shell程序,当用编辑器生成一个文件时,系统赋予的许可权都是644(rw-r-r-),用"chomd 755 Shell文件名"命令将其改为可执行的,因此,当用户需要运行这个文件时,"./Shell文件名"来执行就是行了。 在这3种运行Shell程序的方法中,最好按下面的方式选择: 当刚创建一个Shell程序,对它的正确性还没有把握时,应当使用第一种方式进行调试。当一个Shell程序已经调试好时,应使用第三种方式把它固定下来,以后只要键入相应的文件名即可,并可被另一个程序所调用。 source (.) 使用 source 命令和点号(.)是等价的, 类似于 C/C++ 中的 #include 预处理指令,都是将指定的脚本内容拷贝至当前的脚本中,由同一个 Shell 进程来执行。 source (source /directory/script.sh) 与fork的区别是不新开一个 sub-shell 来执行被调用的脚本,而是在同一个 shell 中执行。所以被调用的脚本中声明的变量和环境变量。 都可以在主脚本中得到和使用。 使用 source 执行命令时, 脚本文件可以没有执行权限, source 命令是 bash 的内置命令,不需要 (也没有) 绝对路径. source 命令也称为"点命令",也就是一个点符号 “. “。 source 命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。 ...

Created 26266-12-26 · Updated 26266-12-26 · 2 min · 241 words · -

gorun, golang, shell

gorun, golang, shell 用 golang 语言编写脚本 install gorun go get github.com/erning/gorun 能用 “./” 执行的 golang代码 /// 2>/dev/null ; gorun "$0" "$@" ; exit $? package main import ( "os/exec" "fmt" "os" ) func main() { println("Hello world!") var whoami []byte var err error var cmd *exec.Cmd cmd = exec.Command("whoami") if whoami, err = cmd.Output(); err != nil { fmt.Println(err) os.Exit(1) } // 默认输出有一个换行 fmt.Println(string(whoami)) } ./hello.go golang shell, 在 golang 中调用 shell func shellExec(command string) string { log.Printf("exec: %s\n", command) cmd := exec.Command("/bin/sh", "-c", command) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { log.Printf("failed to exec: %s, err: %v", command, err) return "" } ourStr := out.String() log.Printf("exec response: \n%s", ourStr) return ourStr } func shellExecFmt(format string, params ...interface{}) string { return shellExec(fmt.Sprintf(format, params...)) } go 调用 shell https://blog.csdn.net/qq_36874881/article/details/78234005 ...

Created 18186-09-18 · Updated 18186-09-18 · 2 min · 352 words · -

shell 彩色 echo

shell 彩色 echo #黑底红字 echo -e 'E[31;40mThis prints in red.'; tput sgr0 #!/bin/bash echo -e 'E[COLOR1;COLOR2mSome text goes here.' COLOR1: Foreground Color COLOR2: Background Color #!/bin/bash echo -e 'E[32;40mThis prints in green.'; tput sgr0 echo -e:enable interpretation of backslash escapes -e "允许 反斜杠 (对字符)的转义" e[32;1m: 控制字体和背景颜色的转义字符,3037是字体颜色、4047是背景颜色 “m"终止该转义序列, 然后文本以结束的转义指定的属性显示. tput sgr0: 把终端设置恢复为原样. 如果省略这一句会使后续在该终端的输出仍为xx色. 色彩 前景色 背景色 黑 30 40 红 31 41 ...

Created 28286-07-28 · Updated 28286-07-28 · 1 min · 83 words · -