[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Open
  • Priority: Medium
  • Security: Public
  • Views: 53
  • Last Modified:

Linux basic scripting (similar to batch script in Windows)

I have to build a Buch of machines in work and have 30 odd linux commands to run. We use CentOS and I'm trying to find out if I can automate the process a little by having a script that cycles through the commands so I don't have to keep copy and pasting them.

I'm new to linux and could write a batch script in Windows but don't know what the linux equivalent is to search in google.
I would like to keep each command on its own line in a file, then have a window that basically asks the user "do you want to run this command xxxxxxxxxxx"
The user then presses "y" and it goes onto the next one etc etc.

Is this easy to do in linux?
0
PJ0302917
Asked:
PJ0302917
  • 3
  • 2
6 Comments
 
Travis MartinezSmoke JumperCommented:
It is rather easy with the "read" statement in your script.  This is kind of difficult to expand upon without further information.
0
 
Travis MartinezSmoke JumperCommented:
Here's a basic example and output:

$ while [ $x -eq 1 ]
> do
> echo "hello world /n"
> echo "would you like to continue (y/n)"
> read answer
> if [ "$answer" = n ]
> then $((x++))
> fi
> done
hello world /n
would you like to continue (y/n)
y
hello world /n
would you like to continue (y/n)
y
hello world /n
would you like to continue (y/n)
0
 
arnoldCommented:
As Travis pointed out with an example, the answer is yes.

There are many possibilities, but lack of information on what the tasks are, it is hard to answer
The short answer is yes, sh, bash, zsh, ksh, csh are the basic scripting somewhat equivalent on a level to batch scripting since they rely on the execution of other commands to do things.
There are other scripting option as does Windows that are more robust in the same comparison vbscript, powershell on Windows such as perl, TCl/ti, Python, php.....

The optimal choice depends on what the tasks are.
Getting user input is the least as read in the example provides, once you get the user input what then?

The shell, batch scripting requires that you harness the response from one command and can pass it to the next one, the more robust scripting avail the option to handle the flow in a more seamless wait, less probe to typo, or oversight that while you expect the prior respinse to be passed to the next command..

If the task is simple, execute the following commands in a sequence without interdependency I.e. Execution of this command does not depend on the execution of the prior.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
PJ0302917Author Commented:
Hi guys, thanks for looking into my question and helping me out. I've not used Linux much and a Shell Script seems to be the way to go.
As far as additional details go, I'd like to code the following.
1) A master script which the user runs and interacts with
2) Configuration files which contain commands such as make directory, copy files, change privileges etc. There could be a number of configuration files but only one master script.

So the master script will start up and list all the configuration files found in its local directory. User inputs number 4 to load configuration file 4.

I guess the master script will use the "read" command to iterate each line of the configuration file and execute the command.

In the master script the user is asked if they want to run "mkdir Test_Files"   (this is the first line of Configuration File 4)
User presses 'Y'
master script then states "mkdir Test_Files successful"
master script then asks if they want to run "cd Test_Files"    (this is the second line of Configuration File 4)
User presses 'Y' etc etc
0
 
arnoldCommented:
Where does this master script create files?
Issue, to create thing outside the user's home dir will require the script to run with elevated rights.
0
 
Travis MartinezSmoke JumperCommented:
Are you more familiar with PowerShell?  You can install the shell as a package on the base CentOS image.  Not sure if that would help put the scripting knowledge back in the wheelhouse you're familiar with:

https://blogs.msdn.microsoft.com/powershell/2017/02/01/installing-latest-powershell-core-6-0-release-on-linux-just-got-easier/
0

Join & Write a Comment

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now