Quote: I have never written a shell script before, perhaps because of laziness, perhaps not being forced to write a shell. However, some time ago, the job requirement required a rerun of the script for a few months, which ran regularly every day, and several scripts were associated with running each day. You might say, it's too easy, write a loop, and then let him run on his own. Yes, you can easily use your programming language to write loops, such as PHP. But, you know, this is actually changing the structure of the code, and what does the ghost know will result? And, I don't guarantee the meaning of all the code inside, I know! So, the question is, without changing the original code, under the premise of how to cycle these months? Yes, that's what happens when you simulate a real runtime, passing in a date parameter that you need to receive (if you already have this door in your code)! You know, this kind of timing script has an elegant name, crontab, then, the shell, all you have to do is write the shell.
Never written a shell? It doesn't matter, you obviously already know when the need is OK, it's too easy. Isn't that a grammar question? You do not tell me you will not Google, will not Baidu!
I will first throw a few points to consider, the following directly to the code!
1, how to get the current time, and convert to the format you need? Key words: Date
2. How to prevent running the same content multiple times at the same time? Key words: Lock
3, how to let the program after running once, cooling execution? Key words: Sleep
4, how to specify the running time period, counter or start date? Key words: While, let, expr
5, attach: How to know how the current state of operation? Key words: Echo, progress
Take these questions into consideration, you just step by step to write it, do not know the grammar, direct Google, Baidu, code reference as follows:
#/bin/bash # @author: Youge # @date: 2015-12-22 startdate= "2015-11-24" # when to start startdatetimestamp= ' date-d ' $startDate "+%s ' enddate=" 2015-12-14 "# When to end Enddatetimestamp= ' date-d ' $endDate ' +%s ' sleeptime=25 # t O Take a break havesthundo=0 # check if there are something undo, if not, exit the program rootdir= '/cygdrive/d/wam p/ppi/' dir= $rootDir "cron/" itemarr= ("Itema" "Itemb" "ITEMC") # The items you want to run there for item in ${itemarr[@]
Do runfile= $rootDir $item ". Run"; if [!-F "$runFile"];
Then havesthundo=1;
Echo $item "runs on Me" $runFile "touched"; Echo-e "script starttime:" "' Date" +%y-%m-%d%h:%m:%s "" \nbegindate: "$startDate" \nenddate: "$endDate" \npath: "$dir"
;> $runFile Touch "$runFile";
Break
else echo $item "is runing, skipped." $runFile;
Fi done; If [$haveSthUndo-ne 1];
Then Echo-e "Nothing to do now ... \ncheck it ...";
Exit
fi echo "Havesthundo eq:" $haveSthUndo; While [$endDateTImestamp-ge $startDateTimestamp]] do rundate= ' date-d @ $startDateTimestamp +%y-%m-%d ';
#1987 -01-06 params= "item= $item &d= $runDate";
msg= "[' Date" +%y-%m-%d%h:%m:%s "] now we run the date:" ${rundate} "[params]:" ${params}; Echo $msg;
# to show me ... echo $msg >> $runFile; # Run the scripts below CD $dir &&/bin/php/script1.php $params &&/bin/php./script2.php $par AMS &&/bin/php/scriptx.php $params # Run the scripts upon startdatetimestamp= ' expr $startDateTimes Tamp + 86400 '; # Run the next day ... echo ' ___sleeping for $sleepTime seconds ...
";
X= '; for ((itime=0; itime< $sleepTime; itime++));
Do let itime2= $itime +1;
progress= ' expr $itime 2 \* 100/$sleepTime '; printf "cooling:[%-" $sleepTime "s]%d%%\r" $x $progress x=# $x sleep 1;
# sleep xx seconds to run the next time, show the progress done;
Echo
Done echo [' Date ' +%y-%m-%d%h:%m:%s '] the end.">> $runFile;
#end of the file
Appendix:
According to my first shell, the problems encountered in the following example, hoping to reduce the time to detour:
1. The entire shell script, in fact, is equivalent to a series of commands you enter in the terminal, if you want to do what in the shell, first think about what can be done in the terminal, the word Fu connection, is directly with "" double quotes, output, variable definition without $ symbol, but use must add $ symbol.
2, "=" assignment symbol, both sides must not have spaces, this and other languages have differences, especially you have their own code beautiful style special attention, otherwise will report grammatical errors!
3, for the array contents are separated by "", not "," comma-delimited
4. Conditional judgment [true] a space is required after the brackets, but no spaces between the two brackets are allowed ([true])
5, while conditions can be judged by () parentheses, can also be used in [[]] Brackets
6, if you use Windows to write a shell, be sure to pay attention to the line break format \ n instead of \ r \ n, need to use some editor (such as notepad++) to change the line character format!
End: In fact, language is just a tool, never too difficult (too difficult and no one to use AH), the real tool to work up, is your mind!
It's actually very simple, Just do it.
[Root @my-pc]$ sh urscripts.sh #see your next time
The above is the detailed content of this article, I hope to help you learn.