- How To Run a Script In Linux
- Examples that shows how to run a script in Linux
- How to execute a shell script from C in Linux?
- 6 Answers 6
- Bash Beginner Series #1: Create and Run Your First Bash Shell Script
- Create and run your first shell script
- Convert your shell script into bash script
- The SheBang line at the beginning of shell script
- Adding your shell script to the PATH (so that it can be run from any directory)
How To Run a Script In Linux
H ow do I run a Linux shell script? How can I run a script in Linux operating system using command line options?
By default, the shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | N/A |
chmod +x script-name-here
OR
chmod 0755 script.sh
Next, use the ls command to view permission on the script:
$ ls -l script-name-here
To execute the script, type:
$ ./script-name-here
You can also run a script using any one of the following syntax:
$ /path/to/shell/script/backup.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 ➔
Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh
To run a script called backup.bash using BASH shell:
$ bash backup.bash
Examples that shows how to run a script in Linux
Create a shell script called hello.sh using a text editor such as vi or gedit/nano:
nano hello.sh
OR
vim hello.sh
Append the following code:
Conclusion
You learned how to write a simple shell script and run a script in Linux operating system with help of chmod and other commands. Please see the following tutorials for more information on bash shell scripting under Linux or Unix-like operating systems:
🐧 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.
this is very good. It’s just missing one thing. the export command should be added in in the
/.bashrc file to survive across sessions.
$ tail -1
or the equivalent for your chosen shell (bs, ksh, tcsh etc …)
Just the solution I needed to figure this out, thanks man! 😀
Nice.
For compiling C prgms,
cc hello.c
./a.out
btw, Why don’t you have categories in WP?
Click on category icon (hint: Bash shell).
Hello Vivek, I was not aware that that image is a category link, too 🙂 It really looks like a post image. I’m referencing this site from time to time, and I’ve learned that you have categories just now! I’d really suggest using a different method for showing categories, unless you want to hide them 🙂 Thanks for the great site.
we can run a script even with out permissions using source command
sourch scriptname.sh
no dear if u want to run script the u hv to give execute permission on that file
like….scriptname.sh
chmod 755 or 777 scriptname.sh
Don’t be a patronizing shit…
hi. i m facing problem to write for setup ip address ,subnet and gateway in linux
kindly suggest me how i do…
if u have the give me idea…
#include
int main()
<
system(“c:\\windows\\your system name\\ip config”);
return 0;
>
Good information. I am trying to run a sql using a shell command.
Please provide the info..
How to run the Unix command only during the even and odd hours only.
Kindly provide the command to incorporate in the script.
Hi,
I am having a problem related to this. I have downloaded an program and it has a GUI written in Java. I need to run the script to launch this program, with “sudo” privileges in order to having it running properly. I don’t want to navigate to the folder where this script is, every time I need it, therefore I first made it executable and added the folder to the PATH. Now, when I write “sudo script-name”, I get “script-name not command found”, if I write only “script-name”, it finds it but it doesn’t run properly. Is there a way to launch a script, that is in the PATH, with sudo privileges? Thank you in advance.
Run it as follows:
Or cd to /home/foo and run:
Thank you. I decide to add it as an alias: “alias script-name=’sudo bash /path/to/script/script-name”
What’s the difference with §. /PATH/TO/TARGET§ and §./PATH/TO/TARGET§ ?
thanx, im learning and your are a good teacher.
Источник
How to execute a shell script from C in Linux?
How can I execute a shell script from C in Linux?
6 Answers 6
It depends on what you want to do with the script (or any other program you want to run).
If you just want to run the script system is the easiest thing to do, but it does some other stuff too, including running a shell and having it run the command (/bin/sh under most *nix).
If you want to either feed the shell script via its standard input or consume its standard output you can use popen (and pclose ) to set up a pipe. This also uses the shell (/bin/sh under most *nix) to run the command.
Both of these are library functions that do a lot under the hood, but if they don’t meet your needs (or you just want to experiment and learn) you can also use system calls directly. This also allows you do avoid having the shell (/bin/sh) run your command for you.
The system calls of interest are fork , execve , and waitpid . You may want to use one of the library wrappers around execve (type man 3 exec for a list of them). You may also want to use one of the other wait functions ( man 2 wait has them all). Additionally you may be interested in the system calls clone and vfork which are related to fork.
fork duplicates the current program, where the only main difference is that the new process gets 0 returned from the call to fork. The parent process gets the new process’s process id (or an error) returned.
execve replaces the current program with a new program (keeping the same process id).
waitpid is used by a parent process to wait on a particular child process to finish.
Having the fork and execve steps separate allows programs to do some setup for the new process before it is created (without messing up itself). These include changing standard input, output, and stderr to be different files than the parent process used, changing the user or group of the process, closing files that the child won’t need, changing the session, or changing the environmental variables.
You may also be interested in the pipe and dup2 system calls. pipe creates a pipe (with both an input and an output file descriptor). dup2 duplicates a file descriptor as a specific file descriptor ( dup is similar but duplicates a file descriptor to the lowest available file descriptor).
Источник
Bash Beginner Series #1: Create and Run Your First Bash Shell Script
If you have to do it more than once, automate it!
You will often find yourself repeating a single task on Linux over and over again. It may be a simple backup of a directory or it could be cleaning up temporary files or it can even be cloning of a database.
Automating a task is one of the many useful scenarios where you can leverage the power of bash scripting.
Let me show you how to create a simple bash shell script, how to run a bash script and what are the things you must know about shell scripting.
Create and run your first shell script
Let’s first create a new directory named scripts that will host all our bash scripts.
Now inside this ‘scripts directory’, create a new file named hello.sh using the cat command:
Insert the following line in it by typing it in the terminal:
Press Ctrl+D to save the text to the file and come out of the cat command.
You can also use a terminal-based text editor like Vim, Emacs or Nano. If you are using a desktop Linux, you may also use a graphical text editor like Gedit to add the text to this file.
So, basically you are using the echo command to print «Hello World». You can use this command in the terminal directly but in this test, you’ll run this command through a shell script.
Now make the file hello.sh executable by using the chmod command as follows:
And finally, run your first shell script by preceding the hello.sh with your desired shell “bash”:
You’ll see Hello, World! printed on the screen. That was probably the easiest Hello World program you have ever written, right?
Here’s a screenshot of all the steps you saw above:
Convert your shell script into bash script
Confused? Don’t be confused just yet. I’ll explain things to you.
Bash which is short for “Bourne-Again shell” is just one type of many available shells in Linux.
A shell is a command line interpreter that accepts and runs commands. If you have ever run any Linux command before, then you have used the shell. When you open a terminal in Linux, you are already running the default shell of your system.
Bash is often the default shell in most Linux distributions. This is why bash is often synonymous to shell.
The shell scripts often have almost the same syntaxes, but they also differ sometimes. For example, array index starts at 1 in Zsh instead of 0 in bash. A script written for Zsh shell won’t work the same in bash if it has arrays.
To avoid unpleasant surprises, you should tell the interpreter that your shell script is written for bash shell. How do you do that? You use shebang!
The SheBang line at the beginning of shell script
The line “#!/bin/bash” is referred to as the shebang line and in some literature, it’s referred to as the hashbang line and that’s because it starts with the two characters hash ‘#’ and bang ‘!’.
When you include the line “#!/bin/bash” at the very top of your script, the system knows that you want to use bash as an interpreter for your script. Thus, you can run the hello.sh script directly now without preceding it with bash.
Adding your shell script to the PATH (so that it can be run from any directory)
You may have noticed that I used ./hello.sh to run the script; you will get an error if you omit the leading ./
Bash thought that you were trying to run a command named hello.sh. When you run any command on your terminal; they shell looks for that command in a set of directories that are stored in the PATH variable.
You can use echo to view the contents of that PATH variable:
The colon character (:) separates the path of each of the directories that your shell scans whenever you run a command.
Linux commands like echo, cat etc can be run from anywhere because their executable files are stored in the bin directories. The bin directories are included in the PATH. When you run a command, your system checks the PATH for all the possible places it should look for to find the executable for that command.
If you want to run your bash script from anywhere, as if it were a regular Linux command, add the location of your shell script to the PATH variable.
First, get the location of your script’s directory (assuming you are in the same directory), use the PWD command:
Use the export command to add your scripts directory to the PATH variable.
Notice that I have appended the ‘scripts directory’ to the very end to our PATH variable. So that the custom path is searched after the standard directories.
The moment of truth is here; run hello.sh:
It works! This takes us to the end of this tutorial. I hope you now have some basic idea about shell scripting.
Since I introduced you to PATH variable, stay tuned for the next bash scripting tutorial where I discuss shell variables in detail.
Источник