shell指令碼使用記錄一 操作檔案

2022-02-05 03:32:03 字數 2380 閱讀 1974

一,連線遠端資料庫(保證在伺服器上能使用mysql命令列,至少要安裝mysql客戶端)

#

!/bin/bash

hostname="ip"

port="

3306

"username="

username

"password="

password

"dbname="

test

"tablename="

shell_test

"insert_sql="

insert into $tablename values(null,'billchen',2)

"mysql -h$hostname -p$port -u$username -p$password

$dbname -e "

$insert_sql

"

二,遍歷資料夾,並輸出該資料夾下所有的所有資料夾資訊(計算資料夾大小)

#

!/bin/bash

#function:new ls

#author:reed

files=$1

if [ $#

-eq 1 ];then

for filelist in $(find $1);do

filetype=$(ls -lhd $filelist |awk -f' ' ''|cut -c 1)

if [ "

$filetype

" ==d ];then

dirsize=$(du -sh $filelist|awk '')

ls -lhd $filelist|sed "

s/[^ ]\+/$dirsize/5

"else

ls -lh $filelist

fi done

else

echo

"--usage:$0 +[directory] or [file];

"echo

"--example:$0 /root

"fi

如果想把這些資訊輸入到檔案中:

#

!/bin/bash

#function:new ls

#author:reed

files=$1

if [ $#

-eq 1 ];then

for filelist in $(find $1);do

filetype=$(ls -lhd $filelist |awk -f' ' ''|cut -c 1)

if [ "

$filetype

" ==d ];then

dirsize=$(du -sh $filelist|awk '')

ls -lhd $filelist|sed "

s/[^ ]\+/$dirsize/5

" >> /root/yaming/vedio-log/mylog.txt

else

ls -lh $filelist >> /root/yaming/vedio-log/mylog2.txt

fidone

else

echo

"--usage:$0 +[directory] or [file];

"echo

"--example:$0 /root

"fi

如果還想把上面檔案中的資訊存入資料庫中:

#

!/bin/bash

host="ip"

table="

statistics_storage

"passwd="

password

"filename=`date +%y-%m-%d.txt`

cat /root/yaming/vedio-log/$|tr """

," > /root/yaming/vedio-log/$

cat /root/yaming/vedio-log/$ | sed 's/^/null\t/g' |sed 's/\t/,/g' >> /root/yaming/vedio-log/$

cat /root/yaming/vedio-log/$ |while

read line

doecho

$line

query=`echo "

$line

" |awk -f",

" ''`

echo

$query

mysql -h$host dbname -u username -p$passwd

<$table values($query

);eof

done

Shell 指令碼學習記錄(一)

一 shell 學習shell指令碼之前,我們有必要先來認識認識什麼叫shell?談到這個問題,我們首先來回憶一下計算機自底向上的體系,最下層是硬體,上面是驅動,上面是作業系統,在上面是shell,所謂的外殼。shell其實是使用者和作業系統進行人機互動的乙個inte ce。主要通過命令列來使系統完...

shell指令碼記錄

size x large shell size case語法 case 1 in start start stop stop usage esac 檢視系統32位還是64位 uname m x86 64 arch x86 64 size x large grep size grep命令hang住的問...

shell指令碼記錄

shell學習 shell 指令碼格式 檔案以.sh結尾,檔案頂行輸入 bin bash 變數 變數名 變數值 獲取變數值 變數名 環境變數 使用export 變數名 這樣在任何位置都可以獲取到變數值 shell 預設賦值變數都是字串 0 shell檔名 n 第n個變數 變數數量 所有變數 所有變數...