使用function 來獲取
#!/bin/bash function read_dir(){ today=`date +%Y-%m-%d` for file in `ls $1` do if [ -d $1"/"$file ] ;then read_dir $1"/"$file elif [ -f $1"/"$file ] ;then longfile=`ls -l --time-style=long-iso $1"/"$file` check=`echo $longfile | grep $today` if [ -n "$check" ] ; then echo $1"/"$file fi fi done } read_dir $1

#!/bin/bash dt=`date "+%Y-%m-%d"` path=/home/vmuser/linbo/test_upload/data_file/unilever_sales_and_retrun/prod/ echo $dt echo "ls --full-time $path | sed -n '/${dt}/p' | awk '{print \$9}' " command="ls --full-time $path | sed -n '/${dt}/p' | awk '{print \$9}' >/home/vmuser/linbo/kettleDemo/job/data/file.txt"
eval $command
echo $?
捎帶介紹一下sed命令這兩個選項:
- -n選項:只顯示匹配處理的行(否則會輸出所有)(也就是關閉默認的輸出)
- -p選項:打印
[vmuser@bd-c02 prod]$ vim test.txt
[vmuser@bd-c02 prod]$ cat test.txt
abcd;9527;efg
hello shell
[vmuser@bd-c02 prod]$ sed 's/9527/hello/' test.txt > target.txt 首先sed是有一個默認輸出的,也就是將所有文件內容都輸出,加上命令行中的替換,那么輸出結果就是下面這樣
[vmuser@bd-c02 prod]$ cat target.txt
abcd;hello;efg
1035925174
exit
hello shell
[vmuser@bd-c02 prod]$ sed 's/9527/hello/p' test.txt > target.txt 這行的意思就是:首先sed默認輸出文件全部內容,然后p又將匹配到的內容打印了一遍,也就是會輸出兩邊匹配到的內容
[vmuser@bd-c02 prod]$ cat target.txt
abcd;hello;efg
abcd;hello;efg
1035925174
exit
hello shell
[vmuser@bd-c02 prod]$ sed -n 's/9527/hello/p' test.txt > target.txt 這行就是sed -n屏蔽默認輸出然后s替換,p再將匹配到的內容打印出來,所以只顯示了一行,也就是匹配到的那一行
[vmuser@bd-c02 prod]$ cat target.txt
abcd;hello;efg
[vmuser@bd-c02 prod]$ sed -n 's/9527/hello/' test.txt > target.txt 這行就是sed -n選項屏蔽默認輸出,s替換,但是沒有p就不會將匹配到的內容輸出
[vmuser@bd-c02 prod]$ cat target.txt
[vmuser@bd-c02 prod]$
