shell基础知识

来源:互联网 发布:关系数据库语言的特点 编辑:程序博客网 时间:2024/05/16 16:58

/////////////////////////////////////////////////////////////////////////////////////////////

如何用Shell命令从一个文件中读取数据到变量中

/////////////////////////////////////////////////////////////////////////////////////////////

     #!/bin/bash

    while read line

    do

    i=$line

    done<file                

      #file是要读的文件,行内容已经到i中

    #循环读文件中每行的内容赋到i变量中

 

 

 

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

用awk如何显示和读取文件的最后一行

/////////////////////////////////////////////////////////////////////////////////////////////

#在终端显示文件(filename为要显示的文件名)的最后一行 

awk '{a=$0} END{print a}' filename

 

#在终端显示文件(filename)的最后一行的某个域(n表示第几个域,从1开始)

awk '{a=$0} END{print $n}' filename

 

#读取文件最后一行的某个域(或整行)到变量variable

variable=`awk '{a=$0};END {print $n}' filename`

 

 

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

获取时间到变量中

/////////////////////////////////////////////////////////////////////////////////////////////

 

#获取时间到变量timenow中 ,并指定格式为HH:MM:SS

timenow=`date '+%H:%M:%S'`

 

#获取时间到变量TimeLastSecond中,获取是自1970 1.1 0:0开始的秒数

TimeLastSecond=`date '+%s'`

 

#更多关于时间的操作见date命令选项

 

#注意符号" ' "和" ` ",前者是双引号在英文输入法下的那个键,而后者是在英文输入法下的~那个键

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////  

用shell实现读取文件中每行各个字段的内容

/////////////////////////////////////////////////////////////////////////////////////////////

 while read line
      do
       t1=`echo "$line" | awk -F ',' '{print $1}'`
       t2=`echo "$line" | awk -F ',' '{print $2}'`
       t3=`echo "$line" | awk -F ',' '{print $3}'`
       echo $t1
       echo $t2
       echo $t3
done < text.txt

#-F ','指定每个字段的间隔符为"," 
 
 
//////////////////////////////////////////////////////////////////////////////
写的一个shell脚本
//////////////////////////////////////////////////////////////////////////////
#!/bin/sh
#
#==============================================================
#Description : This script is used to record thd terminal restart count,which
#                   testing the 128M NAND FLASH or other NAND FLASH
#                   U can change the directory and filename if need
#                   **Test nand flash**
#
#Author  : reille
#Created : 2009.6.5
#Version : V1.0
#==============================================================
#
#
###[START]###
#define the file directory
DIR=/mnt/update/
#DIR=/root/reille/
#define the file name
FILENAME=restart_count.txt
#restart counts
restart_count=0
#system time now
timenow=`date '+%H:%M:%S'`
#system time(seconds) now and last restart
TimeNowSecond=0
TimeLastSecond=0
#the interval between this restart and last restart
interval=0
cd $DIR
#read terminal restart count elapsed time from file, if file don't exit, it will creat the file and the restart count  #will set to '0'
if [ -e $FILENAME ]; then
    if [ -r $FILENAME ]; then    
         restart_count=`awk '{a=$0};END {print $1}' $FILENAME`
         TimeLastSecond=`awk '{a=$0};END {print $3}' $FILENAME`
         echo "last total restart count=$restart_count"
         echo "last system seconds=$TimeLastSecond"
         awk '{a=$0};END {print $4}' $FILENAME
         echo "======================================="
    fi
    else
         echo "First run and file don't exit, so create the file"
         restart_count=0
         TimeLastSecond=`date '+%s'`
         touch $FILENAME 
fi
 
#refresh terminal restart count and write it to the file!!
if [ -w $FILENAME ]; then
     restart_count=$(($restart_count + 1))
     echo "refresh restart count to $restart_count"
     temp=`date '+%s'`
     echo "current system seconds=$temp"
     interval=$(($temp - $TimeLastSecond))
     echo "Interval=$interval"
     echo -e "$restart_count `date '+%H:%M:%S'` `date '+%s'` $interval" >>$FILENAME
     echo "*********************************************"
fi
#
###[END]####
#
#
#