Sunday, September 8, 2013

SCRIPT BASIC

You can choose any name for the file. File name should not be same as any of the Unix built-in commands.Script always starts with the two character '#!' which is called as she-bang. This is to indicate that the file is a script, and should be executed using the interpreter (/bin/bash) specified by the rest of the first line in the file.Execute the script as shown below. If you have any issues executing a shell script, refer toshell script execution tutorial$ bash helloworld.sh Hello WorldWhen you execute the command "bash helloworld.sh", it starts the non-interactive shell and passes the filename as an argument to it.


SCRIPT SYNTAX
--

$ cat helloworld.sh #!/bin/bash echo Hello World

--

$ bash helloworld.sh Hello World


--


$ cat ~/.bash_profile hname=`hostname` echo "Welcome on $hname." echo -e "Kernel Details: " `uname -smr` echo -e "`bash --version`" echo -ne "Uptime: "; uptime echo -ne "Server time : "; date lastlog | grep "root" | awk {'print "Last login from : "$3 print "Last Login Date & Time: ",$4,$5,$6,$7,$8,$9;}'







----

Create a script by typing the following two lines into a file using your favourite editor.$ cat helloworld.sh #!/bin/bash echo Hello World



Print the last login details

If multiple users are using the same machine with same login, then details like the machine from which the last login happened, and time at which they logged in, would be the most useful details. This example prints the last login details during the starting of an interactive shell.$ cat ~/.bash_profile hname=`hostname` echo "Welcome on $hname." echo -e "Kernel Details: " `uname -smr` echo -e "`bash --version`" echo -ne "Uptime: "; uptime echo -ne "Server time : "; date lastlog | grep "root" | awk {'print "Last login from : "$3 print "Last Login Date & Time: ",$4,$5,$6,$7,$8,$9;}'



When you login to an interactive shell, you will see the welcome messages as shown below.login as: root root@dev-db's password: Welcome on dev-db


Change the permission on the script to allow you(User) to execute it, using the command "chmod u+x helloworld.sh".Directory containing the script should be included in the PATH environment variable. If not included, you can execute the script by specifying the absolute path of the script.echo is a command which simply outputs the argument we give to it. It is also used to print the value of the variable


Get the list of usernames in /etc/passwd file

This sed example displays only the first field from the /etc/passwd file.$sed 's/\([^:]*\).*/\1/' /etc/passwd root bin daemon adm lp sync shutdown

No comments:

Post a Comment