How to run scripts under Linux‏

翻译 2013年12月04日 23:23:44

How do I run a Linux shell script? How can I run a script in Linux operating system using command line options?

By default shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:

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
Run a script called backup.bash using BASH shell:
$ bash backup.bash

Example

Create a shell script called hello.sh using a text editor such as vi or gedit:
 
#!/bin/bash
echo "Hello $USER."
echo "Today is $(date)"
echo "Current working directory : $(pwd)"
 
Save and close the file. Set the permission:
$ chmod +x hello.sh
Run the script:
$ ./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:
Hello vivek.
Today is Thu Nov 10 17:49:15 IST 2011
Current working directory : /nafiler05/users/v/vivek/bin/demos

相关文章推荐

How to use "chmod" under linux

This "chmod" command is be used quite a lot. and I cry for understanding and being familiar with thi...

how to compile and run cocos2dx in linux

I believe for android development, linux is much better than windows. Originally development in visu...

How to change the default run level of a RedHat 9.0 or Fedora Core Linux system

During the boot process for Redhat 9.0 and Fedora Core systems the init command opens the /etc/ini...
  • hhcjb
  • hhcjb
  • 2011年12月15日 13:21
  • 315

How to install QEMU under FreeBSD 8.1 as host

QEMU is a processor emulator that relies on dynamic binary translation to achieve a reasonable speed...

How to Post Data and Fetch Remote Pages from PHP Scripts

http://phpstarter.net/2008/12/how-to-post-data-and-fetch-remote-pages-from-php-scripts/ Published: ...
  • maroee
  • maroee
  • 2012年03月04日 15:47
  • 526

HOW-TO:Debug Python Scripts with Eclipse

HOW-TO:DebugPython Scripts with Eclipse From XBMC Jump to: navigation,search Here is amethod yo...
  • lzy_168
  • lzy_168
  • 2011年11月17日 13:52
  • 2038

How to install flash player under ubuntu

  • 2014年04月26日 14:13
  • 216B
  • 下载

Linux 启动顺序 添加开机启动服务 rc?.d rc.local init.d System run levels and init.d scripts

Ubuntu 的 运行级别
  • me10zyl
  • me10zyl
  • 2014年07月27日 10:13
  • 1617

This howto guide shows you’ll how to enable EPEL repository under RHEL/CentOS 6/5 to install additio

This howto guide shows you’ll how to enable EPEL repository under RHEL/CentOS 6/5 to install addit...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to run scripts under Linux‏
举报原因:
原因补充:

(最多只允许输入30个字)