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.
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?
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:
- yum install -y wget
- yum install -y unzip
- yum install -y zip
- wget http://link.com/file.zip
- unzip file.zip
- cd file
- 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
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
Yes, that's the most efficient way
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.
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
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
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.
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
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..
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.
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!!
err. wrong thread!
sorry ignore plss.