BASH Snippet


自動找出mediaserver的pid,並印meminfo和該pid的memory map
while [ true ]
do
  top -n 1 | grep mediaserver
  cat /proc/meminfo
  cat /proc/`ps | grep -i mediaserver | awk {'print $2'}`/maps
  echo ' '
  echo ' '
done

顯示指定檔案的size和md5
#!/bin/bash

if [ "$#" -lt 1 ] || ! [ -f "$1" ]
then
    echo 'should assian a valid file';
    exit -1;
fi

file=$1
if [ -f "$file" ]
then
    fsize=`ls -l $file | awk {'print $5'}`;
    md5val=`md5sum $file | awk {'print $1'}`;
    echo "filename=$file size=$fsize md5sum=$md5val";
else
    echo "$file not found."
    exit -1;
fi

顯示環境變數PATH中的各個目錄
#!/bin/bash

(IFS=':'
idx=0
for p in $PATH;
do
  echo "PATH[$idx]=$p"
  idx=$((idx+1))
done)

產生短版日期
#!/bin/bash
echo "$(date +%y%m%d)"
2016/8/24執行時會產生結果160824


印出目前主機的login使用者
w
w -h # ignore table header
w -s # short format
# show information
who
who -u # show login users
who -b # server last boot time


用ssh登入遠端主機時,查看我們登入所用的ip
echo $SSH_CLIENT | awk '{ print $1}'
w $USER | awk '{print $3}' | xargs | awk '{print $3}'

留言