13 Jun

shell中的几个循环语法

分类:电脑技术 » linux shell   出处:本站原创   
比如生成1~100的循环体

index=1
while [ $index -le 100 ]
do

。。。
index=$(($index +1))
done


#!/bin/bash
while :;do
    ((++index))
    echo $index
    ((index==100))&&break
done


for i in {1..100}
  do
   .......
  done


min=1
max=100
while [ $min -le $max ]
    do
       echo $min
        min=`expr $min + 1`
done


seq 1 100


printf '%d\n' {1..100}
Tags: ,
13 Jun

Linux主要shell命令详解

分类:电脑技术 » linux shell   出处:本站原创   
shell是用户和Linux操作系统之间的接口。Linux中有多种shell,其中缺省使用的是Bash。本章讲述了shell的工作原理,shell的种类,shell的一般操作及Bash的特性。

 什么是shell
Tags: ,
13 Jun

sed技巧

分类:电脑技术 » linux shell   出处:本站原创   
Sed:
1、删除行首空格
   sed 's/^[ ]*//g' filename
   sed 's/^ *//g' filename
   sed 's/^[[:space:]]*//g' filename

2、行后和行前添加新行
   行后:sed 's/pattern/&\n/g' filename
   行前:sed 's/pattern/\n&/g' filename
   &代表pattern
Tags: ,
6 Jun

linux日期函数使用技巧

分类:电脑技术 » linux shell   出处:本站原创   
上午在群里cu-nosmoking提出一个问题,涉及到date函数的一些使用技巧,顺便记录一下。
cu-nosmoking(122121234) 10:16:39
各位都在吧?我問個問題先,我想每天將 files(當前日期“變量”).txt  複製為  files(當前日期的前兩天“變量”).txt  該如何通過命令實現?
比如: files20080606.txt 複製為files20080604.txt
到了明天命令可以自動將
files20080607.txt 複製為files20080605.txt

望牛人指教!!!!!!!!!

解决方法:

cp files`date +20%y%m%d`.txt files`date -d '2 days ago' +20%y%m%d`.txt

上面 "+20%y%m%d" 也可以写成 "+%Y%m%d" 。

date使用技巧还有:
date -d yesterday %Y%m%d` (昨天)
date -d '-2 day' +%Y%m%d (2天前)
date -d '3 days ago' (3天前)



如果要自动拷贝当前目录下类似文件(包括子目录),该如何写呢?

我写了个脚本,时间仓促,写的比较毛糙:grin

root@MyLFS:~/kevin# ls
aa20080606.txt  bbb20080606.txt  binlist.txt  ccc20080606.txt  oye*
root@MyLFS:~/kevin# cat oye
#!/bin/bash
# write for nosmoking
# code by thatday

curdate=`date +%Y%m%d`
newdate=`date -d '-2 day' +%Y%m%d`

find ./ -name '*'$curdate.txt > tmp

cat tmp|while read name; do
    have=`echo $name | grep $curdate`
    if [ ! "$have" = "" ]; then
      newname=`echo $name | sed 's/'$curdate'/'$newdate'/g'`
      echo "cp $name to $newname"
      cp $name $newname
    fi
done

rm -f tmp

echo "Oye! done..."

Tags: ,
19 May

Lsof命令详解

分类:电脑技术 » linux shell   出处:赛迪网   
一般root用户才能执行lsof命令,普通用户可以看见/usr/sbin/lsof命令,但是普通用户执行会显示“permission denied”

总结一下lsof指令的用法:

lsof abc.txt 显示开启文件abc.txt的进程

lsof -i :22 知道22端口现在运行什么程序

lsof -c abc 显示abc进程现在打开的文件

lsof -g gid 显示归属gid的进程情况

lsof +d /usr/local/ 显示目录下被进程开启的文件

lsof +D /usr/local/ 同上,但是会搜索目录下的目录,时间较长
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]