cスクリプトによる操作
cスクリプトによるいくつかの操作
体得する
c/c++に比べて面倒なファイル操作を処理する必要がある場合は、shellスクリプトを呼び出すことで実現できます.
相関c関数
(1)関数プロトタイプ:
FILE * popen ( const char * command , const char * type )
int pclose ( FILE * stream );
(2)ヘッダファイルが必要:#include
(3)関数の説明
popen()関数はパイプを作成しforkを呼び出してサブプロセスを生成します.
shellを実行してコマンドを実行してプロセスを開きます.
このプロセスはpclose()関数によって閉じられます.
pclose()関数は標準I/Oストリームを閉じ、コマンドの実行が終了するのを待ってshellの終了状態に戻ります.
shellが実行できない場合、pclose()が返す終了状態はshellがexitを実行したのと同じです.
(4)typeパラメータは、読み取りまたは書き込みのいずれかのみであり、得られる戻り値(標準I/Oストリーム)もtypeに対応する読み取り専用または書き込みのみである
を選択します.typeが「r」である場合、ファイルポインタはcommandの標準出力に接続される.typeが「w」の場合、ファイルポインタは
commandの標準入力.
(5)commandパラメータは、NULLで終わるshellコマンド文字列を指すポインタである.このコマンドはbin/shに渡され、
-cフラグを使用すると、shellはこのコマンドを実行します.
(6)popenの戻り値は標準I/Oストリームであり,pcloseによって終了しなければならない.
前述したように、このストリームは一方向です.したがって、このストリームへの書き込みは、コマンドを書き込む標準入力に相当する.
コマンドの標準出力はpopenを呼び出すプロセスと同じです.
これに対して、ストリームからのデータの読み出しは、読み出しコマンドの標準出力に相当する.
コマンドの標準入力とpopenを呼び出すプロセスは同じです.
コード実装c/c++呼び出しshellスクリプト実装という機能:
フォルダに一定数のtest_があるdelの先頭のファイル、優先度は以下の通りです.
第一に、保証ファイル数が10個より大きい
第二に、1週間以上のファイルを削除します.
#c++ファイル、exec_text.cpp
#shellスクリプトファイル、script_text.sh
体得する
c/c++に比べて面倒なファイル操作を処理する必要がある場合は、shellスクリプトを呼び出すことで実現できます.
相関c関数
(1)関数プロトタイプ:
FILE * popen ( const char * command , const char * type )
int pclose ( FILE * stream );
(2)ヘッダファイルが必要:#include
(3)関数の説明
popen()関数はパイプを作成しforkを呼び出してサブプロセスを生成します.
shellを実行してコマンドを実行してプロセスを開きます.
このプロセスはpclose()関数によって閉じられます.
pclose()関数は標準I/Oストリームを閉じ、コマンドの実行が終了するのを待ってshellの終了状態に戻ります.
shellが実行できない場合、pclose()が返す終了状態はshellがexitを実行したのと同じです.
(4)typeパラメータは、読み取りまたは書き込みのいずれかのみであり、得られる戻り値(標準I/Oストリーム)もtypeに対応する読み取り専用または書き込みのみである
を選択します.typeが「r」である場合、ファイルポインタはcommandの標準出力に接続される.typeが「w」の場合、ファイルポインタは
commandの標準入力.
(5)commandパラメータは、NULLで終わるshellコマンド文字列を指すポインタである.このコマンドはbin/shに渡され、
-cフラグを使用すると、shellはこのコマンドを実行します.
(6)popenの戻り値は標準I/Oストリームであり,pcloseによって終了しなければならない.
前述したように、このストリームは一方向です.したがって、このストリームへの書き込みは、コマンドを書き込む標準入力に相当する.
コマンドの標準出力はpopenを呼び出すプロセスと同じです.
これに対して、ストリームからのデータの読み出しは、読み出しコマンドの標準出力に相当する.
コマンドの標準入力とpopenを呼び出すプロセスは同じです.
コード実装c/c++呼び出しshellスクリプト実装という機能:
フォルダに一定数のtest_があるdelの先頭のファイル、優先度は以下の通りです.
第一に、保証ファイル数が10個より大きい
第二に、1週間以上のファイルを削除します.
#c++ファイル、exec_text.cpp
#include
#include
#include
#include
#define DPRINT printf //
#define EPRINT printf //
#define CMD_OUTPUT_LEN (1024)
/*
* shell ,
* 1:
* 2: ,
* 3: int, 0 ,
*/
int execCmd(const std::string& sCmd, std::string& sRes)
{
//
if(sCmd.empty())
{
EPRINT("execCmd sCmd is empty
");
return -1;
}
FILE* pFile = popen(sCmd.c_str(), "r");
if (!pFile)
{
EPRINT("exexCmd popen err(%d)", errno);
return -1;
}
char buffer[CMD_OUTPUT_LEN] = {0};
while(!feof(pFile))
{
//fget \0, sizeof(buffer)-1
if (fgets(buffer, sizeof(buffer), pFile))
{
sRes = sRes + buffer;
}
}
pclose(pFile);
return 0;
}
#define SCRIPT_PATH "./script_text.sh"
#define SCRIPT_SUCCESS "success"
int main()
{
int nCode = -1;
std::string sRec;
nCode = execCmd(SCRIPT_PATH, sRec);
// success
if(0 != sRec.compare("success"))
{
EPRINT("call script(%s) failure, sRec=%s
", SCRIPT_PATH, sRec.c_str());
}
else
{
DPRINT("call script(%s) success, sRec=%s
", SCRIPT_PATH, sRec.c_str());
}
return 0;
}
#shellスクリプトファイル、script_text.sh
#!/bin/sh
# : , success
# , ,
log="/var/log/test_error.log"
date > $log
#
max=3
#
opr_dir="./"
#
file_name="test_del"
#
current_time=`date "+%Y-%m-%d %H:%M:%S"`
current_ts=`date -d "$current_time" +%s`
#
save_time=$(( 60 * 60 * 24 *7 ))
# , , 10 , 10 ,
update_test()
{
#to do
local num=0
for file in `ls -t | grep -e "^$file_name"`
do
if [ ! -f $file ]; then
continue
fi
((num++))
if [ $num -le $max ]; then
continue
fi
# ,
create_file_time_m=`stat $file|grep Modify|awk '{print $2" "$3}'`
# ,
create_file_time=${create_file_time_m%%.*}
#
create_file_ts=`date -d "$create_file_time" +%s`
#
time_diff=$(( $current_ts - $create_file_ts ))
if [ $save_time -lt $time_diff ]; then
`rm -rf $file`
fi
done
return 0
}
#
start()
{
#opr directory
cd $opr_dir
update_test
echo -n "success"
return 0
}
#main start
start
#main end
exit 0