bash脚本小记1

1. Bash中的数组
注意如果你头部使用了”#!/bin/sh”可能会不支持. 这个时候你需要改成”#!/bin/bash”
在bash中定义一个数组 myarray = (a b c)
里面每个元素之间用空格隔开.
计算当前数组的数量, ${#myarray[@]}
获取当前数组所以元素 ${myarray[@]}
或者其中某一个元素 ${myarray[index]} 例如第一个元素即${myarray[0]}

遍历数据

for el in "${myarray[@]}"; do echo "$el"; done 

1.1 数组转成字符串(相当于 join, concat这类js函数)

FOO=( a b c )
SAVE_IFS=$IFS
IFS=","
FOOJOIN="${FOO[*]}"
IFS=$SAVE_IFS
echo $FOOJOIN

2. Mysql
使用mysql命令行 将select的数据转成数据
只需要

MYSQL_BIN=`which mysql`
DUMPARGS="-u $DB_USER -p$DB_PASSWD --skip-triggers --compact --skip-extended-insert --no-create-info $DATABASE"
list=($($MYSQL_BIN $QUERY_ARGS 'SELECT aid FROM dede_addonarticle order by aid DESC LIMIT 0, 1000'))

echo ${#list[@]}
echo ${list[@]}

3 Mysqldump

mysqldump -u $DB_USER -p$DB_PASSWD --skip-triggers --compact --skip-extended-insert --no-create-info $DATABASE   table --where=""

Leave a Comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: