Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell使用 #23

Open
heidsoft opened this issue Jan 5, 2017 · 9 comments
Open

shell使用 #23

heidsoft opened this issue Jan 5, 2017 · 9 comments
Assignees
Labels

Comments

@heidsoft
Copy link
Owner Author

heidsoft commented Jan 5, 2017

@heidsoft heidsoft self-assigned this Jan 5, 2017
@heidsoft heidsoft added the linux label Jan 5, 2017
@heidsoft
Copy link
Owner Author

heidsoft commented Nov 30, 2018

目录判断

if [ -d "$WORKING_DIR" ]; then rm -Rf $WORKING_DIR; fi
if [ $( ls <file> ) ]; then rm <file>; fi
#!/bin/sh

if [ -fe FILE ]
then 
    rm FILE
fi 

https://stackoverflow.com/questions/4846007/check-if-directory-exists-and-delete-in-one-command-unix

判断目录是否存在

General syntax to see if a directory exists or not
[ -d directory ]
OR
test directory
See if a directory exists or not with NOT operator:
[ ! -d directory ]
OR
! test directory

Find out if /tmp directory exists or not
Type the following command:
$ [ ! -d /tmp ] && echo 'Directory /tmp not found'

OR
$ [ -d /tmp ] && echo 'Directory found' || echo 'Directory /tmp not found'

#!/bin/bash
DIR="$1"
 
if [ $# -ne 1 ]
then
	echo "Usage: $0 {dir-name}"
	exit 1
fi
 
if [ -d "$DIR" ]
then
	echo "$DIR directory  exists!"
else
	echo "$DIR directory not found!"
fi

https://www.cyberciti.biz/tips/find-out-if-directory-exists.html

@heidsoft heidsoft changed the title sed使用 shell使用 Nov 30, 2018
@heidsoft
Copy link
Owner Author

heidsoft commented Nov 30, 2018

ps 显示内存排序

ps aux --sort -rss
ps aux --sort rss

ps 根据pid 排序

ps aux --sort pid

ps 显示信息案例

EXAMPLES
To see every process on the system using standard syntax:
   ps -e
   ps -ef
   ps -eF
   ps -ely

To see every process on the system using BSD syntax:
   ps ax
   ps axu

To print a process tree:
   ps -ejH
   ps axjf

To get info about threads:
   ps -eLf
   ps axms

To get security info:
   ps -eo euser,ruser,suser,fuser,f,comm,label
   ps axZ
   ps -eM

To see every process running as root (real & effective ID) in user format:
   ps -U root -u root u

To see every process with a user-defined format:
   ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
   ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
   ps -eopid,tt,user,fname,tmout,f,wchan

Print only the process IDs of syslogd:
   ps -C syslogd -o pid=

Print only the name of PID 42:
   ps -p 42 -o comm=

https://alvinalexander.com/linux/unix-linux-process-memory-sort-ps-command-cpu
https://alvinalexander.com/unix/man/linux-ps-man-page-ps-help

ps 实时查看

watch -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head'

ps-command-examples-for-linux-process-monitoring

@heidsoft
Copy link
Owner Author

grep 高级技艺

grep 同时满足多个关键字
① grep -E "word1|word2|word3" file.txt 
满足任意条件(word1、word2和word3之一)将匹配。 
② grep word1 file.txt | grep word2 |grep word3 
必须同时满足三个条件(word1、word2和word3)才匹配。

grep 同时排除多个关键字
不说废话, 例如需要排除 abc.txt 中的 mmm nnn
grep -v 'mmm\|nnn' abc.txt 


grep '[0-9]:[0-9][0-9]:[0-9][0-9]'
If you need get timestamp only, and your grep is gnu grep.

grep -o '[0-9]:[0-9][0-9]:[0-9][0-9]'
and if you work more harder, limit on time format only:

grep '[0-2][0-9]:[0-5][0-9]:[0-5][0-9]'

@heidsoft
Copy link
Owner Author

@heidsoft
Copy link
Owner Author

heidsoft commented Dec 23, 2020

Best Practices for Writing Bash Scripts

https://kvz.io/bash-best-practices.html
https://yoone.eu/article/good-practices-for-writing-shell-scripts/

Use long options (logger --priority vs logger -p). If you're on cli, abbreviations make sense for efficiency. but when you're writing reusable scripts a few extra keystrokes will pay off in readability and avoid ventures into man pages in the future by you or your collaborators.

Use set -o errexit (a.k.a. set -e) to make your script exit when a command fails.

Then add || true to commands that you allow to fail.

Use set -o nounset (a.k.a. set -u) to exit when your script tries to use undeclared variables.

Use set -o xtrace (a.k.a set -x) to trace what gets executed. Useful for debugging.

Use set -o pipefail in scripts to catch mysqldump fails in e.g. mysqldump |gzip. The exit status of the last command that threw a non-zero exit code is returned.

#!/usr/bin/env bash is more portable than #!/bin/bash.

Avoid using #!/usr/bin/env bash -e (vs set -e), because when someone runs your script as bash ./script.sh, the exit on error will be ignored.

Surround your variables with {}. Otherwise bash will try to access the $ENVIRONMENT_app variable in /srv/$ENVIRONMENT_app, whereas you probably intended /srv/${ENVIRONMENT}_app.

You don't need two equal signs when checking if [ "${NAME}" = "Kevin" ].

Surround your variable with " in if [ "${NAME}" = "Kevin" ], because if $NAME isn't declared, bash will throw a syntax error (also see nounset).

Use :- if you want to test variables that could be undeclared. For instance: if [ "${NAME:-}" = "Kevin" ] will set $NAME to be empty if it's not declared. You can also set it to noname like so if [ "${NAME:-noname}" = "Kevin" ]

Set magic variables for current file, basename, and directory at the top of your script for convenience.

Summarizing, why not start your next bash script like this:

#!/usr/bin/env bash

Bash3 Boilerplate. Copyright (c) 2014, kvz.io

set -o errexit
set -o pipefail
set -o nounset

set -o xtrace

Set magic variables for current file & dir

__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app

arg1="${1:-}"
If you have additional tips, please share and I'll update this post.

@heidsoft
Copy link
Owner Author

cat pods1.list.log |tr -s '[:blank:]' ',' > pods2.list.cvs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant