Sponsored Content
Top Forums Shell Programming and Scripting How to run several bash commands put in bash command line? Post 303011707 by drysdalk on Monday 22nd of January 2018 07:00:21 PM
Old 01-22-2018
Hi,

If you're using Bash, then the simplest way to do this is just to separate them with semi-colon (;) characters. Commands separated by semi-colons will be executed one after the other, regardless of the success or failure of the preceding command.

Example:

Code:
$ echo "This is the output of the first command." ; /bin/ls -F / ; echo "This is the output of the third command."
This is the output of the first command.
bin/   dev/  home/                 lib/    lost+found/  mnt/  proc/  run/   srv/  tmp/  var/
boot/  etc/  installCDE.15787.log  lib64/  media/       opt/  root/  sbin/  sys/  usr/
This is the output of the third command.
$

There are a few other ways to do this. If you only want the next command in the chain to be executed if the previous command was a success, then you can use a logical 'and' operator, represented by two ampersand (&) symbols:

Code:
$ echo "First command." && /bin/true && echo "The two previous commands returned 0, so we get to run our third."
First command.
The two previous commands returned 0, so we get to run our third.
$

And lastly, if you only want a command chain to continue if the previous command fails rather than succeeds, you can use the logical 'or' operator, represnted by a double pipe (|) symbol:

Code:
$ echo "First command". || /bin/true || echo "This time, we won't see the third command."
First command.
$

Hope this helps.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

2. Shell Programming and Scripting

how to run non-standard commands in bash script?

Hello All. I suspect that this will be a clear noob question, but I haven't been able to figure it out using the usual methods, so I turn to you. I've written a script to create input files for the quantum chemistry program NWCHEM. Generally you create an input file and then execute it by... (12 Replies)
Discussion started by: EinsteinMcfly
12 Replies

3. AIX

Typing "bash" at the command line spawns two bash processes

Server: IBM p770 OS: AIX 6.1 TL5 SP1 When one of our develoeprs types "bash" on the command line to switch shells, it hangs. For some reason, two bash processes are created....the first bash process spawns a second bash process in the same console, causing a hang. Anyone have any idea what... (2 Replies)
Discussion started by: wjssj
2 Replies

4. Shell Programming and Scripting

[bash] run a shell who runs commands

Hi all. On X11 I'm on a shell ...shell_1 (/bin/bash). From here I want to open another shell window shell_2 who executes commands like "ls -l" or programs like ". /program"... so the "result" of commands shows in shell_2 window and not in shell_1. Is that possible ? (4 Replies)
Discussion started by: jerold
4 Replies

5. Shell Programming and Scripting

How i can put the result of a command inside a bash variable?

#!/bin/bash #... for i in `ls -c1 /usr/share/applications` do name="cat $i | grep ^Name= | cut -d = -f2" echo $name #... done Now inside name as output is present: while i want only the result of the command. Ideally i would like obtain that information using only bash ... or... (8 Replies)
Discussion started by: alexscript
8 Replies

6. Shell Programming and Scripting

Bash if run a command

I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string. This works perfectly. echo $(ipcs | grep Shared | awk '{print $2}')When I put it in an if statement I get some problems. $ if ; then echo expression evaluated... (10 Replies)
Discussion started by: cokedude
10 Replies

7. Shell Programming and Scripting

Put a command into router through sshpass bash script

hello, i am facing some issue. I am using a simple bash script that via sshpass put a command into router. Now, problem is that i have a file and commands into it. sshpass -p $pass ssh -o $log -n $user@$h /ip address set address=10.0.0.1/24 so if I have that command ip address set ... (0 Replies)
Discussion started by: tomislav91
0 Replies

8. Solaris

Run automated bash commands from sh login shell

I use plink.exe to automate remote commands that return data to Windows machines. This works well on newer servers running Red Hat since the commands were developed for bash and the designated user's login shell is bash. I need to also support older servers which are running Solaris 10 but the... (5 Replies)
Discussion started by: randman1
5 Replies

9. Shell Programming and Scripting

Run command through html+cgi in bash

Hi everyone, I want to kill process through the web, so I create html page with single bottom that run kill command in shell script with CGI. Here is html code: <td><form METHOD="GET" action="http://IP:port/cgi_bin/script.cgi" > <input type="submit" value= "Submit" > <INPUT name="q"... (7 Replies)
Discussion started by: indeed_1
7 Replies

10. UNIX for Beginners Questions & Answers

Compilation error when I run Bash configuration command

Hi, I downloaded source code file from The GNU website and changed the source code of ls.c file, added printf command to it. It worked fine. Then, I deleted the printf command, saved the file and ran the command 'make sudo && make install' closed the terminal and printf statement went away. I... (1 Reply)
Discussion started by: akanksha1509
1 Replies
EXEC(3) 								 1								   EXEC(3)

exec - Execute an external program

SYNOPSIS
string exec (string $command, [array &$output], [int &$return_var]) DESCRIPTION
exec(3) executes the given $command. PARAMETERS
o $command - The command that will be executed. o $output - If the $output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as , is not included in this array. Note that if the array already contains some elements, exec(3) will append to the end of the array. If you do not want the function to append elements, call unset(3) on the array before passing it to exec(3). o $return_var - If the $return_var argument is present along with the $output argument, then the return status of the executed command will be written to this variable. RETURN VALUES
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru(3) function. To get the output of the executed command, be sure to set and use the $output parameter. EXAMPLES
Example #1 An exec(3) example <?php // outputs the username that owns the running php/httpd process // (on a system with the "whoami" executable in the path) echo exec('whoami'); ?> NOTES
Warning When allowing user-supplied data to be passed to this function, use escapeshellarg(3) or escapeshellcmd(3) to ensure that users can- not trick the system into executing arbitrary commands. Note If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends. Note When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable. Warning With safe mode enabled, the command string is escaped with escapeshellcmd(3). Thus, echo y | echo x becomes echo y | echo x. SEE ALSO
system(3), passthru(3), escapeshellcmd(3), pcntl_exec(3), backtick operator. PHP Documentation Group EXEC(3)
All times are GMT -4. The time now is 07:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy