Navigation Menu

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

Bash 脚本杂项 #33

Open
hanxi opened this issue Aug 9, 2019 · 0 comments
Open

Bash 脚本杂项 #33

hanxi opened this issue Aug 9, 2019 · 0 comments
Labels

Comments

@hanxi
Copy link
Owner

hanxi commented Aug 9, 2019

读取配置文件

比如配置文件是这样的:

token=abcd
enckey=xyz
function get_config() {
    echo $(awk -F "=" '/^'$1'/ {print $2}' $config)
}

token=$(get_config token)
enckey=$(get_config enckey)

输出多行字符串

function usage() {
    cat << EOF
Usage: xclip [OPTION] [FILE]...
Access an X server selection for reading or writing.

  -i, -in          read text into X selection from standard input or files
                   (default)
  -o, -out         prints the selection to standard out (generally for
                   piping to a file or program)
  -l, -loops       number of selection requests to wait for before exiting
  -d, -display     X display to connect to (eg localhost:0")
  -h, -help        usage information
      -selection   selection to access ("primary", "secondary", "clipboard" or "buffer-cut")
      -noutf8      don't treat text as utf-8, use old unicode
      -target      use the given target atom
      -version     version information
      -silent      errors only, run in background (default)
      -quiet       run in foreground, show what's happening
      -verbose     running commentary

Report bugs to <astrand@lysator.liu.se>
EOF
    exit 0
}

## 输出多行字符串到文件

```bash
cat > $config << EOF
token=$1
enckey=$2
EOF

加密解密字符串

echo -e "Hello World" | openssl enc -e -aes-256-cbc -k enckey > enc.tmp
cat enc.tmp | openssl enc -d -aes-256-cbc -k enckey

读取命令行参数

# xxx.sh [-i file] [-o]
oclip_type=copy
input_file='-'
while getopts ":i:o" o; do
    case "${o}" in
        i)
            input_file=${OPTARG}
            ;;
        o)
            oclip_type=paste
            ;;
        *)
            usage
            ;;
    esac
done

插入路径到 PATH 环境变量

#insert ~/.local/bin into $PATH
old_path_len=${#PATH}
sub_path=${PATH#$bin}
new_path_len=${#sub_path}
#echo $old_path_len
#echo $new_path_len
if [ $old_path_len -eq $new_path_len ]; then
    profile=$HOME/.bashrc
    touch $profile
    bin='$HOME/.local/bin'
    sed -i "\:export PATH=\$PATH\:$bin:d" $profile
    echo "export PATH=\$PATH:$bin" >> $profile
    . $profile
fi
@hanxi hanxi added the Linux label Aug 9, 2019
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