Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple commands in Linux? Make a programm? [closed]

Tags:

linux

command

First of all, I would like to let you know that I am new to Linux. I did my search, but I don't really know what it is that I am looking for.

What I need is to make a simple program, that executes different commands one after another in terminal, because I don't want to run them by hand every time I need it. How do I make it? I suppose I can't just make an executable file that would trigger on double click, or can I?

Update:
When I posted this question I was just a kid who somehow managed to install a Linux based OS. I didn't know anyone else who had Linux or anyone in CS for that matter. I didn't have anyone to ask the "stupid questions". I didn't know how to express myself. I was looking for a shell script guide, but all I found for "linux programming" was C, which was way out of my depth then.

like image 381
Teodors Avatar asked Sep 19 '25 15:09

Teodors


2 Answers

The simple program you are referring to is usually called a "shell script". Essentially, you just collect a number of commands you want in a file, and execute them. You can also have some sort of flow control (loops, if-else statements etc) for more complex scripts.

To build a simple bash shell script, let's call it myscript.sh, follow these steps:

At the first line of the "myscript.sh" source file put (see Note below)

#!/bin/bash

then your commands, for instance for demonstration purposes

echo "hello, I'm coming from the script file"
ls -l

Save the file.

To make this file "executable" type

$ chmod +x myscript.sh

and then you should be able to run this file from the command line with

$ ./myscript.sh

You'd see the output of the echo command "hello, I'm coming from the script file", followed by a directory listing in long format.

Here are links for a bash tutorial and a Bash Guide for Beginners.

Finally, sometimes you can just string a couple of commands together using aliases, though most people just "alias" shorter versions of common commands creating abbreviations for commands.

Note re location of bash:

To find out where your bash shell is you can always type which bash, it will return a path/directory, so put that at the top if it's different. For instance if it says /usr/bin/bash you'd put #!/usr/bin/bashthat in place of #!/bin/bash as the first line of your script.

like image 103
Levon Avatar answered Sep 22 '25 10:09

Levon


try writing a shell script:

#!/bin/bash

command1
command2 | grep 'something' | pager
foo | perl -ne 'print'

Save is as something.sh.

Mark it as executable with chmod +x something.sh.

Then cd to the directory where it resides and run ./something.sh.

Whether you can double-click start your script it depends on your configuration, but as most scripts create text output this doesn't often make sense. Use .desktop files to create shortcuts to a single (!) command (or whatever filetype your Desktop Environment (gnome/kde/xfce) perfers).

If your needs go beyond some simple bash, take a look at scripting languages like the object-oriented Python or the classic *nix administration language Perl.

View @Levon's post for further details.

like image 24
amon Avatar answered Sep 22 '25 08:09

amon