- How to run shell script in Linux
- How to run shell script in Linux
- Step 1. Create a shell script
- Step 2. Run the shell script on Linux
- Step 3. How to execute test.sh program
- Understanding test.sh shell script
- Conclusion
- How To Run the .sh File Shell Script In Linux / UNIX
- How do I run .sh file shell script in Linux?
- Syntax
- How to run .sh file as root user
- How to use chmod command to run .sh shell script in Linux
- How do I run .sh file named install.sh?
- GUI method to run .sh file
- Running .sh file shell script and debugging options
- Conclusion
- Run shell scripts in your Linux VM by using Run Command
- Benefits
- Restrictions
- Available commands
- Azure CLI
- Azure portal
- PowerShell
- Limiting access to Run Command
- Next steps
How to run shell script in Linux
How to run shell script in Linux
- Use a text editor such as vi or nano to write Linux shell script: vi script-name
- Make shell script executable by typing chmod +x script-name
- Run Linux shell script by typing “ ./script-name ” OR “ bash script-name “
Let us see all examples and usage in details.
Step 1. Create a shell script
Type nano command or vi command to create a new shell script named test.sh:
vi test.sh
Append the following text. Press ESC and type I to insert text:
To save a file, press ESC key and type 😡 .
Step 2. Run the shell script on Linux
Try to run it:
./test.sh
Sample outputs:
The command ./test.sh displayed an error message on the screen. It will not run script since you have not set execute permission for your script test.sh.
Step 3. How to execute test.sh program
Run the following command to set up executable permission on test.sh script:
chmod +x test.sh
Run your script:
./test.sh
You can use full path too:
/path/to/test.sh
So if test.sh stored in /home/vivek/, run:
/home/vivek/test.sh
Understanding test.sh shell script
The first line is:
#!/bin/bash
The #! syntax used in shell scripts to indicate an interpreter for execution. It is called a shebang or a “bang” line. All scripts under Linux execute using the interpreter specified on a first line. The second line display Hello message to user:
echo «Hello, $USER»
The $USER is a shell variable.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Conclusion
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How To Run the .sh File Shell Script In Linux / UNIX
I have downloaded software for my Linux or Unix-like system from the Internet. There is a file called install.sh. How do I run an .sh file to install the software in macOS? How do your run .sh files from command line?
You can open or run .sh file in the terminal on Linux or Unix-like system. The .sh file is nothing but the shell script to install given application or to perform other tasks under Linux and UNIX like operating systems. The easiest way to run .sh shell script in Linux or UNIX is to type the following commands. Open the terminal (your shell prompt) and type the commands.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux or Unix-like systems |
Est. reading time | 3 mintues |
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
Let us see script examples and usage in details.
Syntax
The syntax is:
sh file.sh
OR
bash file.sh
How to run .sh file as root user
Some time you need root access to install application; without root, you won’t have the necessary permissions to install application or make system level modifications. Root access is disabled by default on many Linux and UNIX like systems. Simply use the sudo or su command as follows:
sudo bash filename.sh
Type your password. Another option is to use the su command as follows to become superuser:
su —
Type root user password and finally run your script:
bash filename.sh
How to use chmod command to run .sh shell script in Linux
Another recommend option is to set an executable permission using the chmod command as follows:
chmod +x file.sh
Now your can run your .sh file as follows
./file.sh
/path/to/file.sh
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How do I run .sh file named install.sh?
Just run the following two command:
chmod +x install.sh
./install.sh
The dot (.) refers to the current working directory. The install.sh is in the current directory so you run it as above or as follows too:
bash install.sh
OR
sh install.sh
GUI method to run .sh file
- Select the file using mouse.
- Right-click on the file.
- Choose Properties:
- Click Permissions tab.
- Select Allow executing file as a program:
- Now click the file name and you will be prompted. Select “Run in the terminal” and it will get executed in the terminal.
Running .sh file shell script and debugging options
Pass the -x to debug shell script when running on your system. It print commands and their arguments as they are executed. For example:
bash -x script-name.sh
sh -x script-name.sh
Want to show shell input lines as they are read. Try passing the -v option:
bash -v script-name
We can combine both options. In other words, try it as follows:
bash -x -v backup.sh
For more information see how to improve your bash/sh shell script with ShellCheck lint script analysis tool and other bash debugging hints here.
Conclusion
You learned how to run .sh file shell script using combination of the chomod and dot (.) or sh/bash command. We can use any one of the following command execute shell script on Linux and Unix-like systems:
See the following resources for more info:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
anybody tell by which coomand i will check the modified date of files/dir in linux o/s
date -s “2 OCT 2006 18:00:00”
if we want to install software from cd.. how should be the command? i’m the newbie
rashid, if you want to install software from the cd you should try synaptic package handler or apt-get .
if the software is usually from the cd it will tell you to insert it
How can i execute this command export PS1=”\[[33[01;32m\]\u@\h\[33[01;34m\] \W]\]#” from a .sh script for example and make it work ?
Hey Black_Ps`
There are a few ways to do this but if you are not familiar with some of the nuances of Linux, it might be easier to create a new folder to place the file in (or all of your scripts in) like home/username/Desktop/scripts (substituting the actual user name in place of username). The following instructions are based on Fedora 10 with GNOME desktop so if you are running a different Linux distro, you may or may not have a few slight variances. Once you have a good place to store your scripts, you can use the GUI to help with creation of the shell script. Next, click on the “Applications” menu and highlight “Accessories” and then select “Text Editor” Type the following command #!/bin/bash and then press the enter key. Now, type the command you listed into the text editor and then select the “Save As” option. Now name your file (highly recommended that you don’t name it with spaces and make the name all lower case to help with ease of use later) and save it to your new folder you created…remember to give it a .sh name extension. Now close the text editor and go back to the “Applications” menu and highlight “System Tools” and then select “Terminal”. Type the following command cd /home/username/Desktop/scripts (again substituting the actual user name in place of username). The command ls and then press the enter key. Did it return the name of your script as a file in that folder? If so, move on to the next step. If it did not, navigate to the folder where you saved this file and move it to the correct folder. Type the following command chmod 755 yourfilename.sh. That’s it, you now have an executable shell script that will execute your command. You can do so by double clicking on it from the GUI, calling it from the terminal like so bash /home/username/Desktop/scripts/yourfilename.sh or you could put it on the cron to run unattended if you needed to. I hope this helps some or at least points you in the right direction.
can you please expalin it more detailes.
Ok thanks i’ll give it a try
When i open the terminal in UNIX..something like interactive keyboard authentication bla.. bla.. is coming..nd its difficult me to go on typing my commands and navigating through the directors and files..how can i fix this. any idea…? any command? anything helpful will be highly appreciated. _thanks_
Источник
Run shell scripts in your Linux VM by using Run Command
Applies to: вњ”пёЏ Linux VMs вњ”пёЏ Flexible scale sets
The Run Command feature uses the virtual machine (VM) agent to run shell scripts within an Azure Linux VM. You can use these scripts for general machine or application management. They can help you to quickly diagnose and remediate VM access and network issues and get the VM back to a good state.
Benefits
You can access your virtual machines in multiple ways. Run Command can run scripts on your virtual machines remotely by using the VM agent. You use Run Command through the Azure portal, REST API, or Azure CLI for Linux VMs.
This capability is useful in all scenarios where you want to run a script within a virtual machine. It’s one of the only ways to troubleshoot and remediate a virtual machine that doesn’t have the RDP or SSH port open because of improper network or administrative user configuration.
Restrictions
The following restrictions apply when you’re using Run Command:
- Output is limited to the last 4,096 bytes.
- The minimum time to run a script is about 20 seconds.
- Scripts run by default as an elevated user on Linux.
- You can run one script at a time.
- Scripts that prompt for information (interactive mode) are not supported.
- You can’t cancel a running script.
- The maximum time a script can run is 90 minutes. After that, the script will time out.
- Outbound connectivity from the VM is required to return the results of the script.
To function correctly, Run Command requires connectivity (port 443) to Azure public IP addresses. If the extension doesn’t have access to these endpoints, the scripts might run successfully but not return the results. If you’re blocking traffic on the virtual machine, you can use service tags to allow traffic to Azure public IP addresses by using the AzureCloud tag.
Available commands
This table shows the list of commands available for Linux VMs. You can use the RunShellScript command to run any custom script that you want. When you’re using the Azure CLI or PowerShell to run a command, the value that you provide for the —command-id or -CommandId parameter must be one of the following listed values. When you specify a value that is not an available command, you receive this error:
Name | Description |
---|---|
RunShellScript | Runs a Linux shell script. |
ifconfig | Gets the configuration of all network interfaces. |
Azure CLI
The following example uses the az vm run-command command to run a shell script on an Azure Linux VM.
To run commands as a different user, enter sudo -u to specify a user account.
Azure portal
Go to a VM in the Azure portal and select Run command in the left menu, under Operations. You see a list of the available commands to run on the VM.
Choose a command to run. Some of the commands might have optional or required input parameters. For those commands, the parameters are presented as text fields for you to provide the input values. For each command, you can view the script that’s being run by expanding View script. RunShellScript is different from the other commands, because it allows you to provide your own custom script.
The built-in commands are not editable.
After you choose the command, select Run to run the script. After the script finishes, it returns the output and any errors in the output window. The following screenshot shows an example output from running the ifconfig command.
PowerShell
The following example uses the Invoke-AzVMRunCommand cmdlet to run a PowerShell script on an Azure VM. The cmdlet expects the script referenced in the -ScriptPath parameter to be local to where the cmdlet is being run.
Limiting access to Run Command
Listing the run commands or showing the details of a command requires the Microsoft.Compute/locations/runCommands/read permission. The built-in Reader role and higher levels have this permission.
Running a command requires the Microsoft.Compute/virtualMachines/runCommand/action permission. The Virtual Machine Contributor role and higher levels have this permission.
You can use one of the built-in roles or create a custom role to use Run Command.
Next steps
To learn about other ways to run scripts and commands remotely in your VM, see Run scripts in your Linux VM.
Источник