Bash_or_Commands - 9dian/Index GitHub Wiki

apt install exfatprogs
tune.exfat -L "disk2" /dev/sdb1

常用命令

命令获取外网IP

If the Linux system is connected directly to the internet, the public and private IP addresses will be the same. However, in most cases they differ. Here is an example using the curl command.

$ curl ifconfig.me
216.239.32.10
$ curl icanhazip.com
216.239.32.10
$ curl ipecho.net/plain
216.239.32.10
$ curl ident.me
216.239.32.10
$ curl bot.whatismyipaddress.com
216.239.32.10
$ curl https://diagnostic.opendns.com/myip
216.239.32.10
$ curl http://checkip.amazonaws.com
216.239.32.10
$ curl http://whatismyip.akamai.com
216.239.32.10

ubuntu 下载.deb包

apt-cache depends -i PACKAGE | awk '/Depends:/ {print $2}' | xargs  apt-get download && apt-get download PACKAGE
# Then you can install it in the directory you downloaded these as follows.
dpkg -i *.deb 

grep中特殊字符

awk中特殊字符

本地APT Repository服务

22.04 or 24.04

sudo apt install -y apache2
sudo systemctl enable apache2

https://www.linuxtechi.com/setup-local-apt-repository-server-ubuntu

Create Package Repository Directory & Install Apt-Mirror Utility

sudo mkdir -p /var/www/html/ubuntu
sudo chown www-data:www-data /var/www/html/ubuntu
sudo apt update
sudo apt install -y apt-mirror

Configure Apt-Mirror

sudo cp /etc/apt/mirror.list /etc/apt/mirror.list-bak
sudo vi /etc/apt/mirror.list

############# config ##################
#
# set base_path    /var/spool/apt-mirror
#
# set mirror_path  $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
#set base_path    /var/www/html/ubuntu
set base_path    /repo/ubuntu
set nthreads     20
set _tilde 0
#
############# end config ##############

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main multiverse restricted universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main multiverse restricted universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main multiverse restricted universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main multiverse restricted universe

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main multiverse restricted universe

clean https://mirrors.tuna.tsinghua.edu.cn/ubuntu



修改问题

vim fix_apt-mirror-errors.sh

 !/bin/bash

# cd /var/www/html/ubuntu/archive.ubuntu.com/ubuntu/dists
cd /envs/bak/tmp/1/

#for dist in noble noble-updates noble-security noble-backports; do
#  for comp in main multiverse universe; do
#    for size in 48 64 128; do
#      mkdir -p $dist/$comp/dep11/
#      # wget https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/$dist/$comp/dep11/icons-${size}x${size}@2.tar.gz -O $dist/$comp/dep11/icons-${size}x${size}@2.tar.gz;
#      wget https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/$dist/$comp/dep11/icons-${size}x${size}@2.tar.gz -O $dist/$comp/dep11/icons-${size}x${size}@2.tar.gz;
#   done
# done
#done

#cd /var/tmp
cd /envs/bak/tmp/2/
for p in "${1:-noble}"{,-{security,updates,backports}}/{main,restricted,universe,multiverse}
do
  >&2 echo "${p}"
  wget -c -r -e robots=off -np -R "index.htm*" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${p}/cnf/Commands-amd64.xz"
  wget -c -r -e robots=off -np -R "index.htm*" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${p}/cnf/Commands-i386.xz"
  wget -c -r -e robots=off -np -R "index.htm*" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${p}/binary-i386/"
done

# sudo cp -av archive.ubuntu.com/ubuntu/ /var/www/html/ubuntu/archive.ubuntu.com


同步到本地

 sudo apt-mirror

本地APT Repository服务

22.04 or 24.04

sudo apt install -y apache2
sudo systemctl enable apache2

https://www.linuxtechi.com/setup-local-apt-repository-server-ubuntu

Create Package Repository Directory & Install Apt-Mirror Utility

sudo mkdir -p /var/www/html/ubuntu
sudo chown www-data:www-data /var/www/html/ubuntu
sudo apt update
sudo apt install -y apt-mirror

Configure Apt-Mirror

sudo cp /etc/apt/mirror.list /etc/apt/mirror.list-bak
sudo vi /etc/apt/mirror.list

############# config ##################
set base_path    /repo/ubuntu
set nthreads     20
set _tilde 0
#
############# end config ##############

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main multiverse restricted universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main multiverse restricted universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main multiverse restricted universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main multiverse restricted universe

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main multiverse restricted universe

clean https://mirrors.tuna.tsinghua.edu.cn/ubuntu



修改问题

vim fix_apt-mirror-errors.sh

 !/bin/bash

# cd /var/www/html/ubuntu/archive.ubuntu.com/ubuntu/dists
cd /envs/bak/tmp/1/

#for dist in noble noble-updates noble-security noble-backports; do
#  for comp in main multiverse universe; do
#    for size in 48 64 128; do
#      mkdir -p $dist/$comp/dep11/
#      # wget https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/$dist/$comp/dep11/icons-${size}x${size}@2.tar.gz -O $dist/$comp/dep11/icons-${size}x${size}@2.tar.gz;
#      wget https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/$dist/$comp/dep11/icons-${size}x${size}@2.tar.gz -O $dist/$comp/dep11/icons-${size}x${size}@2.tar.gz;
#   done
# done
#done

#cd /var/tmp
cd /envs/bak/tmp/2/
for p in "${1:-noble}"{,-{security,updates,backports}}/{main,restricted,universe,multiverse}
do
  >&2 echo "${p}"
  wget -c -r -e robots=off -np -R "index.htm*" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${p}/cnf/Commands-amd64.xz"
  wget -c -r -e robots=off -np -R "index.htm*" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${p}/cnf/Commands-i386.xz"
  wget -c -r -e robots=off -np -R "index.htm*" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/${p}/binary-i386/"
done

# sudo cp -av archive.ubuntu.com/ubuntu/ /var/www/html/ubuntu/archive.ubuntu.com


同步到本地

 sudo apt-mirror

Debug

With bash 4.1 or later:

#!/bin/bash

exec 5> command.txt
BASH_XTRACEFD="5"

echo -n "hello "

set -x
echo -n world
set +x

echo "!"

Output to stdout (FD 1):

hello world!

Output to command.txt (FD 5):

+ echo -n world
+ set +x

GPU/CPU/TPU

 lshw -C display
 lshw -numeric -C display

sed

sed中特殊字符

https://unix.stackexchange.com/questions/32907/what-characters-do-i-need-to-escape-when-using-sed-in-a-sh-script

支持换号匹配

sed ':a;N;$!ba;s/\t#\n\tpap/NewString/g' file

add BOM to csv file

UTF-8


sed -i '1s/^\(\xef\xbb\xbf\)\?/\xef\xbb\xbf/' foo-*

sed -i '1s/^/\xef\xbb\xbf/' foo-*

UTF-16

To add BOMs to the all the files that start with "foo-", you can use sed. sed has an option to make a backup.

sed -i '1s/^\(\xff\xfe\)\?/\xff\xfe/' foo-*

straceing this shows sed creates a temp file with a name starting with "sed". If you know for sure there is no BOM already, you can simplify the command:

sed -i '1s/^/\xff\xfe/' foo-*

Make sure you need to set UTF-16, because i.e. UTF-8 is different.

delete all blank lines.

sed '/./!d' intputfile

VIM

Java变量名 to SQL字段名

:%s/\<\u\|\l\u/\= join(split(tolower(submatch(0)), '\zs'), '_')/gc

or 不是以大写字母开头

:%s/\l\u/\= join(split(tolower(submatch(0)), '\zs'), '_')/gc

SQL/json字段名 to Java变量名

%s#\l#= toupper(split(submatch(0), '')[0])#g

reverse the order of lines in a file

:g/^/m0

vim convert file format from dos to unix

If you do :e +ff=dos, you convert the file to DOS format; i.e. after writing, all line endings will be converted to CR-LF. You've mentioned that you cannot convert to the whole file to Unix format, but if converting to DOS is okay, then this is the solution.

For your function, you don't need to go through :normal, as the :e command is an Ex command, and those can be directly used in a function. You only need :normal for normal-mode commands like dd. Therefore, you can rewrite:

function! Fix_dos() " This would be the correct syntax, but no need for :normal: "execute "normal :e ++ff=dos<cr>" " Also, no need for execute: "execute "e ++ff=dos" edit ++ff=dos endfunction In fact, I would define:

:command! FixDos edit ++ff=dos If you cannot covert the entire file, you'll have to live with the ^M; best you can do is trying to hide them, as you've tried with concealing. This variant might catch more instances of ^M by also appying inside other syntax groups:

:syntax match Todo /\r/ conceal containedin=ALL

4.

1,5s/\v(0\d)/\=printf("%02d", substitute(submatch(0), '^0+', '', 0)+1)/g 1,5s/\v(0\d)/\=printf("%02d", substitute(submatch(0), '^0+', '', '')+1)/g

sql中日期自增

.s/\v(2020-?)([01]\d-?)([0-3]\d)/\=printf("%s%s%02d", submatch(1), submatch(2), substitute(submatch(3), '^0+', '', '')+1)/g

Bash test

文件/目录判断:

[ -a FILE ] 如果 FILE 存在则为真。

[ -b FILE ] 如果 FILE 存在且是一个块文件则返回为真。

[ -c FILE ] 如果 FILE 存在且是一个字符文件则返回为真。

[ -d FILE ] 如果 FILE 存在且是一个目录则返回为真。

[ -e FILE ] 如果 指定的文件或目录存在时返回为真。

[ -f FILE ] 如果 FILE 存在且是一个普通文件则返回为真。

[ -g FILE ] 如果 FILE 存在且设置了SGID则返回为真。

[ -h FILE ] 如果 FILE 存在且是一个符号符号链接文件则返回为真。(该选项在一些老系统上无效)

[ -k FILE ] 如果 FILE 存在且已经设置了冒险位则返回为真。

[ -p FILE ] 如果 FILE 存并且是命令管道时返回为真。

[ -r FILE ] 如果 FILE 存在且是可读的则返回为真。

[ -s FILE ] 如果 FILE 存在且大小非0时为真则返回为真。

[ -u FILE ] 如果 FILE 存在且设置了SUID位时返回为真。

[ -w FILE ] 如果 FILE 存在且是可写的则返回为真。(一个目录为了它的内容被访问必然是可执行的)

[ -x FILE ] 如果 FILE 存在且是可执行的则返回为真。

[ -O FILE ] 如果 FILE 存在且属有效用户ID则返回为真。

[ -G FILE ] 如果 FILE 存在且默认组为当前组则返回为真。(只检查系统默认组)

[ -L FILE ] 如果 FILE 存在且是一个符号连接则返回为真。

[ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read则返回为真。

[ -S FILE ] 如果 FILE 存在且是一个套接字则返回为真。

[ FILE1 -nt FILE2 ] 如果 FILE1 比 FILE2 新, 或者 FILE1 存在但是 FILE2 不存在则返回为真。

[ FILE1 -ot FILE2 ] 如果 FILE1 比 FILE2 老, 或者 FILE2 存在但是 FILE1 不存在则返回为真。

[ FILE1 -ef FILE2 ] 如果 FILE1 和 FILE2 指向相同的设备和节点号则返回为真。

字符串判断

[ -z STRING ] 如果STRING的长度为零则返回为真,即空是真

[ -n STRING ] 如果STRING的长度非零则返回为真,即非空是真

[ STRING1 ]  如果字符串不为空则返回为真,与-n类似

[ STRING1 == STRING2 ] 如果两个字符串相同则返回为真

[ STRING1 != STRING2 ] 如果字符串不相同则返回为真

[ STRING1 < STRING2 ] 如果 “STRING1”字典排序在“STRING2”前面则返回为真。

[ STRING1 > STRING2 ] 如果 “STRING1”字典排序在“STRING2”后面则返回为真。

数值判断

[ INT1 -eq INT2 ] INT1和INT2两数相等返回为真 ,=

[ INT1 -ne INT2 ] INT1和INT2两数不等返回为真 ,<>

[ INT1 -gt INT2 ] INT1大于INT2返回为真 ,>

[ INT1 -ge INT2 ] INT1大于等于INT2返回为真,>=

[ INT1 -lt INT2 ] INT1小于INT2返回为真 ,<

[ INT1 -le INT2 ] INT1小于等于INT2返回为真,<=

逻辑判断

[ ! EXPR ] 逻辑非,如果 EXPR 是false则返回为真。 [ EXPR1 -a EXPR2 ] 逻辑与,如果 EXPR1 and EXPR2 全真则返回为真。 [ EXPR1 -o EXPR2 ] 逻辑或,如果 EXPR1 或者 EXPR2 为真则返回为真。 [ ] || [ ] 用OR来合并两个条件 [ ] && [ ] 用AND来合并两个条件

其他判断

[ -t FD ] 如果文件描述符 FD (默认值为1)打开且指向一个终端则返回为真

[ -o optionname ] 如果shell选项optionname开启则返回为真

Cron Expression

ref: https://www.baeldung.com/cron-expressions Let's understand the cron expression.

It consists of five fields:

<minute> <hour> <day-of-month> <month> <day-of-week> <command>

3.1. Special Characters in Expression

  • * (all) specifies that event should happen for every time unit. For example, “*” in the field means “for every minute.”
  • ? (any) is utilized in the and <day-of -week> fields to denote the arbitrary value and thus neglect the field value. For example, if we want to fire a script at “5th of every month” irrespective of what day of the week falls on that date, we specify a “?” in the field.
  • – (range) determines the value range. For example, “10-11” in the field means “10th and 11th hours.”
  • , (values) specifies multiple values. For example, “MON, WED, FRI“ in field means on the days “Monday, Wednesday and Friday.”
  • / (increments) specifies the incremental values. For example, a “5/15” in the field means at “5, 20, 35 and 50 minutes of an hour.”
  • L (last) has different meanings when used in various fields. For example, if it's applied in the field, it means last day of the month, i.e. “31st of January” and so on as per the calendar month. It can be used with an offset value, like “L-3”, which denotes the “third to last day of the calendar month.” In , it specifies the “last day of a week.” It can also be used with another value in , like “6L”, which denotes the “last Friday.”
  • W (weekday) determines the weekday (Monday to Friday) nearest to a given day of the month. For example, if we specify “10W” in the field, it means the “weekday near to 10th of that month.” So if “10th” is a Saturday, the job will be triggered on “9th,” and if “10th” is a Sunday, it will trigger on “11th.” If we specify “1W” in and if “1st” is Saturday, the job will be triggered on “3rd,” which is Monday, and it will not jump back to the previous month.
  • # specifies the “N-th” occurrence of a weekday of the month, for example, “third Friday of the month” can be indicated as “6#3”.
⚠️ **GitHub.com Fallback** ⚠️