How do I execute a script under UNIX or Linux like operating system using command prompt?
First, you need to set the execute permissions on your script. This can be done using the chmod command as follows:
$ chmod +x your-script-name-here
OR
$ chmod u+x your-script-name-here
Next, check that you got the permissions that you want with the ls -l command:
$ ls -l your-script-name-here
Finally, you can execute a script using any one of the following syntax:
$ ./your-script-name-here
OR
$ bash your-script-name-here
OR
$ sh your-script-name-here
OR
$ ksh /nas/server/scripts/my.ksh
Bash Shell Script Example
Create a shell script called test.sh:
$ vi test.sh
Add the following code:
#!/bin/bash # test.sh - A sample shell script # Author - Vivek Gite # Tested under Debian GNU/Linux using Bash shell version 4.x # ----------------------------------------------------------------------------- echo "Hello $USER," echo "Current date and time set to \"$(date)\" on the \"$HOSTNAME\" system." echo "Amount of free and used memory in the system:" free -m
Save and close the file. Run it as follows:
$ chmod u+x test.sh
$ ./test.sh
Sample outputs:
Hello vivek,
Current date and time set to "Fri Jul 15 15:24:12 IST 2011" on the "sai.nixcraft.net.in" system.
Amount of free and used memory in the system:
total used free shared buffers cached
Mem: 7930 3699 4230 0 78 2115
-/+ buffers/cache: 1505 6424
Swap: 8114
More links and information :
- Chapter 2: Getting Started With Shell Programming from the nixCraft Shell scripting wiki.
- man chmod
- man bash
- man ls
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 0 comments… add one now }