Skip to content

FAQ

2>&1

  • 0:stdin
  • 1:stdout
  • 2:stderr

redirect stderr appending to stdout stream. &1是为了与文件1区别

echo test 2 >> 1
echo test 2 >>&1

这两者是不一样的。

tee

tee 命令支持从std IO读取并输出到文件。

1
2
3
4
5
6
 make 2>&1 | tee a
make: *** No targets specified and no makefile found.  Stop.
 ls
a 
 cat a
make: *** No targets specified and no makefile found.  Stop. 

ssh login without password

Copy local public key to remote machine.

ssh-copy-id -i id_rsa.pub uec1szh@SGHZ001036195

Shell Parameter Expansion

\({param:-\)}

检查param是否为空,如果是,则使用备选的XXX

1
2
3
4
5
6
7
8
9
#A="aaabbbccc"
B="bd"
C=${A:-${B}}
echo $A, $B, $C

A="aaabbbccc"
B="bd"
C=${A:-${B}}
echo $A, $B, $C

结果为

, bd, bd
aaabbbccc, bd, aaabbbccc

GNU doc

shell script set -xue

这些开关可以显式地写在脚本顶端,也可以在调用shell脚本时作为参数传入:

sh -xeu xxx.sh
  • set -u 检查是否有变量未定义undefined,如果有未定义,则终止脚本执行。 这个选项很有用,特别是当脚本里使用了rm -rf时可以避免意外的删除。
1
2
3
4
#!/bin/bash

set -u
echo $AAA
  • set -e 脚本运行有错误error时,终止执行。相当于开启了断点。 不使用它的时候,shell默认会继续执行后续语句。
1
2
3
4
5
6
#!/bin/bash

set -ue

echo $AAA
echo helloworld!
  • set -x 展开eXpand显示变量的内容

Using Docker with Rootless Mode

1.Add docker group to system (if non-existed)

sudo groupadd docker

2.Adding user uec1szh to group docker

sudo gpasswd -a $USER docker

3.change group or logout to change to the new group

newgrp docker

NOTE: For lower version ubuntu (< 21.04), still need to change socker file attribute.

sudo chown root:docker /var/run/docker.sock
sudo chown -R root:docker /var/run/docker

4.(Optional) logout

sudo pkill -u $USER

logout via remote ssh

sudo pkill -u $USER

Cannot change default shell - user 'xxx' does not exist in /etc/passwd

The system is using distributed authentication (Kerberos), so user account doesn't appear in local /etc/passwd.

1
2
3
4
5
6
xxx@xxx:~$ chsh
Password:
Changing the login shell for uec1szh
Enter the new value, or press ENTER for the default
        Login Shell [/bin/bash]:
chsh: user 'uec1szh' does not exist in /etc/passwd

However user account can be retrieved via:

getent passwd $USER

output:

xxx@host:~$ getent passwd $USER
xxx:*:1420530021:1420530021:xxx xxx (department/XXX):/home/xxx:/bin/bash