How execute bash script line by line?

前端 未结 5 2139
逝去的感伤
逝去的感伤 2020-12-02 05:08

If I enter bash -x option, it will show all the line. But the script will execute normaly.

How can I execute line by line? Than I can see if it do the correct thing,

5条回答
  •  执笔经年
    2020-12-02 05:41

    If your bash script is really a bunch of one off commands that you want to run one by one, you could do something like this, which runs each command one by one when you increment a variable LN, corresponding to the line number you want to run. This allows you to just run the last command again super easy, and then you just increment the variable to go to the next command.

    Assuming your commands are in a file "it.sh", run the following, one by one.

    $ cat it.sh
    echo "hi there"
    date
    ls -la /etc/passwd
    
    $ $(LN=1 && cat it.sh | head -n$LN | tail -n1)
    "hi there"
    
    $ $(LN=2 && cat it.sh | head -n$LN | tail -n1)
    Wed Feb 28 10:58:52 AST 2018
    
    $ $(LN=3 && cat it.sh | head -n$LN | tail -n1)
    -rw-r--r-- 1 root wheel 6774 Oct 2 21:29 /etc/passwd
    

提交回复
热议问题