33篇 shell related articles

Linux 命令:flock - 对打开的文件加锁或解锁

先来看看flock -h 的用法:

Usage:
 flock [options] <file|directory> <command> [command args]
 flock [options] <file|directory> -c <command>
 flock [options] <file descriptor number>

Options:
 -s  --shared             get a shared lock
 -x  --exclusive          get an exclusive lock (default)
 -u  --unlock             remove a lock
 -n  --nonblock           fail rather than wait
 -w  --timeout <secs>     wait for a limited amount of time
 -E  --conflict-exit-code <number>  exit code after conflict or timeout
 -o  --close              close file descriptor before running command
 -c  --command <command>  run a single command string through the shell

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see flock(1).
More ~

shell中的特殊变量

变量 含义
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。
$# 传递给脚本或函数的参数个数。
$* 传递给脚本或函数的所有参数。
$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。
$? 上个命令的退出状态,或函数的返回值。
$$ 当前 Shell 进程 ID。对于 Shell 脚本,就是这些脚本所在的进程 ID。
More ~

shell function: Syntax error: “(” unexpected

shell执行时报这个错

./shell/notify.sh: 7: ./shell/notify.sh: Syntax error: "(" unexpected

notify.sh 写了个方法如

function notify() {
  echo "hello"
}

在mac上执行没问题,但是jenkins下跑时报错了。原因查到在这:

shell中函数定义应该是这样,没有function

notify() {
  echo "hello"
}

如果你硬要加function,那就去掉括号(),如下

function notify {
  echo "hello"
}
More ~

shell cut string

Sample:
api-login-103923

to get the api

echo "api-login-103923"|cut -d"-" -f1

-d is the seprator
-f is the index , starting from 1

More ~

rpmbuild: '\r': command not found

RPM编译时遇到这个问题

xecuting(%build): /bin/sh -e /var/tmp/rpm-tmp.HDvAfF
+ umask 022
+ cd /tmp/csmp-build-TLlpXrJT/BUILD
+ cd csmp-2.0.3sp1
+ $'\r'
/var/tmp/rpm-tmp.HDvAfF: line 29: $'\r': command not found
error: Bad exit status from /var/tmp/rpm-tmp.HDvAfF (%build)


RPM build errors:
    Bad exit status from /var/tmp/rpm-tmp.HDvAfF (%build)
make: *** [rpm] Error 1
make: Leaving directory `/root/rpms/xxx'

打开这个文件/var/tmp/rpm-tmp.HDvAfF时,会看到29号有个特殊的字符 ^M

这个其实是CLRF 没转成LF的问题, linux上用yum install dos2unix,修改相应的文件 即 dos2unix filename 即可

More ~

curl 使用 proxy (http_proxy, https_proxy)

第一种方法:定义环境变量

//无需用户名和密码
export http_proxy=http://ip:port
export https_proxy=http://ip:port

//需要用户名和密码
export http_proxy=http://user:password@ip:port
export https_proxy=https://user:password@ip:port

第二种方法:在执行curl时加参数-x

curl -x <[protocol://][user:password@]proxyhost[:port]> url
--proxy <[protocol://][user:password@]proxyhost[:port]> url
--proxy http://user:password@Your-Ip-Here:Port url
-x http://user:password@Your-Ip-Here:Port url


curl -x http://mike:123456@api.baidu.com:3392 https://www.zixi.org

More ~

python中执行shell command

python3 适用,执行shell命令

使用subprocess

import subprocess

def run(args):
    out = subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)
    stdout, stderr = out.communicate()
    return stdout

print(run(['ls', '-l']))

使用os.system

import os
os.system('ps -ef')

使用os.popen

import os
os.popen('ls')
More ~

shell tree in macos

Use find to list the current folder:

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

Add to ~/.bash_profile or ~/.zshrc
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
  

Or install the tree with brew
brew install tree

More ~

rsync 用法

sync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]host:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
More ~