Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: How to combine commands in the terminal?

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Beans
    369
    Distro
    Ubuntu 12.04 Precise Pangolin

    How to combine commands in the terminal?

    I would like to combine a number of install and uninstall commands in the terminal. For instance, "sudo apt-get install vlc", and in the same single terminal command, also entering "sudo apt-get remove mplayer", and many more, all as one single long command made up of lots of these separate sections.

    I would like to compile my long command in Gedit and then paste it into the terminal after a fresh system boot, hit enter and then just watch everything arrive and depart as desired.

    How should I go about writing a command, and separating the separate commands within the command, in order to do this?

  2. #2
    Join Date
    Mar 2007
    Location
    SoCal
    Beans
    70
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to combine commands in the terminal?

    you can alias it in your .bashrc file ...

    (ex.)
    alias update='sudo apt-get update -qy; sudo apt-get upgrade -qy; sudo apt-get autoremove'

    you could also use '&&' in between the commands.
    Lenovo S10e - Ubuntu 9.10 Remix
    Inspiron 530n - DB w/ Ubuntu 9.04 & WinXP_Pro
    Main_svr-1 - Ubuntu 8.04.2 LTS Server
    Bkup_svr-2 - Ubuntu 8.04.2 LTS Server

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Beans
    369
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How to combine commands in the terminal?

    So to confirm, typing in the terminal:

    Code:
    sudo apt-get install vlc && sudo apt-get remove mplayer
    would install VLC and remove Mplayer. both at the same time?

  4. #4
    Join Date
    Nov 2006
    Location
    40.31996,-80.607213
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: How to combine commands in the terminal?

    Quote Originally Posted by Pipps View Post
    So to confirm, typing in the terminal:

    Code:
    sudo apt-get install vlc && sudo apt-get remove mplayer
    would install VLC and remove Mplayer. both at the same time?
    Since dpkg locks the program from initating multiple times, thus corrupting the package database, both programs will not run simultaneously. Of course, using '&&' is like saying 'run next command after the previous one exited with a status of zero'. If the previous command failed to complete with an exit status of zero (meaning, the previous command failed), then the next command will not start.

    If you want to run one command and then another, whether or not the previous command completed successfully, then use ';' to join the commands.
    "Security lies within the user of who runs the system. Think smart, live safe." - Dr Small
    Linux User #441960 | Wiki: DrSmall

  5. #5
    Join Date
    Mar 2009
    Beans
    89
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: How to combine commands in the terminal?

    you could just write a script like this:
    Code:
    #!/bin/sh
    #32 bit machine only Ubuntu 8.10
    script_name="thescript.sh"
    
    # Script must run as root
    if [ $USER != "root" ]; then
            echo "You need to run this script as root."
            echo "Use 'sudo ./$script_name' then enter your password when prompted."
            exit 1
    fi
    
    # Update the package manager
    sudo apt-get update
    
    
    # Add Medibuntu to your software sources
    sudo wget http://www.medibuntu.org/sources.list.d/hardy.list -O /etc/apt/sources.list.d/medibuntu.list
    
    # Add the Medibuntu gpg key
    wget -q http://packages.medibuntu.org/medibuntu-key.gpg -O- | sudo apt-key add - && sudo apt-get update
    
    # Update the software list
    sudo apt-get update
    
    # Install Ubuntu Restricted Extras
    sudo apt-get -y install ubuntu-restricted-extras
    
    # Install alien
    sudo apt-get -y install alien
    just add whatever you want and save it as file.sh. The code in the beginning is to make sure your running as root.

    You can also put:
    Code:
    sudo apt-get -y remove alien
    Just do a
    Code:
    sudo apt-get update
    At the end of the file to make sure everything is updating.

    Then all you would have to do is run this.
    Code:
    sudo ./file.sh
    Last edited by D3ath; March 18th, 2009 at 09:07 PM.
    -D3ath-
    XFX nForce 750a SLI Socket AM2 MoBo - AMD Athlon X2 5800+ 3.0GHz OEM Processor, 8GB PC6400 Corsair TWINX, 500GB SATA 2, GeForce 9600 GSO, ATX Clear Side Mid-Tower Case, 750 Watt Power Supply, 32 Inch 1080p Monitor.

  6. #6
    Join Date
    Jun 2008
    Location
    UK
    Beans
    369
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How to combine commands in the terminal?

    Quote Originally Posted by Dr Small View Post
    ... use ';' to join the commands.
    Brilliant! Thank you, Sir!

  7. #7
    Join Date
    Jun 2008
    Location
    UK
    Beans
    369
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How to combine commands in the terminal?

    Quote Originally Posted by D3ath View Post
    you could just write a script like this:...
    Whoa... that looks beautiful, yet rather technical, too! Thank you!

  8. #8
    Join Date
    Mar 2009
    Beans
    89
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: How to combine commands in the terminal?

    Quote Originally Posted by Pipps View Post
    Whoa... that looks beautiful, yet rather technical, too! Thank you!
    I wrote one of those for new installs on Ubuntu 8.10 for friends or someone new trying ubuntu. I can take my drive over to their house install the system and run the script and they all have the same things installed. Least for those type of users what they might like. I also have a very long one that installs everything that i need and want then removes all the stuff i don't like to use at all. Its very handy!
    -D3ath-
    XFX nForce 750a SLI Socket AM2 MoBo - AMD Athlon X2 5800+ 3.0GHz OEM Processor, 8GB PC6400 Corsair TWINX, 500GB SATA 2, GeForce 9600 GSO, ATX Clear Side Mid-Tower Case, 750 Watt Power Supply, 32 Inch 1080p Monitor.

  9. #9
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: How to combine commands in the terminal?

    Take two commands, command_1 and command_2

    # Executes command 1 then command 2 (not at the same time)
    command_1; command_2

    # Execute command 1 and 2 at the same time (in the background):
    command_1 &
    command_2 &

    # Executes command 2 only if command 1 is completed without errors.
    # If command 1 generates an error , command 2 will not run
    command_1 && command_2

    # Executes command 1 or command 2.
    # Command 2 runs only if command 1 fails.
    command_1 || command_2
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  10. #10
    Join Date
    Jun 2008
    Location
    UK
    Beans
    369
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How to combine commands in the terminal?

    Quote Originally Posted by D3ath View Post
    ...I also have a very long one that installs everything that i need and want then removes all the stuff i don't like to use at all. Its very handy!
    It is exactly the same reason that I am hoping to write something similar! Thanks!

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •