linux shell

1. 获取IP:

Linux:

<span class="re2"><span style="color:#007800;">LC_ALL=</span></span>C ifconfig  | <span class="kw2"><strong><span style="color:#c20cb9;">grep</span></strong></span> <span class="st0"><span style="color:#ff0000;">'inet addr:'</span></span>| <span class="kw2"><strong><span style="color:#c20cb9;">grep</span></strong></span> -v <span class="st0"><span style="color:#ff0000;">'127.0.0.1'</span></span> |
<span class="kw2"><strong><span style="color:#c20cb9;">cut</span></strong></span> -d: -f2 | <span class="kw2"><strong><span style="color:#c20cb9;">awk</span></strong></span> <span class="st0"><span style="color:#ff0000;">'{ print $1}'</span></span>

FreeBSD/OpenBSD:

<span class="re2"><span style="color:#007800;">LC_ALL=</span></span>C ifconfig  | <span class="kw2"><strong><span style="color:#c20cb9;">grep</span></strong></span> -E <span class="st0"><span style="color:#ff0000;">'inet.[0-9]'</span></span> | <span class="kw2"><strong><span style="color:#c20cb9;">grep</span></strong></span> -v <span class="st0"><span style="color:#ff0000;">'127.0.0.1'</span></span> |
<span class="kw2"><strong><span style="color:#c20cb9;">awk</span></strong></span> <span class="st0"><span style="color:#ff0000;">'{ print $2}'</span></span>

Solaris:

<span class="re2"><span style="color:#007800;">LC_ALL=</span></span>C ifconfig -a | <span class="kw2"><strong><span style="color:#c20cb9;">grep</span></strong></span> inet | <span class="kw2"><strong><span style="color:#c20cb9;">grep</span></strong></span> -v <span class="st0"><span style="color:#ff0000;">'127.0.0.1'</span></span> |
<span class="kw2"><strong><span style="color:#c20cb9;">awk</span></strong></span> <span class="st0"><span style="color:#ff0000;">'{ print $2}'</span></span>


#!/bin/sh
# Shell script scripts to read ip address
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Get OS name
OS=`uname`
IO="" # store IP
case $OS in
   Linux) IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
   FreeBSD|OpenBSD) IP=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
   SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
   *) IP="Unknown";;
esac
echo "$IP"


2.  test substr

    str='this is a tree! and that is a car.'
//如果包含this
[[ $str =~ "this" ]] && echo "\$str contains this"
//如果不包含that
[[ $str =~ "that" ]] || echo "\$str does NOT contain that"


3. random:

[chengmo@centos5  shell]$ date +%s
1287764773
#获得时间戳,当前到:1970-01-01 00:00:00 相隔的秒数
#如果用它做随机数,相同一秒的数据是一样的。在做循环处理,多线程里面基本不能满足要求了。
 
[chengmo@centos5  shell]$ date +%N
738710457
#获得当前时间的纳秒数据,精确到亿分之一秒。
#这个相当精确了,就算在多cpu,大量循环里面,同一秒里面,也很难出现相同结果,不过不同时间里面还会有大量重复碰撞
 
[chengmo@centos5  shell]$ date +%s%N
1287764807051101270
#这个可以说比较完美了,加入了时间戳,又加上了纳秒


#!/bin/sh
 
#写个随机函数,调用方法random min max
#在min 与 max直接获得随机整数
#copyright chengmo QQ:8292669
 
 
#获得随机数返回值,shell函数里算出随机数后,更新该值
function random()
{
     min=$1;
     max=$2-$1;
     num=$( date +%s+%N);
     ((retnum=num%max+min));
     #进行求余数运算即可
     echo $retnum;
     #这里通过echo 打印出来值,然后获得函数的,stdout就可以获得值
     #还有一种返回,定义全价变量,然后函数改下内容,外面读取
}
 
#得到1-10的seq数据项
for i in {1..10};
do 
     out=$(random 2 10000);
     echo $i, "2-10000" ,$out;
done ;

echo $RANDOM


[chengmo@centos5  shell]$ head -1 /dev/urandom
ãņù…•KTþçanVÕã¹Û&¡õ¾“ô2íùU“ žF¦_ ÿ”†mEðûUráÏ=J¯TŸA•ÌAÚRtÓ
 
#读一行,怎么是乱码呢?其实它是通过二进制数据保存实时数据的,那么我们怎么样把它变成整型数据呢?
 
 
[chengmo@centos5 ~ /shell ]$ head -200 /dev/urandom | cksum
1615228479 50333
#由于urandom的数据是非常多,不能直接通过cat读取,这里取前200行,其实整个数据都是变化的,取多少也一样是唯一的。
#cksum 将读取文件内容,生成唯一的表示整型数据,只有文件内容不变,生成结果就不会变化,与php crc函数
 
[chengmo@centos5  shell]$ head -200 /dev/urandom | cksum | cut -f1 -d " "
484750180
#cut 以” “分割,然后得到分割的第一个字段数据

[chengmo@centos5 ~ /shell ]$ cat /proc/sys/kernel/random/uuid
dff68213-b700-4947-87b1-d9e640334196
[chengmo@centos5 ~ /shell ]$ cat /proc/sys/kernel/random/uuid
7b57209a-d285-4fd0-88b4-9d3162d2e1bc
#连续2次读取,得到的uuid是不同的
 
[chengmo@centos5 ~ /shell ]$ cat /proc/sys/kernel/random/uuid | cksum | cut -f1 -d " "
2141807556
#同上方法得到随机整数










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值