
次のスクリプトがあります
for File in $(cat $IMPORT_PATH/*.txt); do
echo `date +'%m-%d-%Y %H:%M:%S'` "starting $File execute" > $Import_Success_Log
./cli.sh -a execute -i IPROD_$File -fn formrnt -user -password>> $Import_Success_Log
echo `date +'%m-%d-%Y %H:%M:%S'` "$File execute completed" >> $Import_Success_Log
done
./cli.sh -authfile "$AuthFile" -a list -t area -nof > $Import_List_File
for File in $(cat $IMPORT_PATH/*.txt); do
imp_area=`grep -iw "PRD_$File" "$IGC_Import_List_File" | grep -i prod`;
##Testing if imp_area variable has a value
if [ -z "$imp_area" ]; then
echo "- $DataBase Imp Area is not present .Please create." >> $Import_Failure_Log
else
Preview=`grep -iB 3 "The admin setting require" $Import_Success_Log |head -1 | awk '{print $4}'`;
Error=`grep -i error $Import_Success_Log`;
No_Import=`grep -i "does not exist" $Import_Success_Log`;
if [ -z "$Preview" -a -z "$Error" -a -z "$No_Import" ];then
echo "<li> $DataBase </li>" >> $DB_Import_Complete
else
echo "- $Prev is not imported as this database require a preview. >> $Import_Failure_Log
fi
fi
done
スクリプトは特定のパスの txt ファイルを確認し、特定のコマンドを実行します。
このパスにはtxtファイルがたくさんある可能性があるため、毎回別のファイルの名前を.txt以外の名前に変更する必要があります。
そのため、txtファイルをスクリプトに変数/パラメータとして渡したいと思います。
次のようになります。sh script.sh abc.txt。
サンプルファイルの内容:File.txt
SQL_SEVRER_ACCOUNT
Customer_DB
Customer_support_DB
Account_DB
または、スクリプトをスケジュールするときにcrontabエントリの引数として使用されます。
私はスクリプトに初めて触れましたが、それについてはよくわかりません。
答え1
更新された質問に基づいて更新してください。
引数として渡されたファイルからデータベースを読み取るには、次のようにします。
for File in $(< "$1"); do
echo `date +'%m-%d-%Y %H:%M:%S'` "starting $File execute" > $Import_Success_Log
./cli.sh -a execute -i IPROD_$File -fn formrnt -user -password>> $Import_Success_Log
echo `date +'%m-%d-%Y %H:%M:%S'` "$File execute completed" >> $Import_Success_Log
done
(他のすべてのループでも同様)
それから電話してください
sh your-script file.txt
スクリプトはパラメータを使用して呼び出すことができ、これは$1
背中で使用できます。$2
したがって、あなたの場合はこれを行うことができます
File="$1"
echo `date +'%m-%d-%Y %H:%M:%S'` "starting $File execute" > $Import_Success_Log
./cli.sh -a execute -i "IPROD_$File" -fn formrnt -user -password>> $Import_Success_Log
echo `date +'%m-%d-%Y %H:%M:%S'` "$File execute completed" >> $Import_Success_Log
./cli.sh -authfile "$AuthFile" -a list -t area -nof > $Import_List_File
imp_area=`grep -iw "PRD_$File" "$IGC_Import_List_File" | grep -i prod`;
次に、スクリプトを次のように呼び出します。
sh your-script filename
PS:これは、スクリプトの残りの部分(特にコードスニペットの前に実行された部分)の間に値が$1
何らかの理由で変更されないと仮定します。
答え2
パラメータを使用してスクリプトを実行すると、これらのパラメータは自動的に変数$ 1、$ 2、$ 3、...に保存されます。
したがって、スクリプトを次のように実行すると:
sh script.sh abc.txt
その後、「abc.txt」は変数$ 1に格納されます。