shell基础知识
2009-06-09 10:58
337 查看
/////////////////////////////////////////////////////////////////////////////////////////////
如何用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]####
#
#
#
如何用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]####
#
#
#
相关文章推荐
- Linux小白日记(三)shell基础知识篇
- Shell编程——bash基础知识
- shell基础知识
- shell基础知识梳理二
- shell基础知识梳理二
- shell基础知识学习
- 学习 shell脚本之前的基础知识
- Shell脚本基础知识
- shell脚本的基础知识1:基本脚本的编译
- 学习 shell脚本之前的基础知识
- 学习 shell脚本之前的基础知识
- Shell脚本基础知识详细介绍
- 学习 shell脚本之前的基础知识
- Shell程序设计(一)—基础知识
- Shell基础知识
- L25 shell基础知识(三)
- shell 基础知识
- Linux基础-shell脚本知识整理和脚本编写----------变量、运算符、流程控制、函数、计划任务(发送邮件)
- shell基础知识总结
- shell基础知识