How to run multiple Unix commands in one time?

前端 未结 8 1253
一向
一向 2021-02-06 02:04

I\'m still new to Unix. Is it possible to run multiple commands of Unix in one time? Such as write all those commands that I want to run in a file, then after I call that file,

相关标签:
8条回答
  • 2021-02-06 02:25

    If you want to use multiple commands at command line, you can use pipes to perform the operations.

    grep "Hello" <file-name> | wc -l
    

    It will give number of times "Hello" exist in that file.

    0 讨论(0)
  • 2021-02-06 02:26

    We can run multiple commands in shell by using ; as separator between multiple commands

    For example,

    ant clean;ant
    

    If we use && as separator then next command will be running if last command is successful.

    0 讨论(0)
  • 2021-02-06 02:39

    Sure. It's called a "shell script". In bash, put all the commands in a file with the suffix "sh". Then run this:

    chmod +x myfile.sh
    

    then type

    . ./myFile
    

    or

    source ./myfile
    

    or just

    ./myfile
    
    0 讨论(0)
  • 2021-02-06 02:42

    To have the commands actually run at the same time you can use the job ability of zsh

    $ zsh -c "[command1] [command1 arguments] & ; [command2] [command2 arguments]"

    Or if you are running zsh as your current shell:

    $ ping google.com & ; ping 127.0.0.1

    The ; is a token that lets you put another command on the same line that is run directly after the first command.

    The & is a token placed after a command to run it in the background.

    0 讨论(0)
  • 2021-02-06 02:43

    Short answer is, yes. The concept is known as shell scripting, or bash scripts (a common shell). In order to create a simple bash script, create a text file with this at the top:

    #!/bin/bash
    

    Then paste your commands inside of it, one to a line.

    Save your file, usually with the .sh extension (but not required) and you can run it like:

    sh foo.sh
    

    Or you could change the permissions to make it executable:

    chmod u+x foo.sh
    

    Then run it like:

    ./foo.sh
    

    Lots of resources available on this site and the web for more info, if needed.

    0 讨论(0)
  • 2021-02-06 02:43

    you can also use a semicolon ';' and run multiple commands, like : $ls ; who

    0 讨论(0)
提交回复
热议问题