顯示文章

這裡允許您檢視這個會員的所有文章。請注意, 您只能看見您有權限閱讀的文章。


文章 - wenlien

頁: [1] 2 3 4
1
Got it.

因為我要定期將command history export 出來.
看來還是只能用我寫的 script 撐著先~

Thanks for your reply.  :D

2
目前只好先個替代script撐著先~ >:(

代碼: [選擇]
[admin@Stanley tmp]$cat ./t.sh
#!/bin/bash
cat ~/.bash_history | while read l
do
  echo $l | grep "^#[0-9]" > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    timestamp=${l#*#}
  else
    datetime=`date -d "@$timestamp" "+[%F %T %Z] "`
    echo $datetime $l
  fi
done

引用
[admin@Stanley tmp]$./t.sh  | tail -5
[2013-08-14 02:47:23 UTC] sl -al
[2013-08-14 02:47:25 UTC] vi ./t.sh
[2013-08-14 02:47:36 UTC] ./t.sh
[2013-08-14 02:47:42 UTC] ./t.sh | tail -10
[2013-08-14 02:47:52 UTC] ./t.sh | tail -6

3
請問Yamaka:

你也是寫成shell script後來執行嗎?

因為在我的環境中,
如果在bash prompt中執行是正常的, 而一但寫成script再執行就錯了~ 真奇怪!  ???

4
Hi all:
我想要在shell script 上取得bash歷史指令+時間戳記,
遇到了一些問題,並從網路上找了一些方法。
但是還是有個地方解不開,還請大家不吝指導:

代碼: [選擇]
[admin@Stanley tmp]$cat /tmp/t.sh
#!/bin/bash
export HISTTIMEFORMAT="[%F %T %Z]"
export HISTFILE=~/.bash_history
set -o history
history

引用
== 執行結果 (Prefix的timestamp都是 current time, 但在history內容中, 是有正確的timestamp) ==
[admin@Stanley tmp]$/tmp/t.sh | tail -20
  455  [2013-08-14 02:14:55 UTC]#1376446410
  456  [2013-08-14 02:14:55 UTC]cd /tmp
  457  [2013-08-14 02:14:55 UTC]#1376446412
  458  [2013-08-14 02:14:55 UTC]vi ./t.sh
  459  [2013-08-14 02:14:55 UTC]#1376446422
  460  [2013-08-14 02:14:55 UTC]chmod u+x ./t.sh
  461  [2013-08-14 02:14:55 UTC]#1376446424
  462  [2013-08-14 02:14:55 UTC]./t.sh
  463  [2013-08-14 02:14:55 UTC]#1376446432
  464  [2013-08-14 02:14:55 UTC]history | more
  465  [2013-08-14 02:14:55 UTC]#1376446439
  466  [2013-08-14 02:14:55 UTC]./t.sh | tail -10
  467  [2013-08-14 02:14:55 UTC]#1376446445
  468  [2013-08-14 02:14:55 UTC]history | tail -10
  469  [2013-08-14 02:14:55 UTC]#1376446445
  470  [2013-08-14 02:14:55 UTC]history | tail -10
  471  [2013-08-14 02:14:55 UTC]#1376446479
  472  [2013-08-14 02:14:55 UTC]history | tail -10
  473  [2013-08-14 02:14:55 UTC]#1376446486
  474  [2013-08-14 02:14:55 UTC]/tmp/t.sh | tail -10

引用
== 預期結果 ==
[admin@Stanley tmp]$history | tail -10
  515  [2013-08-14 02:14:30 UTC] history -a
  516  [2013-08-14 02:13:09 UTC] cat /tmp/t.sh
  517  [2013-08-14 02:13:30 UTC] cd /tmp
  518  [2013-08-14 02:13:32 UTC] vi ./t.sh
  519  [2013-08-14 02:13:42 UTC] chmod u+x ./t.sh
  520  [2013-08-14 02:13:44 UTC] ./t.sh
  521  [2013-08-14 02:13:52 UTC] history | more
  522  [2013-08-14 02:13:59 UTC] ./t.sh | tail -10
  523  [2013-08-14 02:14:05 UTC] history | tail -10
  524  [2013-08-14 02:14:39 UTC] history | tail -10

請問我少了什麼東西呢?

My Environment:
[admin@Stanley tmp]$bash --version
GNU bash, version 4.1.0(2)-release

感謝~

5
我照wenlien大大所說把script作了測試

#!/bin/tcsh
/tools/bin/pgp -e sss mykey << EOF
y
EOF

以及
#!/bin/tcsh
echo y | /tools/bin/pgp -e sss mykey

執行後還是會停住要我輸入(y/N)

Pretty Good Privacy(tm) Version 6.5.8
(c) 1999 Network Associates Inc.
Uses the RSAREF(tm) Toolkit, which is copyright RSA Data Security, Inc.
Export of this software may be restricted by the U.S. government.

Recipients' public key(s) will be used to encrypt.

Key for user ID: mykey <mykey@hotmail.com.tw>
1024-bit DSS key, Key ID 0x0C892DA2, created 2008/04/08
WARNING:  Because this public key is not certified with a trusted
signature, it is not known with high confidence that this public key
actually belongs to: "mykey <mykey@hotmail.com.tw>".

Are you sure you want to use this public key (y/N)?

如果這樣不行,那你可能要試試看expect.
給你一個link去研究研究吧~
http://lists.netisland.net/archives/plug/plug-1999-09/msg00099.html
wish you good luck!

6
試試看:
echo y | /tools/bin/pgp -e sss mykey

/tools/bin/pgp -e sss mykey << EOF
y
EOF

7
Hi all:

一般我們在deploy war檔到tomcat時,都會放在webapps下。
想請問一下,可以不要放在webapps下嗎?
有沒有什麼參數可以support這個?

Thanks~

8
trigger + event ?.....

or use cron + command:

[in crontab]
0 0 1 1 * psql < /t.sql

[in /t.sql]
ALTER SEQUENCE serial RESTART WITH 1;

9
wenlien大,
你交代的任務先完成一部分,會再接下去繼續...  ;)
lol
Bunko

Hi Bunko大大:
感謝你的分享,應該請版主大大來把這篇文章至頂才是~

看過你的Stored Procedure之後,突然發現一個小弟之前沒思考周慮到的地方:
1. 權限控管的部份,分成五個在做控管:
user, db, tables_priv, columns_priv, procs_priv.
http://dev.mysql.com/doc/refman/5.1/en/grant-table-structure.html
所以,要確定裡面都沒有insert的權限,才可以避免user insert data。
如果可以當然是從程式下手,已達到完美,
再不然,我會建議,從管理的角度下手,儘量使用單一的設定權限方法(資料表)來作。
以你的程式為例,只控管mysql.db這個資料表。

2. 你在procedure中,直接使用update。
代碼: [選擇]
...
UPDATE mysql.db SET Insert_Priv='N',Create_priv='N' where db=ctrldb;
...
UPDATE mysql.db SET Insert_Priv='Y',Create_priv='Y' where db=ctrldb;
...
如果考慮到權限控管,不見得一開始每一個user都有insert的權限。
ex.
User 1: select/insert/update table A.
User 2: select table A only.

所以,如果可以有一個資料表將最原始的權限都記錄下來,就更完美了~ (我覺得這個應該可以算是optional)
ex.
代碼: [選擇]
[create backup permission table]
-- create permission table:
create table test.db select * from mysql.db where user is NULL;

[setting insert block]
-- backup permission.
insert into test.db select * from mysql.db where db='test';
-- update permission.
update mysql.db set ...;
-- flush privileges.
flush privileges;

[release insert block]
-- stored permission.
delete from mysql.db where db='test';
insert into mysql.db select * from test.db where db='test';
-- delete backup permission.
delete from test.db where db='test';
-- flush privileges.
flush privileges;


此外,小弟發現你有一個計算size的function,是用cursoe來實作。
利用cursor應該會耗用較多的資源,且如果將來有人要接手source code,
其門檻也會變高,所以,我認為用一般的select就可以達到你要的結果:
代碼: [選擇]
DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`dbsize`$$
CREATE FUNCTION `test`.`dbsize` (in_dbname varchar(64)) RETURNS BIGINT UNSIGNED
BEGIN
DECLARE rtnSize BIGINT UNSIGNED;
select sum(data_length)+sum(index_length) into rtnSize
   from information_schema.tables
   where table_schema = in_dbname
   and table_type = 'BASE TABLE'
   and engine = 'MyISAM';

RETURN rtnSize;

END$$

DELIMITER ;

最後,再感謝一次Bunko大大,並期待你後續的分享~  :)

10
引用
但是,如果DB換帳號密碼,那php裡面的設定就要一定改~
所以,可以把邏輯寫成Stored Procedure,然後用MySQL的event來執行。
上述的問題就可以解決!

1, 請問php 那部份需要改 ?
2, 如何可以寫成Stored Procedure ?

謝謝!

1. change here:
代碼: [選擇]
/*
 * Settings
 */
 
$mysql_host  = 'localhost';
$mysql_user  = 'root'; // Do NOT change, root-access is required
$mysql_pass  = '';
$mysql_db    = 'quotadb'; // Not the DB to check, but the db with the quota table
$mysql_table = 'quota';

2.  第二問題,恕小弟看不懂你的問題,
     a. 邏輯的部份,請參閱php source code.
     b. 建立Stored Procedure的語法, 請參閱MySQL Manual.
http://dev.mysql.com/doc/refman/5.1/en/stored-programs-views.html

11
就是在建 table 時設好每個欄位大小

不然就用下面的方法

http://blog.wu-boy.com/2009/10/09/1716/


這個方法讚~
但是,如果DB換帳號密碼,那php裡面的設定就要一定改~
所以,可以把邏輯寫成Stored Procedure,然後用MySQL的event來執行。
上述的問題就可以解決!

另一個問題時,這樣的狀況,會有lag。(端看schedule設定的時間長短而定)
再來就是permission何時生效的問題了~
http://dev.mysql.com/doc/refman/5.1/en/privilege-changes.html
如果,如果是放進mysql.db的設定,要等到下次user 使用'use db_name'(或是重新連線)的時候,
權限才會生效。

(我猜,並沒有試過,有興趣的人可以試試看,再請跟大家分享)
我猜,以下的方式(延續之前quota table的概念),
應該有機會可以解決上述的問題(先不論performance impact  :P):
1. create stored procedure for check quota for check quota, flush privillege, if check fail, also raise error.
2. create before trigger for table.
3. call stored_procedure in trigger.

12
有一天,有個朋友問我要如何用bash寫一個有timeout限制的menu。
我覺得很有趣就寫了一個sample給他。
後來功能就越加越多~

Finally, 決定拿出來跟大家分享(野人獻曝),any feed back is welcome~

PS. 我的環境是Ubuntu 10.04

代碼: [選擇]
#!/bin/bash

##############################################
##
## for menu utility
##
## author: Stanley Huang
## licence: Creative Commons Attribution-Share Alike 3.0 Taiwan License
## last release reversion: 0.1
## last modify date: 2010/08/04 15:30
## change list:
##   01. 2010/08/04 15:30, Stanley Huang. Build.
##
##############################################

showUsage() {
  cat <<EOF
Usage:
  $0
     [-e] (expert mode)
     [-h] (help menu)
     [-t] timeout_sec (default timeout = $nTimeout sec)
     [-c] no. of columns to display (default no. of column = $nColumn)
     [-w] column wide (default column wide = $nWide)
     [-m] menu file
Ex.
  $0
  $0 -e
  $0 -h
  $0 -t 10
  $0 -c 3
  $0 -w 10
  $0 -m /tmp/t.menu
EOF
}

pak2c() {
  read -n 1 -p "Press any key to continue..." pak2c
}

checkItemExist() {
  local sItem=$1
  grep "^$sFilter$sItem$sMenuActSeparator" $sMenuFile > /dev/null 2>&1
  return $?
}

checkItemNotExist() {
  local sItem=$1
  checkItemExist $sItem
  if [ $? -eq 0 ]
  then
    return 1
  else
    return 0
  fi
}

getHeader() {
  local sHeader="`grep "^$sHeaderFilter" $sMenuFile | head -1 | cut -d' ' -f2-`"
  echo $sHeader
}

setHeader() {
  while true
  do
    read -p "Please enter your header: " sNewHeader
    [ ! -z "$sNewHeader" ] && break
  done
  sHeader=$sNewHeader
  if [ `grep -c "^$sHeaderFilter" $sMenuFile` -ne 0 ]
  then
    perl -pi -e "s/^$sHeaderFilter.*$/$sHeaderFilter$sNewHeader/" $sMenuFile
  else
    echo "$sHeaderFilter$sNewHeader" >> $sMenuFile
  fi
}

buildMenu() {
  clear
  declare -i nColumn=$1
  declare -i nWide=$2
  declare -i nCount=0
  declare -i nMod=0
  declare    sCount=""

  printf -- "$sOuterSeparatorLine\n" && \
  printf -- "        `getHeader`\n" && \
  printf -- "$sOuterSeparatorLine\n" && \

  while read fItem item
  do
    nCount=$nCount+1 && sCount=$nCount && [ $isExpert -eq 0 ] && sCount=.
    aMenu[$nCount]="`echo $item | awk -F$sMenuActSeparator '{print($1)}'`"
    aActs[$nCount]="`echo $item | awk -F$sMenuActSeparator '{print($2)}'`"
   
    fDisable=`echo $fItem | sed -e 's/[^*]//g'`
    printf "%${#nTotalCount}s) %${nWide}s%1s" $sCount ${aMenu[$nCount]} $fDisable
   
    nMod=$nCount%$nColumn
    if [ $nMod -eq 0 ]
    then
      printf "\n";
    else
      printf " ";
    fi
  done <<EOF
`grep "^$sFilter" $sMenuFile ## disable display with sorting`
EOF
  #### `grep "^$sFilter" $sMenuFile | sort -k 2 ## display with sorting`
  [ $nMod -ne 0 ] &&  printf "\n"

  printf -- "$sOuterSeparatorLine\n" && \
  if [ "$isExpert" -eq 0 ]
  then
    printf "%${#nTotalCount}s) %${nWide}s\n" "a" "(A)dd"
    printf "%${#nTotalCount}s) %${nWide}s\n" "c" "(C)hange name"
    printf "%${#nTotalCount}s) %${nWide}s%s\n" "d" "(D)isable" ", marked *"
    printf "%${#nTotalCount}s) %${nWide}s\n" "e" "(E)nable"
    printf "%${#nTotalCount}s) %${nWide}s\n" "h" "set (H)eader"
    printf "%${#nTotalCount}s) %${nWide}s\n" "m" "(M)odify"
    printf "%${#nTotalCount}s) %${nWide}s\n" "r" "(R)emove"
    printf "%${#nTotalCount}s) %${nWide}s\n" "s" "(S)ort data"
    printf "%${#nTotalCount}s) %${nWide}s\n" "v" "(V)iew cmd"
    printf -- "$sInnerSeparatorLine\n"
    printf "%${#nTotalCount}s) %${nWide}s\n" "n" "(N)ormal mode"
  else
    printf "%${#nTotalCount}s) %${nWide}s\n" "e" "(E)xpert mode"
  fi
  printf "%${#nTotalCount}s) %${nWide}s\n" "q" "(Q)uit"
  printf -- "$sOuterSeparatorLine\n"
}

## Usage:
##   repeat [character] [line size]
## Ex.
##   repeat x 100
repeat() {
  [ $# -lt 2 ] && return 1
  sChar=${1:0:1}
  nSize=$2
  printf -vch  "%${nSize}s" ""
  printf "%s\n" "${ch// /$sChar}"
}

############################################# main

clear
set -o noglob
declare -i nDefaultTimeout=10
declare -i nExpertTimeout=65535
declare -i nTimeout=$nDefaultTimeout
declare -i isExpert=1
declare -a aMenu
declare -a aActs
declare    sMenuActSeparator=":"
declare    sFilter=""
declare -i nTotalCount=0
declare -i nDefaultColumn=1
declare -i nExpertColumn=3
declare -i nColumn=$nDefaultColumn
declare -i nWide=16
declare -i nActionWide=20
declare    sMenuFile=$0 ## default menu file is program itself.

declare -i nSeparatorLength=30
declare    sOuterSeparatorLine=`repeat = $nSeparatorLength`
declare    sInnerSeparatorLine=`repeat - $nSeparatorLength`

while getopts ":ehc:t:w:m:" opt
do
  case $opt in
  e)
    isExpert=0
    ;;
  h)
    showUsage
    exit 0
    ;;
  c)
    nDefaultColumn=$OPTARG
    ;;
  m)
    sMenuFile=$OPTARG
    ;;
  t)
    nDefaultTimeout=$OPTARG
    ;;
  w)
    nWide=$OPTARG
    ;;
  :)
    echo "The option ($opt) without parameters, exit program..."
    showUsage
    exit 1
    ;;
  \?)
    echo "Not such option($OPTARG), exit program..."
    showUsage
    exit 1
    ;;
  esac
done

sEnableFilter="#@ "
sDisableFilter="#\* "
sNormalFilter=$sEnableFilter
sExpertFilter="#[@*][ ]"
sExpertFilter4Perl="(#[@*][ ])"
sHeaderFilter="#\^ "
if [ $isExpert -eq 0 ]
then
  sFilter=$sExpertFilter
  nTimeout=$nExpertTimeout
  nColumn=$nExpertColumn
else
  sFilter=$sEnableFilter
  nTimeout=$nDefaultTimeout
  nColumn=$nDefaultColumn
fi
nTotalCount=`grep -c "^$sFilter" $sMenuFile`

while true
do
  buildMenu $nColumn $nWide
  declare -i nChoice
  declare    sChoice
  declare -i fRead=0
  declare    sItem=""

  while true
  do
    echo -n "Please enter your choice ($nTimeout secs to timeout): "
    read -t $nTimeout sChoice || fRead=$?

    #### special events
    [ $fRead -eq 142 ] && echo "Time out..." && exit 0
    [ $isExpert -eq 1 ] && [ $sChoice == 'e' ] && $0 -e -t $nDefaultTimeout -m $sMenuFile && exit 0
    [ $isExpert -eq 0 ] && [ $sChoice == 'n' ] && $0 -t $nDefaultTimeout -m $sMenuFile && exit 0
    [ $sChoice == 'q' ] && exit 0

    #### choice reactions
    nChoice=$sChoice
    [ -z "$sChoice" ] && buildMenu $nColumn $nWide && continue
    [ $isExpert -eq 1 ] && ( [ $nChoice -le 0 ] || [ $nChoice -gt $nTotalCount ] ) && continue
    [ $isExpert -eq 0 ] && ( [ $sChoice == "0" ] || [ $nChoice -ne 0 ] ) && continue
    break
  done

  case "$sChoice" in
  a)
    while true
    do
      read -p "Please enter the item you want to add: " sItem
      [ -z $sItem ] && break
      checkItemExist "$sItem" || break
      echo "Item ($sItem) exist!!"
    done

    if [ ! -z $sItem ]
    then
      read -p "Please enter the action of the item you added: " sAct
      if [ ! -z $sAct ]
      then
        echo "$sEnableFilter$sItem$sMenuActSeparator$sAct" >> $sMenuFile
        echo "Add item ($sItem) done..."
      fi
    fi
    ;;
  c)
    while true
    do
      read -p "Please enter the item you want to rename: " sItem
      [ -z $sItem ] && break
      checkItemNotExist "$sItem" || break
      echo "Item ($sItem) doesnot exist!!"
    done

    if [ ! -z $sItem ]
    then
      read -p "Please enter the new name of the item: " sNewItemName
      perl -pi -e "s/^$sExpertFilter4Perl$sItem$sMenuActSeparator.*$/\1$sNewItemName$sMenuActSeparator$sAct/" $sMenuFile
      echo "change item name ($sItem -> $sNewItemName) done..."
    fi
    ;;
  d)
    while true
    do
      read -p "Please enter the item you want to disable: " sItem
      [ -z $sItem ] && break
      checkItemNotExist "$sItem" || break
      echo "Item ($sItem) doesnot exist!!"
    done

    if [ ! -z $sItem ]
    then
      perl -pi -e "s/^$sEnableFilter$sItem$sMenuActSeparator/$sDisableFilter$sItem$sMenuActSeparator/" $sMenuFile
      echo "disable item ($sItem) done..."
    fi
    ;;
  e)
    while true
    do
      read -p "Please enter the item you want to enable: " sItem
      [ -z $sItem ] && break
      checkItemNotExist "$sItem" || break
      echo "Item ($sItem) doesnot exist!!"
    done

    if [ ! -z $sItem ]
    then
      perl -pi -e "s/^$sDisableFilter$sItem$sMenuActSeparator/$sEnableFilter$sItem$sMenuActSeparator/" $sMenuFile
      echo "enable item ($sItem) done..."
    else
      echo "sItem is empty..."
    fi
    ;;
  h)
    setHeader
    ;;
  m)
    while true
    do
      read -p "Please enter the item you want to modify: " sItem
      [ -z $sItem ] && break
      checkItemNotExist "$sItem" || break
      echo "Item ($sItem) doesnot exist!!"
    done

    if [ ! -z $sItem ]
    then
      read -p "Please enter the action of the item: " sAct
      perl -pi -e "s/^$sExpertFilter4Perl$sItem$sMenuActSeparator.*$/\1$sItem$sMenuActSeparator$sAct/" $sMenuFile
      echo "modify item ($sItem) done..."
    fi
    ;;
  r)
    while true
    do
      read -p "Please enter the item you want to remove: " sItem
      [ -z $sItem ] && break
      checkItemNotExist "$sItem" || break
      echo "Item ($sItem) doesnot exist!!"
    done

    if [ ! -z $sItem ]
    then
      perl -pi -e "s/^$sExpertFilter$sItem$sMenuActSeparator.*\n$//" $sMenuFile
      echo "remove item ($sItem) done..."
    fi
    ;;
  s)
    cp $sMenuFile $sMenuFile.bak ## backup first
    declare -i nEOS=`grep -n '#### \*\*\* Data \*\*\*$' $sMenuFile | cut -d: -f1`
    declare -i nBOD=$nEOS+1 ## not work for Ubuntu
    declare -i nLOD=`wc -l $sMenuFile | awk '{print($1)}'`-$nEOS
    [ $nEOS -eq 0 ] && exit 0
    head -$nEOS $sMenuFile > /tmp/$$.main
    ##tail +$nBOD $sMenuFile | sed -e "s/^#[@*][ ]*$//" | sed -e "/^$/d" | sort -k 2 > /tmp/$$.data ## not work for Ubuntu
    tail -$nLOD $sMenuFile | sed -e "s/^$sExpertFilter*$//" | sed -e "/^$/d" | sort -k 2 > /tmp/$$.data
    cat /tmp/$$.main /tmp/$$.data > $sMenuFile
    rm /tmp/$$.main /tmp/$$.data
    echo "Sorting data done, please restart your program..."
    ;;
  v)
    while true
    do
      read -p "Please enter the item you want to view: " sItem
      [ -z $sItem ] && break
      checkItemNotExist "$sItem" || break
      echo "Item ($sItem) doesnot exist!!"
    done

    if [ ! -z $sItem ]
    then
      grep "^$sExpertFilter$sItem$sMenuActSeparator" $sMenuFile | awk -F"$sMenuActSeparator" '{print("Command:",$2)}'
    fi
    ;;
  "")
    ;;
  *)
    if [ $isExpert -eq 1 ] ## 1 for not expert mode!
    then   
      OLD_IFS=$IFS
      IFS=';'
      for sCmdOpts in ${aActs[$nChoice]}
      do
        echo "Execute Command: $sCmdOpts"
        eval $sCmdOpts
      done
      IFS=$OLD_IFS
    else
      continue
    fi     
    ;;
  esac
  pak2c
done

echo "End of process..."

exit
exit
exit

####
#### Menu item:
####   Use '#@ ', '#* ' at the begin of the line for setting menu item
####   #^ header(title)
####   #@ enable items
####   #* disable items
#### Ex.
####   #@ item
#### PS.
####   Do not modify the data below, use option "-e" (export mode) to modify.
####
#### *** Header ***
#^ My Menu
####
#### *** Data ***
#@ apple:echo "this an apple"
#@ banana:echo "this an banana"
#@ ls:ls
#@ id:id
#@ pwd:pwd
#@ stanley:id;pwd;ls -al;ls ./;echo 1 2 3;pwd;id

13
Hi all:

如果要將軟體deployment到Linux的機器上,
有沒有建議的opensource deployment軟體可以用。
之前在Windows平台上,我都是用InstallShield。


14
trigger 有效能的問題,那 store procedure呢?
Stored Procedure 也是會suffer到一些效能。
所以,我通常只有在需要在backend整理(需定期準備預先處理/或migrate data)資料時,
才會使用到Stored Procedure.
有一派說法(大家自行評斷), 系統中使用過多stored procedure,
或將邏輯拆開寫在Stored Procedure,
會增加開發/管理的複雜度,
非萬不得已, 不要輕易嘗試.

regards,

Stanley Huang

15
恕刪
...
2.1 使用 VIEW的方式
...
2.2 使用 Table + Trigger的方式
...

好久沒有看到這個有心的文章了,
一定要先拍拍手.... ;D

補充一些個人的經驗給大家參考/切磋~

使用view並沒有讓performance有太大的幫助,
但是還要讓DBA額外多管理一個view.
我個人比較喜歡在select中直接引用function.

至於Table + Trigger,
Trigger是一個很耗資源的方法,
通常只有外萬不得已時才會使用.

而Table + 使用定期 update 方式,
可能還要加上一些判斷條件(ex. 轉換資料的起始位置),
否則當時間一長(部分資料已轉換過), 且檔案越來越大,
就會浪費時間在重複處理已經處理的資料.
如果, 表格的時間會變動,
變成要同步處理時間轉換,
否則會有資料不一致的問題.

regards,

Stanley Huang

16
Linux 討論版 / 回覆: ping shell script問題
« 於: 2010-07-19 16:46 »
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:
export PATH
test=/bin/ping -c 3 127.0.0.1
grep -E '0% packet loss|error'
echo $test
test2=cut -d' ' -f 1 $testa
test3=cut -d' ' -f 4 $test
if[ $test2 == $test3 ]; then
echo "OK"
else
echo "bad"
fi


//------------------------------
./ppp.sh: line 4: -c: command not found
為甚麼會找不到 ping
但在/bin/下有找到ping
要麼解決

少反單引號(`), and 記得要在外面再加雙引號(")
ex:
test="`/bin/ping -c 3 127.0.0.1`"

regards,

Stanley Huang

17
1130-Host localhost is not allowed to connect to this MySQL server



救就我 我該怎辦

我不小心去管理使用者的
localhost 砍掉了  砍掉隻後我把MYSQL 還有資料庫都重灌一次

現在你們說的MYSQL裡面的date都找不到 

不是很確定你的環境,
但你的問題,原則上,把資料庫關機,然後把data的目錄砍掉後再重灌,
應該可以使用root帳號登入~~

regards,

Stanley Huang

18
如題(不許員工上網)
請教大家有何可行之道?
(電腦數8,預算不超過六位數)

建議順便考慮:
1. 不允許使用隨身碟
2. 不允許照相手機
3. 不允許錄音設備

4. 不允許照相/錄音手錶
5. 不允許照相/錄音眼鏡
6. 不允許照相/錄音香煙盒
... (族繁不及備載)
總之,不允許任何針孔攝影機及側錄系統,
那可能要脫光光上班了.... ;D (逃)

19
假設我在某PHP頁面設定一變數
例如
      $xxx="zzz";
如何在用流覽器跑完這個頁面後
在BASH環境中取得xxx變數呢??


把變數和值寫到一個檔案上,再讓script去讀它。
ex.
php_var.ini:
代碼: [選擇]
myvar=foo
my.bash:
代碼: [選擇]
#!/bin/bash
. php_var.ini

regards,

Stanley Huang

20
資料表abc中有一個欄位aaa

例如
欄位資料1為99/03/05
欄位資料2為101/10/04

怎麼下mysql來做指定日期今天2010-06-30
30天以內的資料篩選

西元年的表示式呢?

謝謝

暴力法:

代碼: [選擇]
select d, lpad(d,9,0)
from a
order by lpad(d,9,0);
+-----------+-------------+
| d         | lpad(d,9,0) |
+-----------+-------------+
| 97/01/01  | 097/01/01   |
| 99/02/28  | 099/02/28   |
| 99/06/07  | 099/06/07   |
| 99/06/16  | 099/06/16   |
| 99/07/05  | 099/07/05   |
| 99/07/06  | 099/07/06   |
| 99/12/31  | 099/12/31   |
| 100/01/01 | 100/01/01   |
+-----------+-------------+
8 rows in set (0.00 sec)
代碼: [選擇]
select
d c1,
lpad(d,9,0) c2,
concat(
  lpad(
    year(
      current_date
    )-1911,3,0
  ),
  '/',
  lpad(
    month(
      current_date
    ),2,0),
  '/',
  lpad(
    day(
      current_date
    ),2,0
  )
) c3,
concat(
  lpad(
    year(
      date_add(
        current_date, interval -30 day
      )
    )-1911,3,0
  ),
  '/',
  lpad(
    month(
      date_add(
        current_date, interval -30 day
      )
    ),2,0
  ),
  '/',
  lpad(
    day(
      date_add(
        current_date, interval -30 day
      )
    ),2,0
  )
) c4
from a
where
lpad(d,9,0) <=
concat(
  lpad(
    year(
      current_date
    )-1911,3,0
  ),
  '/',
  lpad(
    month(
      current_date
    ),2,0),
  '/',
  lpad(
    day(
      current_date
    ),2,0
  )
)
and
lpad(d,9,0) >=
concat(
  lpad(
    year(
      date_add(
        current_date, interval -30 day
      )
    )-1911,3,0
  ),
  '/',
  lpad(
    month(
      date_add(
        current_date, interval -30 day
      )
    ),2,0
  ),
  '/',
  lpad(
    day(
      date_add(
        current_date, interval -30 day
      )
    ),2,0
  )
);
+----------+-----------+-----------+-----------+
| c1       | c2        | c3        | c4        |
+----------+-----------+-----------+-----------+
| 99/07/06 | 099/07/06 | 099/07/06 | 099/06/06 |
| 99/06/16 | 099/06/16 | 099/07/06 | 099/06/06 |
| 99/07/05 | 099/07/05 | 099/07/06 | 099/06/06 |
| 99/06/07 | 099/06/07 | 099/07/06 | 099/06/06 |
+----------+-----------+-----------+-----------+
4 rows in set (0.00 sec)


21
有了民國年轉西洋年,再來要有配對的西洋年轉民國年的Function才完整.
此時要注意1911年及1911年以前的狀況.
1912年為民國1年,1911年為民前1年.
另外為了兼顧轉換時不需要民國yyyy年mm月dd日格式,所以有轉換型態之參數.
---略---

這麼精彩的東西, bunko大乾脆北、中、南各來幾場啦  ;D ;D

贊成! 我報名先!!

22
大大您好

不好意思,他這次是顯示以下訊息,是否有解,感謝
 MySQL 傳回: 說明文件
#1248 - Every derived table must have its own alias
just give the temp table an alias:

SELECT ip, sum( bytes ) bytes
from
(
SELECT *
FROM today
UNION ALL
SELECT *
FROM week
) t
GROUP BY ip;

regards,

Stanley Huang

23
感謝你的回覆與解答  雖然是"暴力法" 也是可能可以解決問題
我原先想用AWK 或 SED 來做  功力不夠所以卡在這裡
不過這其實更加簡單 又很直覺  謝謝

awk, sed在做完資料修改後, 會將結果顯示到標準輸出. ( 我不知道是否有辦法可以直接修改檔案 )
所以, 我通常也是用兩段式的方法來處理.
#!/bin/bash
sLine="`head -1 ./file-list.txt`"
sed -e '1,1d' ./file-list.txt > ./file-list.txt.tmp
mv ./file-list.txt.tmp ./file-list.txt

但是這樣處理會比用tail -n +2 快嗎??

Just try it!

代碼: [選擇]
#!/bin/bash
echo "sed -e '1,1d' ./t.txt > ./t2.txt"
#echo "head -n +2 ./t.txt > ./t2.txt"
for i in seq 1 100
do
  sed -e '1,1d' ./t.txt > ./t2.txt
  #tail -n +2 ./t.txt > ./t2.txt
done

stanley@Stanley-Ubuntu:~$ time ./test.sh
head -n +2 ./t.txt > ./t2.txt

real    0m0.244s
user    0m0.016s
sys     0m0.092s


stanley@Stanley-Ubuntu:~$ time ./test.sh
sed -e '1,1d' ./t.txt > ./t2.txt

real    0m0.956s
user    0m0.716s
sys     0m0.100s


stanley@Stanley-Ubuntu:~$

如果你的server很忙碌, 或檔案很大.
這樣的做法, 其實會浪費很多資源.
所以, 如果沒有絕對的必要,
其實就把舊檔改名後讓程式去處理就好了,
這樣你就不會回寫檔案了!
代碼: [選擇]
#!/bin/bash
mv file-list.txt file-list.txt.working ## ps. 此時另一隻程式, 還是在持續寫入到file-list.txt
cat ./file-list.txt.working | read line
do
  do some thing...
done

regards,

Stanley Huang

24
大大您好

我執行時會顯示以下錯誤,是否有解,感謝
 SELECT ip, sum( bytes ) bytes(
SELECT *
FROM today
UNION ALL
SELECT *
FROM week
)
GROUP BY ip

MySQL 傳回: 說明文件
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(
select * from today
union all
select * from week
)
group by ip' at line 2

抱歉,
少key一個from.
SELECT ip, sum( bytes ) bytes
from
(
SELECT *
FROM today
UNION ALL
SELECT *
FROM week
)
GROUP BY ip

regards,

Stanley Huang

25
無意中發現一本好書,
有興趣學習進階SQL的人可以去看看.

http://www.delightpress.com.tw/book.aspx?book_id=sknd00002

regards,

Stanley Huang


26
兩位大大好

有辦法把byte欄位加起來嗎
因為有時候today和week的ip會重覆,如下
10.1.1.6分別在today和week出現

today
ip  bytes
10.1.1.1   60
10.1.1.2   70
10.1.1.3   80
10.1.1.6   30

week
ip bytes
10.1.1.4   90
10.1.1.5   50
10.1.1.6   40

合併成一張table後會把ip相同部份的byte欄位給加起來
如下
ip bytes
10.1.1.1   60
s10.1.1.2   70
10.1.1.3   80
10.1.1.4   90
10.1.1.5   50
10.1.1.6   70

如果單單只使用union和union all似乎無法做到
想請問若是要這樣語法該如何下,感謝

如果要summary,
那可以先將兩table union all,
在對該temp table 做aggregation.
selec ip, sum(bytes) bytes
(
select * from today
uion all
select * from week
)
group by ip;

regards,

Stanley Huang


27
各位大大好

小弟使用mysql
有以下2 table
today
ip  bytes
10.1.1.1   60
10.1.1.2   70
10.1.1.3   80

week
ip bytes
10.1.1.4   90
10.1.1.5   50

合併成一張table如下
ip bytes
10.1.1.1   60
10.1.1.2   70
10.1.1.3   80
10.1.1.4   90
10.1.1.5   50

這個用right joind和left join好像都不對
但是mysql 似乎不支援full join
想請問一下這個語法要怎麼下,感謝


合併兩個table的概念應該是union, 不是join.
union 分成 unique 和 all.
如果要消彌重複的資料時, 就用union就好了.

unique:
select * from today union select * from week

all:
select * from today union all select * from week

詳情請參閱user manual guide.
http://dev.mysql.com/doc/refman/5.0/en/union.html

regards,

Stanley Huang


28
通常都是,有人偷吃了機房的"乖乖"才壞的~~

regards,

Stanley Huang

29
Linux 討論版 / 回覆: [請益] polkitd 一問
« 於: 2010-06-08 10:00 »
各位大大們你好:

我在裝完Ubuntu 10.04 之後,
發現有一個process (polkitd) 一直在吃掉我的記憶後,
最終他占用很大的記憶體, 讓我的系統慢到不行.
我曾試著把他kill掉, 但是他還是被系統重新啟動.
所以, 想問:
1. 請問這樣的情形是daemon的bug嗎?
2. 這個daemon很重要嗎? 可以把他關掉嗎?
3. 請問該如何關掉他?

感謝.

Stanley Huang


找到問題了,
原因是我把之前9.10的家目錄直接overwrite到10.04的家目錄.
結果先前的.pulse和.dbus與10.04不相容,造成memory leak.
後來我把這兩個目錄砍掉重練就好了~~

wish this helps.

regards,

Stanley Huang

30
各位先進 我想要讀取一個TEXT檔 的第一行 其中一個欄位作為SHELL SCRIPT處理的變數來源
但是同時也必須將第一行從原來的檔案刪除 如此原來的第二行遞補 做為下一次SCRIPT的來源
可是我想不出來要怎麼做比較方便
TEXT 檔案格式(file-list.txt):
aaa.0529.xyz
bbb.0530.abc
ccc.0531.opq

第一次SCRIPT執行時 讀取第一行 aaa.0528.xyz  摘取0528 做一些處理
但是同時也需要將 aaa.0528.xyz 從原檔案中移除 (也不要空行 否則製造下次讀取麻煩)
最主要也是因為SCRIPT 會產生一個新的檔名 append 到 file-list.txt 的最後一行
變成為(file-list.txt):
bbb.0530.abc
ccc.0531.opq
ddd.0601.rst

有先進可以指點一下使用BASH 如何做出類似SCRIP? 感謝


我用的是暴力解法:
s=`head -1 file-list.txt`                     ## 讀取第一行, s=aaa.0529.xyz
head +2 file-list.txt > ./file-list.txt.bak ## 讀取第二行到最後一行, 並輸出到file-list.txt.bak
mv ./file-list.txt.bak ./file-list.txt         ## 將file-list.txt.bak更名為file-list.txt

regards,

Stanley Huang

頁: [1] 2 3 4