(1)追踪一下 shell strace
./strace ./command.sh
# ./strace ./command.sh execve("./command.sh", ["./command.sh"], 0x7ff0f82270 /* 13 vars */) = 0 brk(NULL) = 0xa2ea000 faccessat(AT_FDCWD, "/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "tls/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/data/usrlibs/tls/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/data/usrlibs/tls/aarch64", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/data/usrlibs/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/data/usrlibs/tls", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/data/usrlibs/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/data/usrlibs/aarch64", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/data/usrlibs/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/data/usrlibs", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib64/tls/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/lib64/tls/aarch64", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib64/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/lib64/tls", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib64/aarch64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) newfstatat(AT_FDCWD, "/lib64/aarch64", 0x7fd3b19fc0, 0) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0\267\0\1\0\0\0\270\374\1\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=1279888, ...}) = 0 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7d5ad000 mmap(NULL, 1352480, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7d43a000 mprotect(0x7f7d56c000, 61440, PROT_NONE) = 0 mmap(0x7f7d57b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x131000) = 0x7f7d57b000 mmap(0x7f7d581000, 13088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7d581000 close(3) = 0 mprotect(0x7f7d57b000, 16384, PROT_READ) = 0 mprotect(0x4b5000, 4096, PROT_READ) = 0 mprotect(0x7f7d5b1000, 4096, PROT_READ) = 0 getuid() = 0 brk(NULL) = 0xa2ea000 brk(0xa30b000) = 0xa30b000 getpid() = 176 rt_sigaction(SIGCHLD, {sa_handler=0x4334a4, sa_mask=~[RTMIN RT_1], sa_flags=0}, NULL, 8) = 0 rt_sigaction(SIGHUP, {sa_handler=SIG_DFL, sa_mask=[HUP], sa_flags=SA_RESTART}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 getppid() = 173 uname({sysname="Linux", nodename="rockchip", ...}) = 0 newfstatat(AT_FDCWD, "/mnt", {st_mode=S_IFDIR|0775, st_size=4096, ...}, 0) = 0 newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0775, st_size=4096, ...}, 0) = 0 openat(AT_FDCWD, "./command.sh", O_RDONLY) = 3 fcntl(3, F_DUPFD, 10) = 10 close(3) = 0 fcntl(10, F_SETFD, FD_CLOEXEC) = 0 rt_sigaction(SIGINT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 rt_sigaction(SIGINT, {sa_handler=0x4334a4, sa_mask=~[RTMIN RT_1], sa_flags=0}, NULL, 8) = 0 rt_sigaction(SIGQUIT, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=~[RTMIN RT_1], sa_flags=0}, NULL, 8) = 0 rt_sigaction(SIGTERM, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 rt_sigaction(SIGTERM, {sa_handler=SIG_DFL, sa_mask=~[RTMIN RT_1], sa_flags=0}, NULL, 8) = 0 read(10, "#! /bin/sh\n\n\nsleep 10s\nprintf \"h"..., 1023) = 41 newfstatat(AT_FDCWD, "/bin/sleep", {st_mode=S_IFREG|S_ISUID|0755, st_size=685024, ...}, 0) = 0 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f7d5adc40) = 177 wait4(-1,
调试技术:
shell中打印:
echo "xxxxxxxxxxxxxxxx"
printf "xxxxxxxxxxxxx\n"
shell中注释:
方法一: 行注释: #
#! /bin/sh :<<eof sleep 1s i am is programmer \n i am is doctor printf "hello\n" eof
方法二:
:<<!
!
方法三:
if false; then
xxxxxxxx
xxxxxxxxxx
fi
方法四
shell 中控制语句:
有个坑: if 后面[ ] 这个符号
xxflag = 0 if [ -f "/jffs2/xxx.sh" ]; then /jffs2/xxxx.sh & xxxflag=1 fi
shell中 sleep
论shell后台运行的重要性: 否则里面有阻塞操作就会导致卡顿
source xxx &
shell中函数调用和函数定义: