Writing your shell is actually very simple


quotation:I haven’t written a shell script before, maybe because I’m lazy, maybe I’m not forced to write a shell. However, some time ago, due to work needs, it is required to run scripts for several months again. These scripts are run regularly every day, and several scripts run together every day. You might say, it’s too simple. Write a cycle, and then let him run by himself. Yes, you can easily write loops in your programming language, such as PHP. But, you know, this actually changes the code structure. Who knows what will happen? Also, I don’t guarantee that I understand the meaning of all the codes in it! So, the question is, how to cycle these months without changing the original code? Yes, that is,Simulate the real run-time situation and pass in the date parameter to be received(the premise is that you already have this door in your code)! You know, this kind of timing script has an elegant name, crontab, so it’s a shell. All you have to do is write a shell.

Haven’t you written a shell? It doesn’t matter. In fact, after the needs are determined, you obviously already know that it’s too simple. Isn’t it just a grammar problem? Don’t tell me you can’t Google or Baidu!

I’ll throw a few points to consider first, and then give them to the code directly!

1. How to get the current time and convert it to the format you need? Key words: Date

2. How to prevent the same content from running multiple times at the same time? Key words: lock

3. How to make the program run once and cool down the execution? Key words: sleep

4. How to specify the run time period, counter or start date? Keywords: while, let, expr

5. Additional: how to know the current running status? Key words: echo,   progress

After considering these problems, you can write them step by step. If you don’t know the syntax, you can directly Google and Baidu. The code reference is as follows:

# @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        # to take a break
haveSthUndo=0      # check if there is something undo , if not , exit the program


itemArr=("itemA" "itemB" "itemC")  # the items you want to run there
for item in ${itemArr[@]}
  if [ ! -f "$runFile" ] ; then
  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";
  echo $item "is runing, skipped. " $runFile;
if [ $haveSthUndo -ne 1 ]; then
  echo -e "Nothing to do now ...\ncheck it...";
echo "haveSthUndo eq: " $haveSthUndo;

while [[ $endDateTimestamp -ge $startDateTimestamp ]]
  runDate=`date -d @$startDateTimestamp +%Y-%m-%d`;    #1987-01-06
  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 $params &&    
    /bin/php ./scriptx.php $params
  # run the scripts upon

  startDateTimestamp=`expr $startDateTimestamp + 86400`;    # run the next day ...

  echo " ___sleeping for $sleepTime seconds ... ";
  for ((itime=0; itime<$sleepTime; itime++)); do
    let itime2=$itime+1;
    progress=`expr $itime2 \* 100 / $sleepTime`;
    printf "cooling:[%-"$sleepTime"s]%d%%\r" $x $progress
    sleep 1;              # sleep xx seconds to run the next time, show the progress
echo "[`date "+%Y-%m-%d %H:%M:%S"`] the end." >> $runFile;

#end of the file


According to my initial experience in shell, the problems encountered are listed as follows, hoping to reduce the time for everyone to make detours:

1. The whole shell script is actually equivalent to a series of commands you input at the terminal. If you want to do anything in the shell, first think about what you can do at the terminal. The connection of characters is to directly use “” double quotation marks, output, and variable definition without $symbol, but you must add $symbol when using.

2、”=”   There must be no spaces on both sides of the assignment symbol, which is different from other languages, especially when you have your own beautiful style of code, otherwise you will report syntax errors!

3. The array contents in for are separated by “” spaces, not “,” commas

4. Conditional judgment [true] a space is required after the bracket, but there can be no space between the two brackets, such as [[true]]

5. The while condition can be judged with () brackets or [[]] brackets

6. If you write a shell with windows, you must pay attention to the newline format \ n rather than \ R \ n, and you need to change the newline format with the help of some editors (such as Notepad + +)!

End: in fact, language is just a tool. It will never be too difficult (it will not be used). What can really make the tool work is your thought!

Just do it

[ root @my-pc ]$ sh urScripts.sh    #see you next time

The above is the detailed content of this article. I hope it will be helpful to your study.