sh 每个子目录中的文件数

Count of files in each sub-directory.sh
# Count of files in each sub-directory
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "$(find "{}" -type f | wc -l)" {}' \; | sort -nr

## Output example
# 1128 ./wordpress-seo
# 676 ./wp-ses
# 618 ./css-javascript-toolbox
# 423 ./gravityforms
# 394 ./wp-usertrack

sh 使用某个端口查找进程

findUsedPorts
netstat -vanp tcp | grep <port#>

sh 指定从PostgtreSQL转储的表

pg_dump.sh
# dump specific tables from PG
for table in table1 table2 table3 etc;
do pg_dump -t $table -U userName dbName > /home/anik/psqlTest/db_dump_dir/$table.sql;
done;
EDIT: This approach can be extended to get the list of tables dynamically by running a query through psql and feeding the results into the loop instead of a hard-coded list:
for table in $(psql -U userName -d dbName -t -c "Select table_name From information_schema.tables Where table_type='BASE TABLE' and table_name like 'thr_%'");
do pg_dump -t $table -U userName dbName > /home/anik/psqlTest/db_dump_dir/$table.sql;
done;

sh 从命令行打开本地站点

example.sh
site-launcher() {
  GREEN='\033[0;32m'
  NC='\033[0m'

  sites=$(cat /etc/hosts | grep "127.0.0.1")
  i=1
  for j in $sites
  do
    if [ $j != "127.0.0.1" ]; then
      printf "${j} ${GREEN}[${i}]${NC}\n"
      site[i]=$j
      i=$(( i + 1 ))
    fi
  done

  read -p "Enter website number: " input
  open "http://${site[$input]}"
}

sh 流浪汉ssh

vagrant_ssh.sh
# save the config to a file
vagrant ssh-config > vagrant-ssh

# run ssh with the file.
ssh -F vagrant-ssh default

sh 计算Linux中HugePages / HugeTLB的建议值

hugepages_settings.sh
#!/bin/bash
#
# hugepages_settings.sh
#
# Linux bash script to compute values for the
# recommended HugePages/HugeTLB configuration
#
# Note: This script does calculation for all shared memory
# segments available when the script is run, no matter it
# is an Oracle RDBMS shared memory segment or not.
# Check for the kernel version
KERN=`uname -r | awk -F. '{ printf("%d.%d\n",$1,$2); }'`
# Find out the HugePage size
HPG_SZ=`grep Hugepagesize /proc/meminfo | awk {'print $2'}`
# Start from 1 pages to be on the safe side and guarantee 1 free HugePage
NUM_PG=1
# Cumulative number of pages required to handle the running shared memory segments
for SEG_BYTES in `ipcs -m | awk {'print $5'} | grep "[0-9][0-9]*"`
do
   MIN_PG=`echo "$SEG_BYTES/($HPG_SZ*1024)" | bc -q`
   if [ $MIN_PG -gt 0 ]; then
      NUM_PG=`echo "$NUM_PG+$MIN_PG+1" | bc -q`
   fi
done
# Finish with results
case $KERN in
   '2.4') HUGETLB_POOL=`echo "$NUM_PG*$HPG_SZ/1024" | bc -q`;
          echo "Recommended setting: vm.hugetlb_pool = $HUGETLB_POOL" ;;
   '2.6' | '3.8' | '3.10' | '4.1' ) echo "Recommended setting: vm.nr_hugepages = $NUM_PG" ;;
    *) echo "Unrecognized kernel version $KERN. Exiting." ;;
esac
# End

sh 的gitのブランチ名を変更する

.sh
git branch -m <古いブランチ名> <新しいブランチ名>

sh 壳 - 循环

demo_for_1.sh
#!/bin/bash

for((i=0;i<10;i++));do
  echo $i
done

sh 在Docker中创建新数据库

sh
docker exec -it db mysql -u [MYSQL_USERNAME] -p{MYSQL_USER_PWD} -e 'CREATE DATABASE IF NOT EXISTS `test-data`'

sh MacOS minikube国内源安装

install.sh
#!/bin/bash

curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.2.0/minikube-darwin-amd64 && \
          chmod +x minikube && sudo mv minikube /usr/local/bin/

minikube start --registry-mirror=https://registry.docker-cn.com

# Specify version
#minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.12.1