【问题标题】:Perform a different task each time a bash script is executed每次执行 bash 脚本时执行不同的任务
【发布时间】:2017-10-26 16:02:02
【问题描述】:

假设我有 2 个任务。 是否可以编写一个 bash 脚本(一个脚本),当我们第一次运行时执行第一个任务,第二次运行脚本时执行第二个任务,第三次运行时执行第一个任务,第四次运行时执行第二个脚本?

【问题讨论】:

  • 你可以有一个外部文件来存储它运行的次数。

标签: linux bash


【解决方案1】:

是的,通过记住/坚持上次运行的内容很容易做到这一点。

$ 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
$ 

您也可以在 current_counter 文件中使用布尔值 0 或 1,或者仅通过检查文件的存在来使用,但它仅适用于这种情况。如果您有更多的任务(不仅仅是 2 个),比如 3 个甚至 100 个,上面的脚本很容易扩展;那么您只需要更改模数并添加更多条件来处理每个残基。

【讨论】:

  • 你可以放弃|| [[ -n "$line" ]] hack;它只需要容纳不能正确以换行符结尾的无效 POSIX 文本文件(即 Windows 文本文件)。
  • 我用 cat 而不是 while,我发现它更容易理解。
【解决方案2】:

我发现 ritesht93 脚本太难阅读,所以我自己制作了。

Bash 版本

运行脚本会提供以下输出:

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

首先我们创建一个带有数字 1 的文件:

{ ~ }  » echo 1 > counter

脚本内容(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 版本(如果需要)

我也有时间在 python 中做:

首先我们创建一个带有数字 1 的文件:

{ ~ }  » echo 1 > counter

脚本内容:

 #!/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")

【讨论】:

    【解决方案3】:

    这比前两个答案更容易。只需创建一个空的控制文件。 Touch control 并使用 rm control 删除,例如。

    运行时检查它是否存在,如果存在则销毁并运行第二个任务,否则运行第一个并创建它。

    您无需检查文件中的任何内容,只需检查其存在即可。

    编辑

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

    您可以通过使用不同的名称作为下一个脚本来添加更多任务:

    #!/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
    

    无论如何,我认为将其用于 2 个以上的任务并不是一个好主意。

    【讨论】:

    • 我喜欢你的想法。您应该为此提供一个脚本。无论如何,这将限制两个任务的执行。读取文件增加了超过 2 次执行的灵活性。另一件事是我总是避免在脚本中使用 rm 。但在某些情况下会是一个不错的方法。
    • 我添加了脚本以防你想看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2013-04-19
    • 2011-07-20
    • 2014-03-25
    • 2014-09-19
    相关资源
    最近更新 更多