Author: jpuyy

  • shell下12个for循环例子

    12个例子写的很全,转自

    12 Bash For Loop Examples for Your Linux Shell Scripting

    基本格式1,用for in:

    for varname in list
    do
     command1
     command2
     ..
    done

    基本格式2,c语言形式:

    for (( expr1; expr2; expr3 ))
    do
    command1
    command2 ..
    done

    1. Static values for the list after “in” keyword直接在”in”后面接值

    In the following example, the list of values (Mon, Tue, Wed, Thu and Fri) are directly given after the keyword “in” in the bash for loop.在下面的例子中,变量为day,在Mon,Tue,Wed,Thu和Fri中取值。

    $ cat for1.sh
    i=1
    for day in Mon Tue Wed Thu Fri
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for1.sh
    Weekday 1 : Mon
    Weekday 2 : Tue
    Weekday 3 : Wed
    Weekday 4 : Thu
    Weekday 5 : Fri

    Caution: The list of values should not be separated by comma (Mon, Tue, Wed, Thu, Fri). The comma will be treated as part of the value. i.e Instead of “Mon”, it will use “Mon,” as value as shown in the example below.注意点:五个不同的值不能用逗号分隔,例如Mon,因为会自动把逗号算入到Mon之内。测试如下:

    $ cat for1-wrong1.sh
    i=1
    for day in Mon, Tue, Wed, Thu, Fri
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for1-wrong1.sh
    Weekday 1 : Mon,
    Weekday 2 : Tue,
    Weekday 3 : Wed,
    Weekday 4 : Thu,
    Weekday 5 : Fri

    Caution: The list of values should not be enclosed in a double quote. (“Mon Tue Wed Thu Fri”). If you enclose in double quote, it will be treated as a single value (instead of 5 different values), as shown in the example below.注意:五个值不可以用双引号括起来,这会被认为只有一个值

    $ cat for1-wrong2.sh
    i=1
    for day in "Mon Tue Wed Thu Fri"
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for1-wrong2.sh
    Weekday 1 : Mon Tue Wed Thu Fri

    2. Variable for the list after “in” keyword在”in”后面接变量

    Instead of providing the values directly in the for loop, you can store the values in a variable, and use the variable in the for loop after the “in” keyword, as shown in the following example.在使用前把值赋给变量,在”in”后面接变量。

    $ cat for2.sh
    i=1
    weekdays="Mon Tue Wed Thu Fri"
    for day in $weekdays
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for2.sh
    Weekday 1 : Mon
    Weekday 2 : Tue
    Weekday 3 : Wed
    Weekday 4 : Thu
    Weekday 5 : Fri

    Caution: As a best practice, you should always quote the bash variables when you are referring it. There are few exceptions to this best practice rule. This is one of them. If you double quote the variable in this for loop, the list of values will be treated as single value. Lot of people fall into this trap. Be careful and do not double quote your variable in the for loop.注意:作为一个良好的习惯,请每次都将值用变量来代替。这里的变量不能用双引号括起来,它会将其视为一个值。测试如下:

    $ cat for2-wrong.sh
    i=1
    weekdays="Mon Tue Wed Thu Fri"
    for day in "$weekdays"
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for2-wrong.sh
    Weekday 1 : Mon Tue Wed Thu Fri

    3. Don’t specify the list; get it from the positional parameters不指定列表

    If you don’t specify the keyword “in” followed by any list of values in the bash for loop, it will use the positional parameters (i.e the arguments that are passed to the shell script).如果in中不加变量,则在执行时加上即可,例如:

    $ cat for3.sh
    i=1
    for day
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for3.sh Mon Tue Wed Thu Fri
    Weekday 1 : Mon
    Weekday 2 : Tue
    Weekday 3 : Wed
    Weekday 4 : Thu
    Weekday 5 : Fri

    Caution: Please be careful if you use this method. You should not include the keyword “in” in the for loop. If you leave the keyword “in” without any values, it will not use the positional parameter as shown below. It will not go inside the loop. i.e for loop will never get executed as shown in the example below.使用这种方法一定要小心,保证 in 这个字符都不出现,下面的例子就是没有输出任何信息。

    $ cat for3-wrong.sh
    i=1
    for day in
    do
     echo "Weekday $((i++)) : $day"
    done
    
    $ ./for3-wrong.sh Mon Tue Wed Thu Fri

    Note: Refer to our earlier article to understand more about bash positional parameters.可以查看bash位置参数。

    4. Unix command output as list values after “in” keyword以命令的输出做为列表

    You can use the output of any UNIX / Linux command as list of values to the for loop by enclosing the command in back-ticks ` ` as shown below.使用反引号将要执行的命令引起来,可使结果作为列表。

    $ cat for4.sh
    i=1
    for username in `awk -F: '{print $1}' /etc/passwd`
    do
     echo "Username $((i++)) : $username"
    done
    
    $ ./for4.sh
    Username 1 : ramesh
    Username 2 : john
    Username 3 : preeti
    Username 4 : jason
    ..

    5. Loop through files and directories in a for loop使用当前目录或文件做列表

    To loop through files and directories under a specific directory, just cd to that directory, and give * in the for loop as shown below.定位到指定目录下,用*代替列表。

    The following example will loop through all the files and directories under your home directory.下面的例子使用了/home目录。

    $ cat for5.sh
    i=1
    cd ~
    for item in *
    do
     echo "Item $((i++)) : $item"
    done
    
    $ ./for5.sh
    Item 1 : positional-parameters.sh
    Item 2 : backup.sh
    Item 3 : emp-report.awk
    Item 4 : item-list.sed
    Item 5 : employee.db
    Item 8 : storage
    Item 9 : downloads

    Usage of * in the bash for loop is similar to the file globbing that we use in the linux command line when we use ls command (and other commands).上面的例子显示类似于ls的执行结果。

    For example, the following will display all the files and directories under your home directory. This is the concept that is used in the above for5.sh example.

    cd ~
    ls *

    The following will display all the *.conf file that begins with either a, b, or, c or d under /etc directory.下面显示以a,b,c或d开头,以.conf结尾的文件

    $ ls -1 /etc/[abcd]*.conf
    /etc/asound.conf
    /etc/autofs_ldap_auth.conf
    /etc/cas.conf
    /etc/cgconfig.conf
    /etc/cgrules.conf
    /etc/dracut.conf

    The same argument that is used in the ls command above, can be used in a bash for loop, as shown in the example below.上面的可直接作为列表

    $ cat for5-1.sh
    i=1
    for file in /etc/[abcd]*.conf
    do
     echo "File $((i++)) : $file"
    done
    
    $ ./for5-1.sh
    File 1 : /etc/asound.conf
    File 2 : /etc/autofs_ldap_auth.conf
    File 3 : /etc/cas.conf
    File 4 : /etc/cgconfig.conf
    File 5 : /etc/cgrules.conf
    File 6 : /etc/dracut.conf

    6. Break out of the for loop(for循环中的break)

    You can break out of a for loop using ‘break’ command as shown below.可以使用如下的方式,中断for循环。

    $ cat for6.sh
    i=1
    for day in Mon Tue Wed Thu Fri
    do
     echo "Weekday $((i++)) : $day"
     if [ $i -eq 3 ]; then
       break;
     fi
    done
    
    $ ./for6.sh
    Weekday 1 : Mon
    Weekday 2 : Tue

    7. Continue from the top of the for loop(for循环中的continue)

    Under certain conditions, you can ignore the rest of the commands in the for loop, and continue the loop from the top again (for the next value in the list), using the continue command as shown below.在有些情况下,可以忽略某些之后再回到循环中,使用continue即可做到。

    The following example adds “(WEEKEND)” to Sat and Sun, and “(weekday)” to rest of the days.当循环遇到i=7 或i=8时,显示WEEKEND,然后继续。

    $ cat for7.sh
    i=1
    for day in Mon Tue Wed Thu Fri Sat Sun
    do
     echo -n "Day $((i++)) : $day"
     if [ $i -eq 7 -o $i -eq 8 ]; then
       echo " (WEEKEND)"
       continue;
     fi
     echo " (weekday)"
    done
    
    $ ./for7.sh
    Day 1 : Mon (weekday)
    Day 2 : Tue (weekday)
    Day 3 : Wed (weekday)
    Day 4 : Thu (weekday)
    Day 5 : Fri (weekday)
    Day 6 : Sat (WEEKEND)
    Day 7 : Sun (WEEKEND)

    8. Bash for loop using C program syntax使用C语言for循环语法

    This example uses the 2nd method of bash for loop, which is similar to the C for loop syntax. The following example generates 5 random number using the bash C-style for loop.在文章开头有使用c语言的for循环,下面的例子生成出1-5个数字

    $ cat for8.sh
    for (( i=1; i <= 5; i++ ))
    do
     echo "Random number $i: $RANDOM"
    done
    
    $ ./for8.sh
    Random number 1: 23320
    Random number 2: 5070
    Random number 3: 15202
    Random number 4: 23861
    Random number 5: 23435

    9. Infinite Bash for loop无限循环

    When you don’t provide the start, condition, and increment in the bash C-style for loop, it will become infinite loop. You need to press Ctrl-C to stop the loop.当不设定起始,条件和步长的时候,会变为无限循环,只能通过Ctrl-C来停止。

    $ cat for9.sh
    i=1;
    for (( ; ; ))
    do
       sleep $i
       echo "Number: $((i++))"
    done

    Like we said above, press Ctrl-C to break out of this bash infinite for loop example.

    $ ./for9.sh
    Number: 1
    Number: 2
    Number: 3

    10. Using comma in the bash C-style for loop在c语言类似的循环中使用逗号

    In the bash c-style loop, apart from increment the value that is used in the condition, you can also increment some other value as shown below.在c语言类似的循环中,可以将增长和变量分开。

    In the initialize section, and the increment section of the bash C-style for loop, you can have multiple value by separating with comma as shown below.

    The following for loop is executed a total of 5 times, using the variable i. However the variable j start with 5, and getting increment by 5 every time the loop gets executed.下面的例子将执行5次,但是目标是改变j的值,每一次加5

    $ cat for10.sh
    for ((i=1, j=10; i <= 5 ; i++, j=j+5))
    do
     echo "Number $i: $j"
    done
    
    $ ./for10.sh
    Number 1: 10
    Number 2: 15
    Number 3: 20
    Number 4: 25
    Number 5: 30

    11. Range of numbers after “in” keyword在in列表中使用数字区间

    You can loop through using range of numbers in the for loop “in” using brace expansion.

    The following example loops through 10 times using the values 1 through 10.下面的例子取值1到10,用大括号括起来。

    $ cat for11.sh
    for num in {1..10}
    do
     echo "Number: $num"
    done
    
    $ ./for11.sh
    Number: 1
    Number: 2
    Number: 3
    Number: 4
    Number: 5
    ...

    12. Range of numbers with increments after “in” keyword在in列表中使用数字区间,并以2做为步长。

    The following example loops through 5 times using the values 1 through 10, with an increment of 2. i.e It starts with 1, and keeps incrementing by 2, until it reaches 10.

    $ cat for12.sh
    for num in {1..10..2}
    do
     echo "Number: $num"
    done
    
    $ ./for12.sh
    Number: 1
    Number: 3
    Number: 5
    Number: 7
    Number: 9

    12个for循环的例子很基础很规范,多看优质代码才能更快成长。

  • ubuntu升级发行版本的两个方法

    1.使用菜单中的update-manager,或者 Alt+F2输入

    sudo update-manager -d

    2.完全使用命令

    sudo apt-get update

    安装update-manager-core软件包:

    apt-get install update-manager-core

    查看/etc/update-manager/release-upgrades文件:

    cat /etc/update-manager/release-upgrades

    确保里面包含了Prompt=normal

    然后运行如下命令启动系统升级:

    do-release-upgrade
  • pptpsetup命令行连接pptp vpn | 拨号

    一般会自带pptpsetup这个工具

    没有的话

    apt-get install pptp-linux

    添加一个vpn连接,执行

    pptpsetup -create vpnname -server xxx.xxx.xxx.xxx -username username -password passwd -encrypt (记得改一下)

    PS:更多选项运行”pptpsetup –help”

    这样其实是写入了/etc/ppp/peers/ 这个目录下面(网上有在这个目录新建文件的方法,看来不如这个方便)

    /etc/ppp/chap-secrets 用户名密码写入到了这个文件

    上线:

    pon vpnname

    下线:

    poff vpnname (全部下线poff -a)

    然后ifconfig查看一下多了ppp0这个网络

    现在还不能通过vpn来走

    因为并不知道数据该怎么走,很明显要配一下路由

    #route 查看现在的默认路由,下面两条操作将默认路由改为vpn的地址,删除原来的默认路由

    route add default gw 10.19.1.1
    route delete default gw xxx.xxx.xxx.yyy

    或者这样更简便

    route del default
    route add default dev ppp0

    这样就能通过vpn来做事了。

     

  • debian|linux 路由

    路由是什么

    路由routing)就是通过互联的网络信息从源地址传输到目的地址的活动。

    http://zh.wikipedia.org/wiki/%E8%B7%AF%E7%94%B1

    linux显示路由表

    #route 或

    # netstat -r

    内核 IP 路由表
    目标            网关            子网掩码        标志  跃点   引用  使用 接口
    into.dyndns.org *              255.255.255.255 UH    0      0        0 eth0
    into.dyndns.org *              255.255.255.255 UH    0      0        0 eth0
    10.19.1.1       *              255.255.255.255 UH    0      0        0 ppp0
    219.230.xxx.xxx *              255.255.255.192 U     1      0        0 eth0
    link-local      *              255.255.0.0     U     1000   0        0 eth0
    default         *              0.0.0.0         U     0      0        0 ppp0

    # route -n                #按照越精确越靠前。这个是路由的排序规则

    默认路由

    ip route add default dev eth0 via 192.168.0.254          #添加默认路由
    ip route del default dev eth0 via 192.168.0.254           #删除默认路由

    主机路由

    ip route add dev eth0 10.0.0.1/32                                 #添加主机路由
    ip route del dev eth0 10.0.0.1/32                                  #删除主机路由

    网段路由

    ip route add dev eth0 192.168.0.0/16                           #添加网段路由
    ip route del dev eth0 192.168.0.0/16                            #删除网段路由

    route add -net 目标网段 netmask 掩码 dev eth0

    route del -net 目标网段 netmask 掩码 dev eth0

  • shell操作mysql一则 | sql update 语句

    背景,打算把radius里有几个用户在凌晨没有人上的时候设为disable,需要用到update语句和at命令

    首先在mysql里创建了一个新的帐号,用户名rrr 密码ttt

    要操作的数据库为 radius ,操作函数mysql_opt ,操作表radusergroup

    #!/bin/bash
    #for use:disable the radius users
    #author: jpuyy.com
    #version:
    #history:
    #relate on: at mysql|update
    sql_user=rrr 
    sql_pass=ttt
    sql_opt="-u$sql_user -p$sql_pass -s"
    
    mysql_opt() {
    db_name=$1
    sql_lan=$2
    mysql $sql_opt << EOF
    USE $db_name;
    $sql_lan;
    QUIT
    EOF
    }
    
    data=`mysql_opt radius "UPDATE  radusergroup SET groupname='daloRADIUS-Disabled-Users'  WHERE username in ('aaa','bbb','ccc')"`
    echo $data
  • Debian操作系统编译内核

    转自:

    想用上最新的内核,就需要我们自己去编译新内核了

    安装需求软件包

    apt-get install kernel-package libncurses5-dev fakeroot wget build-essential

    下载新内核

    你可以在这里查看可用的内核 目前最新的为2.6.x

    linux kernel 官方网站:http://www.kernel.org

    cd /usr/src
    wget http://www.eu.kernel.org/pub/linux/kernel/v2.6/linux-2.6.26.tar.gz

    解压 创建软链接,切换到软链接中操作

    tar zxvf linux-2.6.26.tar.gz
    ln -s linux-2.6.26 linux
    cd /usr/src/linux

    生成内核文件,首先要复制旧内核的配置文件

    make clean && make mrproper
    cp /boot/config-`uname -r` ./.config
    make menuconfig

    使用下面的步骤来载入旧的内核配置
    1. Load an Alternate Configuration File
    2. Type .config if not already in the textbox
    3. Ok
    4. Do any changes to the kernel modules
    5. Click Exit
    6. Click Yes when asked if you want to save

    编译内核

    make-kpkg clean
    fakeroot make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers

    这个过程需要很长时间

    安装新内核

    ls -l /usr/src

    到这里,还没有安装内核,
    dpkg -i linux-headers-2.6.26-custom_2.6.26-custom-10.00.Custom_i386.deb
    dpkg -i linux-image-2.6.26-custom_2.6.26-custom-10.00.Custom_i386.deb

    最后重新启动服务器即可

    reboot