Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to make a script that runs a line of commands one by one on CMD?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

How to make a script that runs a line of commands one by one on CMD?

rEDrED Member
edited April 2017 in Help

Hi,

I need some programming help, i know its not the 'BEST' place to ask but wth..

On each server I need to do a manual copy pasting of around 18 commands in CMD one by one, its like:

  1. yum install -y wget
  2. yum install -y unzip
  3. yum install -y zip
  4. wget http://link.com/file.zip
  5. unzip file.zip
  6. cd file
  7. sh run.sh

etc upto 18 commands in total. Manual entry is required in only 3 of the commands above, rest is automatic.

I need a way where the commands should run one by one.

any help is appreciated..

thankss!

inb4 LowEndProgramming.

Comments

  • Bash script (.sh) should work fine

    Thanked by 1rED
  • rEDrED Member

    @jetchirag said:
    Bash script (.sh) should work fine

    yes a .sh file but I am clueless on how to actually do it/make one..

  • YmpkerYmpker Member
    edited April 2017

    @rED said:

    @jetchirag said:
    Bash script (.sh) should work fine

    yes a .sh file but I am clueless on how to actually do it/make one..

    Create a file called example.sh
    Then paste your commands (one per line):
    https://pastebin.com/FNcLWTuj

    Then Save&Exit the file.

    Chmod +x example.sh

    ./example.sh


    You might wanna add this to the top of example.sh: #!/bin/sh

    Thanked by 2jetchirag rED
  • xrzxrz Member
    edited April 2017

    yum install -y wget && yum install -y unzip && yum install -y zip && wget http://link.com/file.zip && unzip file.zip && cd file && whatever

    Thanked by 1rED
  • @xrz said:

    yum install -y wget && yum install -y unzip && yum install -y zip && wget http://link.com/file.zip && unzip file.zip && cd file && whatever

    Yes, that's the most efficient way :D

  • rEDrED Member

    @Ympker said:

    @rED said:

    @jetchirag said:
    Bash script (.sh) should work fine

    yes a .sh file but I am clueless on how to actually do it/make one..

    Create a file called example.sh
    Then paste your commands (one per line):
    https://pastebin.com/FNcLWTuj

    Then Save&Exit the file.

    Chmod +x example.sh

    ./example.sh


    You might wanna add this to the top of example.sh: #!/bin/sh

    @xrz said:

    yum install -y wget && yum install -y unzip && yum install -y zip && wget http://link.com/file.zip && unzip file.zip && cd file && whatever

    Thanks guyss looks super easy.. as I said there a few commands that requires manual intervention, hope thats not an issue? Or is there a specific piece of code that i need to add for that?

    I am guessing when the script will run until that command and when I type manually after that the rest of the script will continue to run, right?

    Like for each server I would need to assign a domain so there is a command somewhere in that list of cmds that asks for the domain name, so i enter the domain name > the later part of the script is continued..

    Or am i overthinking? Lol.

  • jetchiragjetchirag Member
    edited April 2017

    You can use read command.

    Suppose you need to add domain name in add command | Imaginary command of mine lol

    Then use:
    https://pastebin.com/vSapiU87

    Thanked by 1rED
  • WSSWSS Member

    Put

    #!/bin/sh
    at the very top of the script. That tells it which program to run, and this is the most common, but some people use #!/bin/bash.

    By default your script will execute everything in order. If it waits for you to answer something, that's what it will do. If you want to answer something ahead of time, and the program accepts STDIN, you can echo/yes/etc to it. Learning shell scripting is better put to many available online manuals, but this should be enough to get you running.

    An easy way to pass data to your script is to use variables. The first variable you pass to it is usually $1, $2, $3, etc... and $* is the whole command line

    so you could do a

    for n in $*; do
      echo $n
    done
    

    to see just how that works. If you want to drop something (usually you want to drop $0, which is the script itself, you can shift it. I strongly recommend browsing through "BASH For beginners".

    You can also add all of those yum install statements into a single command line, last time I checked

    yum install -y wget unzip zip ... et al.

    Thanked by 1rED
  • rEDrED Member

    @jetchirag said:
    You can use read command.

    Suppose you need to add domain name in add command | Imaginary command of mine lol

    Then use:
    https://pastebin.com/vSapiU87

    Not sure how will it work? The domain will be unique for each server, from where will it read the domain name? Hostname? Sorry i am uber n00b.

  • @rED said:

    @jetchirag said:
    You can use read command.

    Suppose you need to add domain name in add command | Imaginary command of mine lol

    Then use:
    https://pastebin.com/vSapiU87

    Not sure how will it work? The domain will be unique for each server, from where will it read the domain name? Hostname? Sorry i am uber n00b.

    The read command will ask you for input

    Thanked by 1rED
  • WSSWSS Member

    You really should take an hour out and read my link above, which will explain all of these things as reading variables, redirection, command line, et al..

    Thanked by 1rED
  • rEDrED Member

    @WSS said:
    Put

    #!/bin/sh
    at the very top of the script. That tells it which program to run, and this is the most common, but some people use #!/bin/bash.

    By default your script will execute everything in order. If it waits for you to answer something, that's what it will do. If you want to answer something ahead of time, and the program accepts STDIN, you can echo/yes/etc to it. Learning shell scripting is better put to many available online manuals, but this should be enough to get you running.

    An easy way to pass data to your script is to use variables. The first variable you pass to it is usually $1, $2, $3, etc... and $* is the whole command line

    so you could do a

    for n in $*; do
      echo $n
    done
    

    to see just how that works. If you want to drop something (usually you want to drop $0, which is the script itself, you can shift it. I strongly recommend browsing through "BASH For beginners".

    You can also add all of those yum install statements into a single command line, last time I checked

    yum install -y wget unzip zip ... et al.

    Thanks a lot for taking time to write all that, I appreciate it.

    Hmm I won't be needing to ans anything ahead of time, as it prompts I will answer it at that time.

    So after that the script will resume right..

    That last line will save some time, I did read that somewhere but forgot until i saw it again now. Thanks..

    Btw all this is on centos 6 not sure if it matters!?

  • do as @wss says, all the info is contained in his post.

  • rEDrED Member

    @WSS said:
    You really should take an hour out and read my link above, which will explain all of these things as reading variables, redirection, command line, et al..

    Yup, I will read it.. doesn't seem that hard at all.
    I did java course some time ago and have some programming experience, so should be easy to grasp.

    Thanks again!!

  • rEDrED Member
    edited June 2017

    err. wrong thread!
    sorry ignore plss.

Sign In or Register to comment.