- Running multiple commands in one line in shell
- 6 Answers 6
- How To Execute a Command with a Shell Script in Linux
- Prerequisites
- Getting Started
- Creating and Running a Basic Shell Script
- Using Variables in Shell Scripts
- Reading Input from the Command Line
- Defining Functions
- Conclusion
- Run .sh File Commands All in Once in Linux
- a. How to Run .sh file through the Terminal ?
- b. How to Run the sh file using the Graphical interface ?
- [Need help in fixing Linux system issues ? We can help you . ]
- Conclusion
Running multiple commands in one line in shell
So, /templates/apple will be copied to /templates/used AND /templates/inuse and then after that I’d like to remove the original.
Is cp the best way to do this, followed by rm ? Or is there a better way?
I want to do it all in one line so I’m thinking it would look something like:
cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple
Is this the correct syntax?
6 Answers 6
You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:
cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple
cp /templates/apple /templates/used && mv /templates/apple /templates/inuse
To summarize (non-exhaustively) bash’s command operators/separators:
- | pipes (pipelines) the standard output ( stdout ) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
- |& pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
- && executes the right-hand command of && only if the previous one succeeded.
- || executes the right-hand command of || only it the previous one failed.
- ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.
Why not cp to location 1, then mv to location 2. This takes care of «removing» the original.
And no, it’s not the correct syntax. | is used to «pipe» output from one program and turn it into input for the next program. What you want is ; , which seperates multiple commands.
cp file1 file2 ; cp file1 file3 ; rm file1
If you require that the individual commands MUST succeed before the next can be started, then you’d use && instead:
cp file1 file2 && cp file1 file3 && rm file1
That way, if either of the cp commands fails, the rm will not run.
How To Execute a Command with a Shell Script in Linux
Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.
Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, and variables. Scripts are useful for simplifying a complex series of commands and repetitive tasks.
In this article, you will learn how to create and execute shell scripts for the command line in Linux.
Prerequisites
To complete this tutorial, you will need:
- Familiarity with using the terminal.
- Familiarity with a text editor.
- Familiarity with commands like chmod , mkdir , and cd .
Getting Started
A shell script needs to be saved with the extension .sh .
The file needs to begin with the shebang line ( #! ) to let the Linux system know which interpreter to use for the shell script.
For environments that support bash , use:
For environments that support shell , use:
This tutorial assumes that your environment supports bash .
Shell scripts can also have comments to increase readability. A good script always contains comments that help a reader understand exactly what the script is doing and the reasoning behind a design choice.
Creating and Running a Basic Shell Script
You can create a shell script using the vi editor, a cat command, or a text editor.
For this tutorial, you will learn about creating a shell script with vi :
This starts the vi editor and creates a basic_script.sh file.
Then, press i on the keyboard to start INSERT MODE . Add the following lines:
This script runs the commands whoami and date . whoami displays the active username. date displays the current system timestamp.
To save and exit the vi editor:
Finally, you can run the script with the following command:
You may get output that resembles the following:
Outputroot Fri Jun 19 16:59:48 UTC 2020
The first line of output corresponds to the whoami command. The second line of output corresponds to the date command.
You can also run a script without specifying bash :
Running the file this way might require the user to give permission first. Running it with bash doesn’t require this permission.
Output~bash: ./basic_script.sh: Permission denied
The command bash filename only requires the read permission from the file.
Whereas the command ./ filename , runs the file as an executable and requires the execute permission.
To execute the script, you will need to update the permissions.
This command applies chmod and gives x (executable) permissions to the current user.
Using Variables in Shell Scripts
Scripts can include user-defined variables. In fact, as scripts get larger in size, it is essential to have variables that are clearly defined and that have self-descriptive names.
Add the following lines to the script:
#!/bin/bash # This is a comment # defining a variable GREETINGS="Hello! How are you" echo $GREETINGS
GREETINGS is the variable defined and later accessed using $ (dollar sign symbol. There should be no space in the line where variables are being assigned a value.
This prints out the value assigned to the variable:
When the script is run, GREETINGS is defined and accessed.
Reading Input from the Command Line
Shell scripts can be made interactive with the ability to accept input from the command line. You can use the read command to store the command line input in a variable.
Add the following lines to the script:
#!/bin/bash # This is a comment # defining a variable echo "What is your name?" # reading input read NAME # defining a variable GREETINGS="Hello! How are you" echo $NAME $GREETINGS
A variable NAME has been used to accept input from the command line. This script waits for the user to provide input for NAME . Then it prints NAME and GREETINGS .
OutputWhat is your name? Sammy Sammy Hello! How are you
In this example, the user has provided the prompt with the name: Sammy .
Defining Functions
Users can define their own functions in a script. These functions can take multiple arguments.
Add the following lines to the script:
#!/bin/bash #This is a comment # defining a variable echo "What is the name of the directory you want to create?" # reading input read NAME echo "Creating $NAME . " mkcd () mkdir "$NAME" cd "$NAME" > mkcd echo "You are now in $NAME"
This script asks the user for a directory name. Then, it uses mkdir to create the directory and cd into it.
OutputWhat is the name of the directory you want to create? test_dir Creating test_dir . You are now in test_dir
In this example, the user has provided the prompt with the input: test_dir . Next, the script creates a new directory with that name. Finally, the script changes the user’s current working directory to test_dir .
Conclusion
In this article, you learned how to create and execute shell scripts for the command line in Linux.
Consider some repetitive or time-consuming tasks that you frequently perform that could benefit from a script.
Continue your learning with if-else , arrays, and arguments in the command line.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Run .sh File Commands All in Once in Linux
In Linux distributions, the bash shell environment is the key element for managing the system components. Several commands are executed on the bash shell to perform a specific task. All bash commands are mostly stored in a bash script file with the ‘ .sh ‘ extension. The .sh file is a shell script that is used to install an application or to perform various jobs in Linux / UNIX operating systems. The main benefit of writing the shell script is that you do not need to write different commands again on terminal windows.
Here at LinuxAPT , we shall look into how to create and run the .sh file or bash scripts using the terminal on the Linux system.
a. How to Run .sh file through the Terminal ?
Here, you will learn how to run sh file using the Terminal application.
1. To create .sh file in Linux:
Start by creating a shell script file that we will run through the terminal. Open the text editor and add the following content to this file:
$ echo "Hello this is my first shell script."
Then, Save it with ‘ testscript.sh ‘.
After creating the sh script file, it is important to set some executable permissions. Otherwise, you will receive an error of ‘ permission denied ‘ on your screen.
To make this sh file executable use this command:
Here, name of the file is ‘testscript.sh’:
You can verify whether the required file is executable or not by executing the following command:
The output you will see on the terminal that represents sh file as executable.
Using the two different methods, you can run sh script using the terminal on a Linux system.
The first method to run sh script is by calling the name of the file with its complete:
The alternative method to run sh file is by executing the bash command:
The current user with having sudo privileges can also run the sh file as:
You can also debug the shell script by using the ‘ -x ‘ option:
b. How to Run the sh file using the Graphical interface ?
If you are using the GUI environment in Linux, you can also run sh file in the following steps:
- Select sh file that you want to open on your Linux system.
- Right-click on the file icon and choose ‘ Properties ‘ from the drop-down list.
- Now, go into the ‘ Permissions ‘ tab and check the box ‘ Allow executing file as a program ‘.
- Click on the file. You will select ‘ Run in Terminal ‘ and you will notice it will be executed in the terminal.
[Need help in fixing Linux system issues ? We can help you . ]
Conclusion
This article covers how to run sh files using the terminal as well as using the command line or terminal. In fact, the .sh file is nothing but the shell script to install given application or to perform other tasks under Linux like operating systems.
How do I run .sh file shell script in Linux?
The procedure to run the .sh file shell script on Linux is as follows:
- Open the Terminal application on Linux or Unix.
- Create a new script file with .sh extension using a text editor.
- Write the script file using nano script-name-here.sh.
- Set execute permission on your script using chmod command : chmod +x script-name-here.sh .
- To run your script : ./script-name-here.sh
- Another option is as follows to execute shell script: $ sh script-name-here.sh OR bash script-name-here.sh .