macOS & pbcopy command All In One
macOS & pbcopy command All In One
copy from the command line
pbcopy
$ whoami | pbcopy
# xgqfrms-mbp
$ echo "hello, zsh!" | pbcopy
# hello, zsh!
wifi-password
$ brew install wifi-password
# To get the password for the WiFi you're currently logged onto:
$ wifi-password
# To get it for a specific SSID:
$ wifi-password <ssid>
# To put it straight in your clipboard for pasting elsewhere (OS X only):
$ wifi-password | pbcopy
wifi-password.sh
https://github.com/rauchg/wifi-password/blob/master/wifi-password.sh
wifi-password.sh
#!/usr/bin/env sh
version="0.1.0"
# locate airport(1)
airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
# if then fi
if [ ! -f $airport ]; then
echo "ERROR: Can't find \`airport\` CLI program at \"$airport\"."
exit 1
fi
# by default we are verbose (unless non-tty) 冗长的, if then else fi
if [ -t 1 ]; then
verbose=1
else
verbose=
fi
# usage info 输出格式化的用法文档, cat <<EOF multi lines text EOF
usage() {
cat <<EOF
Usage: wifi-password [options] [ssid]
Options:
-q, --quiet Only output the password.
-V, --version Output version
-h, --help This message.
-- End of options
EOF
}
# parse options 解析命令行的参数, ;; === break ❓
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case $1 in
-V | --version )
echo $version
exit
;;
-q | --quiet )
verbose=
;;
-h | --help )
usage
exit
;;
esac
shift
done
if [[ "$1" == "--" ]]; then shift; fi
# merge args for SSIDs with spaces
args="$@"
# check for user-provided ssid
if [ "" != "$args" ]; then
ssid="$@"
else
# get current ssid
ssid="`$airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`"
if [ "$ssid" = "" ]; then
echo "ERROR: Could not retrieve current SSID. Are you connected?" >&2
exit 1
fi
fi
# warn user about keychain dialog
if [ $verbose ]; then
echo ""
echo "\033[90m … getting password for \"$ssid\". \033[39m"
echo "\033[90m … keychain prompt incoming. \033[39m"
fi
sleep 2
# source: http://blog.macromates.com/2006/keychain-access-from-shell/
pwd="`security find-generic-password -D 'AirPort network password' -ga \"$ssid\" 2>&1 >/dev/null`"
if [[ $pwd =~ "could" ]]; then
echo "ERROR: Could not find SSID \"$ssid\"" >&2
exit 1
fi
# clean up password
pwd=$(echo "$pwd" | sed -e "s/^.*\"\(.*\)\".*$/\1/")
if [ "" == "$pwd" ]; then
echo "ERROR: Could not get password. Did you enter your Keychain credentials?" >&2
exit 1
fi
# print
if [ $verbose ]; then
echo "\033[96m ✓ \"$pwd\" \033[39m"
echo ""
else
echo $pwd
fi
bug ❌
# 输出到文件,避免 man 分页问题
$ man grep >> man-grep.md
# 手动复制 cat ✅
$ cat man-grep.md
# 都不好使,pbcopy 乱码 bug ❌
$ cat man-grep.md | pbcopy
$ man grep | pbcopy
solutions
use
vscode
to fix copy from terminal encoding bug 🚀
vscode 全部选中 "
符号,连续两次删除后,保存即可!✅
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://gist.github.com/xgqfrms/73ddeb86602c83e799caf05d6bf9a0eb
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13926735.html
未经授权禁止转载,违者必究!