首页 > 代码库 > sheel基础

sheel基础

基础教程
http://www.k4.dion.ne.jp/~mms/unix/shellscript/shell_sample.html


cat /dev/null > abc.log (clear the file of abc.log)
$ cat filename (show the contennce of the file)
$ cat > filename (create a new file use the keyboard)
$cat file1 file2 > file (merge the tow files into file file)
cp -p abc.log abc.log.bak (copy abc.log‘s contence ,and paste into abc.log.bak)
gzip abc.log (zip the fil)
#! bin/sh (you must whrite it in the front of the shell, it means that the program after this will be excuted)
chmod a=rwx file (u:user; g:group; o:others; a:ugo)
chmod 777 file (the same with last one)


$# 指参数的个数
$? 指最近一次执行命令的返回值
$1 指第一个参数; $2 指第二个参数
basename 从参数指定的path中抽取文件名 例 basename $0 获得shell自身的文件名; basename ${filepath}

例子
# --共通関数定義--
# ログ出力関数
LOG()
{
  # ログ出力先
  LOG_DIR=./

  # 引数展開
  FILENM=`basename $0`
  MSG=$1

  # 変数定義
  LOG_DATE=`date ‘+%Y-%m-%d‘`
  LOG_TIME=`date ‘+%H:%M:%S‘`
  LOGFILE="${LOG_DIR}${LOG_DATE}_`basename $0 .sh`.log"

  # ログ出力実行
  printf "%-10s %-8s %-14s %-50s\n" \
  "${LOG_DATE}" "${LOG_TIME}" "${FILENM}" "${MSG}" >>${LOGFILE}
}
# 年月取得関数
GetYM()
{
  SYSTEM_MONTH=`date ‘+%Y%m‘`
  echo ${SYSTEM_MONTH}
}
# 年月日取得関数
GetYMD()
{
  SYSTEM_DATE=`date ‘+%Y%m%d‘`
  echo ${SYSTEM_DATE}
}

sheel基础