Saltar al contenido

~ / archivo / stackoverflow / 46958895

Perform a different task each time a bash script is executed

StackOverflow · linux, bash · ayudé
  1. user309960 pregunta 2017-10-26 16:02

    Lets say I have 2 tasks. Is it possible to write a bash script (one script) that should execute 1st task when we run first time and 2nd task when we run the script second time and again 1st task when we run 3rd time and 2nd script when we run 4th time?

    • Miguel Ortiz yo 2017-10-26 17:46 You can have an external file to store the number of times it ran.
  2. riteshtch respuesta +1 2017-10-26 16:16

    Yes its easy to do this by remembering/persisting what was run the last time.

    $ echo "1" > current_counter
    $ cat current_counter 
    1
    $ vim main.bash
    $ cat main.bash 
    #/bin/bash
    current_counter=$(<"current_counter")
    if [[ "$((${current_counter}%2))" -eq 1 ]]; then
        echo "Running task 1"
    else
        echo "Running task 2"
    fi
    echo "$((${current_counter}+1))" > "current_counter"
    $ bash main.bash 
    Running task 1
    $ cat current_counter 
    2
    $ bash main.bash 
    Running task 2
    $ cat current_counter 
    3
    $ bash main.bash 
    Running task 1
    $ cat current_counter 
    4
    $ bash main.bash 
    Running task 2
    $ cat current_counter 
    5
    $ 
    

    You could also use a Boolean 0 or 1 in the current_counter file or just by checking the existence of a file but it will work only for this case. Above script scales easily if you have more tasks (not just 2) like 3 or even 100; then you just have to change the modulus number and add more conditions to handle each residue.

    • chepner 2017-10-26 17:19 You can drop the || [[ -n "$line" ]] hack; it is only needed to accommodate invalid POSIX text files (i.e., Windows text files) that don't correctly end with a newline character.
    • Miguel Ortiz yo 2017-10-26 19:50 I've used cat instead of while, I found it simpler to understand.
  3. Miguel Ortiz yo respuesta 2017-10-26 19:42

    I found too hard to read ritesht93 script, so I made my own.

    Bash Version

    Running the script provides this output:

    { ~ }  » ./test.sh                                                                                                                                       ~
    First Run
    { ~ }  » ./test.sh                                                                                                                                       ~
    Second Run
    { ~ }  » ./test.sh                                                                                                                                       ~
    Third Run
    { ~ }  »    
    

    First we create a file with the number 1 on it:

    { ~ }  » echo 1 > counter
    

    Script content (test.sh):

    #!/usr/bin/bash
    
    foo=`cat counter`
    
    if [ $foo -eq '1' ]
            then
                    echo "First Run"
                    echo '2' > counter
    fi
    
    if [ $foo -eq 2 ]
            then
                    echo "Second Run"
                    echo '3' > counter
    fi
    
    if [ $foo -eq 3 ]
            then
                    echo "Third Run"
                    # after 3 times we reset
                    echo '1' > counter
    fi
    

    Python 3.0 Version (if needed)

    I had time to do it in python as well:

    First we create a file with the number 1 on it:

    { ~ }  » echo 1 > counter
    

    Script content:

     #!/usr/bin/python
    
    with open('counter','r+') as file :
        for l in file :
                #print(type(l))
                line = l.strip()
    
                if line == '1' :
                        print ('this is line 1')
                        file.seek(0)
                        file.truncate()
                        file.write('2')
                elif line == '2' :
                        print ('this is line 2')
                        file.seek(0)
                        file.truncate()
                        file.write('3')
                elif line == '3' :
                        print ('this is line 3')
                        file.seek(0)
                        file.truncate()
                        file.write('1')
                else :
                        print ("I'm lost, reset the counter")
    
  4. Elanochecer respuesta 2017-10-26 21:51

    It´s even easier than the two previous answers. Just create an empty control file. Touch control and delete with rm controlfor example.

    When you run it you check if it exists, if it does you destroy it and run the second task, if not run the first and create it.

    You dont need to check anything inside the file just its existence.

    Edit

    #!/bin/bash
    $FILE=filename
    if [-f $FILE] 
    then
    #Task1
    rm $FILE
    else
    #Task2
    touch $FILE
    fi
    

    You can add more tasks by using different names as the next script:

    #!/bin/bash
    $FILE1=filename1
    $FILE2=filename2
    
    if [-f $FILE1] 
    then
    #Task1
    mv $FILE1  $FILE2
    elif [-f $FILE2]
    then
    #Task2
    rm $FILE2
    else 
    #Task3
    touch %FILE1
    fi
    

    Anyways I think the use of this with more than 2 tasks its not a good idea.

    • Miguel Ortiz yo 2017-10-27 04:08 I like the way you think. You should provide an script for that. Anyway that would limit the execution for two tasks. Reading the file adds flexibility for more than 2 executions. Other thing is I always avoid the use of rm in scripting. But under certain circunstances would be a good approach.
    • Elanochecer 2017-11-02 00:42 I added the script in case you wanted to see it.

← todos los hilos de StackOverflow