sh bash片段 - 这些片段大多只是我自己的笔记;我找到了一些,有些我已经找到了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh bash片段 - 这些片段大多只是我自己的笔记;我找到了一些,有些我已经找到了相关的知识,希望对你有一定的参考价值。

# bash function to get yes/no feed back from the user (return is considered no here)
agree() {
    local question="$1"

    while true; do
        read -p "$question " answer
        case $answer in
            [Yy]|[Yy][Ee][Ss]) return 0 ;;
            [Nn]|[Nn][Oo]|'') return 1 ;; # empty string is default
            *) echo "Please answer yes or no.";;
        esac
    done
}
agree 'Should we continue?' || exit

# use process substitution and a custom file descriptor to
# allow reading from stdin within a while read loop
exec 3< <(find . -type f -maxdepth 1)
while read -u 3 line; do
    # ...
done

# replace runs of 2 or more spaces or horizontal tabs with tabs
sed -e "s/[[:blank:]]\{2,\}/\t/g"

###############################
## setting up a personal central git repository
## ssh-host:  a remote host defined in ~/.ssh/config
## comaro: the name of the repo

# on a remote host or local filesystem:
git init --bare comaro.git  # (option: use --shared if there are other contributors)

# on local host cd to camaro directory:
git remote add origin ssh-host:comaro.git
(for origin on local filesystem: git remote add origin file:///Volumes/volume-name/comaro.git)

git push --set-upstream origin --all
git push --set-upstream origin --tags
###############################

# print pid of java process
ps -C java -o pid=

# create CSR
sudo openssl genrsa -out example.com.key 2048
sudo openssl req -new -key example.com.key.key -out example.com.key.csr

# create a self signed certificate
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

# when nginx configtest fails, show why --CBT
sudo service nginx configtest || tail -n5 /var/log/nginx/error.log

# validating sitemap.xml against sitemaps.org schema
xmllint --noout --schema http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd sitemap.xml 2>&1

# multi-line batch editing html with perl --CBT
perl -i -0777pe 's/my old string(\s*)/my new string$1/s' $(find . -name \*.html -exec grep -l 'my old string' "{}" \; 2>/dev/null)

# start popfile spam filter proxy
cd /Library/POPFile/; sudo VERSIONER_PERL_VERSION=5.12 /usr/bin/perl -Ilib popfile.pl

# revert to last commit
git reset HEAD --hard

# print repo-name/current-branch-name if in a git repository
git status &> /dev/null && echo -n "${PWD##*/}/" && git branch | sed -n 's/^* //p;'

# list all files that've been deleted
git log --name-status --no-merges | sed -n 's/^[D]'$'\t''//p' | sort | uniq

# list all files that haven't been deleted
git log --name-status --no-merges | sed -n 's/^[^D]'$'\t''//p' | sort | uniq

# determine if an ios binary contains localizations other than english --CBT
unzip -l some.ipa | \
sed '/\/en\.lproj\//d' | \
grep '/..\.lproj/Localizable\.strings' >/dev/null \
&& echo multiple localizations \
|| echo english localization only

# dump a mysqlite data base
sqlite3 database.sqlite <<< .dump | less

# create a tarball of files on a remote server while saving it locally --CBT
ssh remote-host 'tar -ckf - remote-folder' > local-file.tbz

# show process that is serving web pages
sudo lsof -i :80

# extract bundleidentifier from ios ipa file --CBT
/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' /dev/stdin <<< $(unzip -p some.ipa \*/Info.plist)

# add an SSHA encrypted entry for user Jim to .htpasswd file --CBT
PASSWORD='SEcRe7PwD'; SALT=$(openssl rand -base64 3);
SHA1=$((echo -n "$PASSWORD$SALT" | openssl dgst -binary -sha1; echo -n $SALT) | openssl base64)
echo "Jim:{SSHA}$SHA1" >> .htpasswd

# list host names of all virtual hosts in apache httpd.conf --CBT
awk '/^<VirtualHost/{while ($0 !~ /ServerName/) {getline} print $2}' httpd.conf

# while loop to test files from a list of files
while read f; do file $f; done < ~/file-list

# test if remote file exists via ssh e.g. before using scp --CBT
rfile() { ssh ${1%%:*} "stat ${1##*:} &>/dev/null"; }
# EXAMPLE:
# rfile ssh-host:afile && echo file exists || scp afile ssh-host:afile

# open a link in default browser if Firefox not present. --CBT
open -a Firefox 'http://localhost' 2>/dev/null || open 'http://localhost'

# generate an upper case RFC 4122 UUID using openssl and sed --CBT
openssl rand -hex 16 | sed 'y/abcdef/ABCDEF/; s/\(........\)\(....\)\(....\)\(....\)\(.*\)/\1-\2-\3-\4-\5/'

# generate an RFC 4122 UUID using openssl and bash string operations --CBT
U=$(openssl rand -hex 16)
echo ${U:0:8}-${U:8:4}-${U:12:4}-${U:16:4}-${U:20:12}

# extract rdf nodes from pdf and output as xml --CBT
env LANG=en_US.US-ASCII tr -cs '[:print:]' '\n' < some.pdf | \
awk 'BEGIN {
    print "<?xml version=\"1.0\"?>"
    print "<rdf:RDF"
    print "  xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
    print "  xmlns=\"http://purl.org/rss/1.0/\""
    print "  xmlns:admin=\"http://webns.net/mvcb/\""
    print ">"
}
/<rdf:Description/,/<\/rdf:Description/{
    print
}
END {
    print "</rdf:RDF>"
}'

# convert pdf to html using doc2html.pl
doc2html.pl /absolute/path/to/some.pdf 'application/pdf'

# Get current flood stage for a river (in feet) in the USA. In this example the Winooski river at Essex Junction, VT (essv1).
# TIP: When this value reaches or exceeds 12.5, avoid North Willston Rd. Bridge. It will most likely be closed. ;) --CBT
curl -s 'http://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=essv1&output=xml' | \
xpath '/*/observed/datum[1]/primary/text()' 2>/dev/null; echo \'

# test if a variable is a number
isNumber() { [[ "$1" =~ ^[0-9]+$ ]] && echo Is a number || echo Is NOT a number; }

# sign a mac application
codesign -s 'Certificate Name' -i 'bundle.identifier' -fv 'Your Program.app'
# windows equivalent:
# signtool sign /v /f c:\path\to\key.p12 /p password /t http://timestamp.example.com prog-to-sign.exe

# read a password in perl without echoing characters
perl -e 'use Term::ReadKey; print "password: "; ReadMode 2; $pw=ReadLine; ReadMode 0; print "\n$pw"'

# create a binary sha1 signature of some stuff in a file named "data" (or stdin):
openssl dgst -sha1 -sign mykey.pem -out data.sig.sha1.bin data

# verify an sha1 signature with rsa public key and original data file (or stdin):
openssl dgst -sha1 -verify pubkey.pem -signature data.sig.sha1.bin data

# to extract the Adobe director player from a mounted shockwave installer disk image, switch to
# an empty folder then run the following:
gunzip -c /Volumes/Adobe*Shockwave*/Shockwave_Installer_Full.pkg/Contents/Archive.pax.gz | \
pax -r ./Lib*/App*

# list read-only mounted file systems --CBT
mount | sed -n 's/^\/dev\/.* on \(\/.*\) (.*read-only.*/\1/p'

# remove comments
sed 's/[[:blank:]]*#.*//; /^$/d' file

# netcat as a port scanner
nc -zw2 localhost 80 &>/dev/null && echo open || echo closed

以上是关于sh bash片段 - 这些片段大多只是我自己的笔记;我找到了一些,有些我已经找到了的主要内容,如果未能解决你的问题,请参考以下文章

sh bash片段

sh Bash的代码片段

sh Git片段#tags:bash

sh Bash片段

使用 Django 模板作为片段

片段 getActivity 不起作用