Showing posts with label Bash (Unix shell). Show all posts
Showing posts with label Bash (Unix shell). Show all posts

Wednesday, June 8, 2016

Blogger: Total Posts in This Year

Command:

$ curl -vs "http://smashingtheory.blogspot.jp" 2>&1 | grep -o -m 1 "<span class='post-count' dir='ltr'>([0-9]*)" | sed -e 's/[^[[:digit:]]]*//g'

Result:

477

Tuesday, May 17, 2016

Command command

$ help command
command: command [-pVv] command [arg ...]
    単純なコマンドを実行するかコマンドに関する情報を表示します。
 
    シェル関数の探索を抑止して COMMAND を引数 ARGS で実行します。または
    指定した COMMANDs の情報を表示します。シェル関数と同じ名前のコマンド
    がディスク上に存在する時に使用します。
 
    オプション:
      -p 全ての標準ユーティリティが見つかると保証される PATH を
    デフォルトの値として使用する
      -v `type'組み込み関数と同様に COMMAND の説明を表示する
      -V COMMAND に対してより冗長な説明を表示する
 
    終了ステータス:
    COMMAND の終了ステータスを返します。または COMMAND が見つからない時に失敗を返します。


$ command -v sar
/usr/bin/sar


$ help type
type: type [-afptP] name [name ...]
    コマンドの種類に関する情報を表示します。
   
    各 NAME に対してコマンド名として使われた時にどのように解釈されるかを
    示します。
   
    オプション:
      -a NAME という名前になっている実行可能なものの全ての位置を表示し
    ます。これには `-p' が同時に指定されていない場合に限ってエイリアス、
    組み込み関数、シェル関数も含みます
      -f シェル関数の検索を抑止します
      -P 各 NAME に対して PATH 探索を強制します。エイリアス、組み込み
    関数、シェル関数があったとしても実行されるディスク上のファイル名を
    返します
      -p 実行されるディスク上のファイル名を返します。`type -t NAME'
    が `file' を返さない場合、何も返しません。
      -t 次のいずれかの単語を返します。`alias', `keyword', `function',
    `builtin', `file' or `'。それぞれ NAME がエイリアス、シェル予約語、
    シェル関数、シェル組み込み関数、ディスク上のファイル、何も見つからない
    に対応します。
   
    引数:
      NAME 解釈するコマンドの名前です。
   
    終了ステータス:
    全ての NAME が見つかった場合に成功を返します。どれかが見つからなかった場合
    は失敗を返します。


$ type -P sar
/usr/bin/sar


$ command -v sar > /dev/null && echo "found"
found

Windows 10: Running Bash on Ubuntu on Windows!

Wednesday, May 11, 2016

Bash: Setting LANG Environment Variable to Japanese

$ date
Wed May 11 19:03:22 JST 2016

$ gdate
Wed May 11 19:03:20 JST 2016

$ export LANG=ja_JP.UTF-8

$ echo $LANG
ja_JP.UTF-8

$ date
2016年 5月11日 水曜日 19時03分49秒 JST

$ gdate
水  5 11 19:03:48 JST 2016

Thursday, April 21, 2016

OS X: Launchpad: Changing Grid Layout

Command:
$ defaults write com.apple.dock springboard-columns -int 12;defaults write com.apple.dock springboard-rows -int 4;defaults write com.apple.dock ResetLaunchPad -bool TRUE;killall Dock
Result:

Wednesday, April 13, 2016

OS X: Bash: Generating Random English Word

Command:

$ wcount=$[`wc -l < /usr/share/dict/words`];fromhead=$[$wcount - 1];random=$[`jot -r 1 0 $fromhead`];head -$[$random] /usr/share/dict/words | tail -1

Result:

everywhereness

Explaination:

Dictionary used: /usr/share/dict/words
Generating random line number: jot command

Friday, March 25, 2016

Bash: Converting Percent-encoded String to Multibyte String

Command:

$ a='%E7%B9%94%E7%94%B0%E4%BF%A1%E9%95%B7'
$ echo -e "${a//%/\\x}"


Output:


織田信長

Wednesday, March 16, 2016

codesign: Code signing

Verifying a code signature of XLD.app:
$ codesign -dv --verbose=4 /Applications/XLD.app/
Output:
/Applications/XLD.app/: code object is not signed at all
Verifying a code signature of LadioCast.app:
$ codesign -dv --verbose=4 /Applications/LadioCast.app/
Output:
Executable=/Applications/LadioCast.app/Contents/MacOS/LadioCast Identifier=com.kawauso.LadioCast Format=bundle with Mach-O universal (i386 x86_64) CodeDirectory v=20200 size=865 flags=0x0(none) hashes=34+5 location=embedded Hash type=sha1 size=20 CDHash=469de51f853b25a595cfffa3b07ef121f1ab00f8 Signature size=8521 Authority=Developer ID Application: Yosirou Sawayanagi Authority=Developer ID Certification Authority Authority=Apple Root CA Timestamp=Feb 28, 2015, 1:12:47 AM Info.plist entries=25 TeamIdentifier=6SWQJR6TV2 Sealed Resources version=2 rules=12 files=32 Internal requirements count=1 size=216

Thursday, February 4, 2016

AWK: Number of Words

Printing the number of words in lines without newline:

$ awk 'BEGIN {words=0}{words+=NF} END { printf "%i", words} ./lines.txt

AWK: Number of Lines

print out number of lines without newline:

$ awk 'BEGIN {} END { printf "%i", NR }' ./lines.txt

Tuesday, February 2, 2016

AWK: Calling Bash Version of echo Command with \c Option

I don't know how to embed Bourne shell echo with \c option inside AWK script, so I did this way.

echo.bash

#!/bin/bash
for i; do
echo -n $i
done


AWK script using above echo.bash script, so you can call bash version of echo:

$ awk -F, '{
cmd="./echo.bash \""$2"\" | openssl dgst -sha256"
while (cmd | getline line){
print line
}
close(cmd)
}' ./test.txt

Monday, February 1, 2016

The Difference Between Bash and Bourne Shell When Using echo Command

Bash

$ echo -n "A. P. Balachandran" | md5
b9114dc37ab93677d533a0e3e025ee58

Bourne shell: Same value!

sh-3.2$ echo "A. P. Balachandran\c" | md5
b9114dc37ab93677d533a0e3e025ee58

Bourne shell: wrong md5 value!

sh-3.2$ echo -n "A. P. Balachandran" | md5
9e63b57c592863eaad63d37f72bce0d0

Bourne shell: -n option isn't working. That is why.

sh-3.2$ echo -n "A. P. Balachandran"
-n A. P. Balachandran

SHA-2: Command Comparison

Using command "shasum":
$ echo -n "test" | shasum -a 256
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -


Using command "openssl dgst":
$ echo -n "test" | openssl dgst -sha256
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08


Using command "openssl dgst" with option "hmac" with secret key:
$ echo -n "test" | openssl dgst -sha256 -hmac \"SECRET\"
c5790721d9fc3b84a46034c7b80d7b01d92f41a2847e67d17d8cc6cc97a6c7ad


Saturday, January 30, 2016

AWK: Appending Characters to Each Line

Appending '|' character to the end of each lines:

awk '{print $0"|"}' ./input.txt > ./output.txt

Friday, January 29, 2016

Cron: Tweet a Line Every 10-19 Minutes

Cron - Tweet a line every 30-40 mins:

*/10 * * * * perl -le 'sleep rand 540' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/lines.rb

lines.rb - using standard input:

require 'json'
require 'oauth'

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = STDIN.gets

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)

Getting Random Line Number -1

Bash line to generate random line number from 1 to max - 1.
echo $(ruby -e 'print rand') $(wc -l < "./lines.txt") | awk '{printf("%d\n", $1*$2)}'

Thursday, January 28, 2016

Generating Random Password

Generating 32-character random password

$ cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1

This is the same:

$ cat /dev/urandom | env LC_CTYPE=C tr -dc '[:alnum:]' | fold -w 32 | head -n 1

Generating Random 15-Character User Name

Generating random 15-character:
$ cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | fold -w 15 | head -n 1
This is the same:
$ $ cat /dev/urandom | env LC_CTYPE=C tr -cd '[:lower:][:digit:]' | fold -w 15 | head -n 1

Friday, May 31, 2013

Dumping Login Keychain Data

security dump-keychain -d ~/Library/Keychains/login.keychain

Sunday, January 27, 2013

Activating AirDrop

Type and enter following code in Terminal.

defaults write com.apple.NetworkBrowser BrowseAllInterfaces 1

Restart computer.