Меню Рубрики

Run script from script linux

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 Easy (rss)
Root privileges No
Requirements None
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

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:

#!/bin/bash # My first shell script # Author: nixCraft # —————————- echo «Hello $USER.» echo «Today is $(date)» echo «Current working directory : $(pwd)»

Save and close the file. Set the permission using the chmod command:
$ chmod +x hello.sh
Run the script as follows:
$ ./hello.sh

If the current directory is in the PATH variable, you can avoid typing the ./ before the hello.sh. It is a good idea to create your own bin directory as follows:
$ mkdir $HOME/bin
Add $HOME/bin to the PATH variable using bash shell export command:
$ export PATH=$PATH:$HOME/bin
$ echo $PATH
Move hello.sh in $HOME/bin using the mv command, run:
$ mv hello.sh $HOME/bin
Execute the script:
$ hello.sh
Sample outputs:

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:

Источник

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can’t find any option similar to Right ClickOpen WithOther Application.Terminal.

How do I open this file in the terminal?

16 Answers 16

Give execute permission to your script:

And to run your script:

Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:

/rem4space.sh;SLL=$(cat $FILENAME|head -1|sed ‘s:^#!\(.*\):\1:g’);[ ! -z $SLL ] && exec $SLL $FILENAME;sh $FILENAME) . edit FILENAME to your liking. Also note that sh will be used if there is no alternative. – MiJyn Jun 19 ’13 at 3:50

You need to mark shell scripts as executable to run them from the file manager:

Right click on your .sh file and select Properties:

In the Permissions tab, check Allow executing file as program:

Close the Properties window and double-click the file. A dialog will pop up giving you the option to run the script in a terminal:

Open a terminal and navigate to the folder where the .sh file is located. Then type:

Prerequisite

Before you can run the .sh file, you need to make it executable:

  1. Right-click on the file
  2. Select Properties
  3. Select Permissions
  4. Select Allow executing file as a program

Warning

Make sure you trust the source where you got the file from. It could be a virus.

The very simple way

This has problem. The terminal will close immediately and you will not be able to see the output.

The simple way

  1. Open Applications -> Accessories -> Terminal
  2. Drag and drop the .sh file into the terminal and press Enter

The way professionals do it

Find where the .sh file

  • Use the ls and cd commands
  • ls will list the files and folders in the current folder. Give it a try: type «ls» and press Enter.
  • Once you see the folder that you want to go in to, run cd , followed by a space, followed by a folder name
  • If you when into a folder that you did not want, run cd .. to go one level up

Once you can see for example script1.sh with ls run this:

Why do it the complicated way?

The terminal has a rich set of powerful tools that are accessible by typing the commands. Professionals locate the .sh file by typing ls and cd . Once you are in the correct current folder you can run the script like this:

or you can run and redirect the output to a file:

or you can filter the output for keywords (e.g. «apples») an then redirect to a file:

There are thousands of things you can to to that file just by typing a few commands.

Another one, you can download a file from the Internet with one simple command:

And then open the file like this:

On Ubuntu 13.04 executable files opened in Nautilus are now opened in gedit by default rather than prompting the user to execute them. To enable the classic behavior you need to adjust the preferences:

Nautilus → Edit menu → Preferences → Behaviour tab → Click the radio button near Ask each time.

Go to the directory where the .sh file is by using cd . In this example I have stored my sh file as

first do pwd to figure out where you are, and if it returns /home/username (where username is your real username), you can run

If you seem to be somewhere else, you can use the absolute path

these are all ways of describing the same place. Once you’ve made it to the location of your script, type

If you can see the sh file in the output, you can use chmod to make it executable. In my case, remember, the filename is test.sh , so I would run

Now that we are in the same directory as the script, we have to specify to the shell that we want to execute the file by giving its location ./ (the current directory followed by a path separator, to distinguish it from the filename). To run my file I would type:

If your script has been written correctly it will run without errors.

Here’s a live example:

Источник

How can I run a function from a script in command line?

I have a script that has some functions.

Can I run one of the function directly from command line?

Something like this?

9 Answers 9

If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the source or . command and then simply call the function. See help source for more information.

Well, while the other answers are right — you can certainly do something else: if you have access to the bash script, you can modify it, and simply place at the end the special parameter «$@» — which will expand to the arguments of the command line you specify, and since it’s «alone» the shell will try to call them verbatim; and here you could specify the function name as the first argument. Example:

For polish, you can first verify that the command exists and is a function:

The following command first registers the function in the context, then calls it:

You can import all of the functions in the script into your environment with source ( help source for details), which will then allow you to call them. This also has the effect of executing the script, so take care.

There is no way to call a function from a shell script as if it were a shared library.

This script will run functions fun1 and fun2 but if you start it with option fun1 or fun2 it’ll only run given function with args(if provided) and exit. Usage

Edit: WARNING — seems this doesn’t work in all cases, but works well on many public scripts.

If you have a bash script called «control» and inside it you have a function called «build»:

Then you can call it like this (from the directory where it is):

If it’s inside another folder, that would make it:

If your file is called «control.sh», that would accordingly make the function callable like this:

I have a situation where I need a function from bash script which must not be executed before (e.g. by source ) and the problem with @$ is that myScript.sh is then run twice, it seems. So I’ve come up with the idea to get the function out with sed:

sed -n «/^func ()/,/^>/p» myScript.sh

And to execute it at the time I need it, I put it in a file and use source :

sed -n «/^func ()/,/^>/p» myScript.sh > func.sh; source func.sh; rm func.sh

you can call function from command line argument like below

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Переводчик для mac os safari
  • Переводчик promt для mac os
  • Переводчик google для mac os
  • Перевод this cd is only intended for use in macintosh mac os x computers
  • Перевести audition на русский на mac os