Tutorial on Parameter Substitution at Runtime

Today, we are presenting a very simple & useful thing i.e. Parameter Substitution in Unix at run-time.We must know that mainly there are four types of parameter substitutions which are commonly used in Unix Scripts.The Bourne shell has a handy set of operators for testing and setting shell variables.These are presented below with some quick examples.This will run in almost all the shells but as we all know exceptions are always there , You can use parameter substitution operators in any command line. You’ll see them used with the colon ( : ) operator , checking or setting default valuesLet us know see what are the types of parameter substations

1.${parameter:-value}
This Type of parameter substitution is used if the value is defined already for the parameter,So now it will use that value otherwise use the above one.Means if Variable is undefined or assigned a null string it must get the default value :For Example lets have a scenario where we have ${EDITOR:-/bin/vi}

In this case it will use the default EDITOR value if it defined otherwise it will use vi.We can use this feature in a program which prompts for a file name and use default value if the user simply press enter

echo “Enter file Name : \c”
read filename
f_name=${filename:-emp.lst}

2.${parameter:+ value}
This one will substitute the value if parameter is defined otherwise it will substitute nothing. (just opposite of – option).It simply override the value of parameter, if parameter is null, it will never override.

OPT_MODE=P
echo “Mode : ${ OPT_MODE:+T}”
output : Mode : T

As it was defined it will override its value.This feature can be used to set a variable to the output of a command and echo a message if the variable is no null.

found_file=`ls *log`
echo ${found:+”There are log”}

3.${parameter:=value}
It works similarly for (- option) except that it goes further and makes the assignment to the variable that is evaluated.With = option , we can use parameter substitution with a command without making intermediate assignment.

echo “enter file name : \c”
read filename
grep $pattern ${filename:=emp.lst} # filename is now assigned
X=1; while [ $x –le 10 ] can be combined with while [ ${x:=1} –le 10 ]

4.${parameter : ?value}
If the parameter is not assigned and null , it echoes value and kill the shell.This is quite useful in terminating script if user fails to respond properly to shell directive.

echo “Enter the filename: \c”
read filename
grep ${filename:? “No file entered”}

If no file name is entered then message will be displayed and script is also aborted the use of explicit exit command.
NOTE : If you omit the colon ( : ) from the expressions , the shell doesn’t check for an empty parameter. In other words, the substitution will happen whenever the parameter is set. (That’s how some early Bourne shells work: they don’t understand a colon in parameter substitution.).The first substitution ( ${Value=default} ) will leave $Value empty because the variable has been set. The second substitution will set $Value to default because the variable has been set but is empty. The third substitution will leave $Value2set to stuff :

+Value=
Value2=stuff
: ${Value=default}
: ${Value:=default}
: ${Value2:=default}

How to run a program on a specific day, at a specific time in Linux

Problem:

Imagine a scenario where you need to run a program on a specific day, at a specific time. Lets say you need to run a program every Sunday at 3 AM. And you also need that the program sends you an email with its output and log to a file.

Solution:

Let us say that the program which I want to execute is named program. The email address to which the output is supposed to be mailed is assumed as [email protected] The log is saved to a file named mylog_file.log. This scheduling can be done by making a crontab file. The content of my crontab file (Let me name it as my_crontab_file) would be the following:

MAILTO=”[email protected]
0 03 * * sun /path/to/program > /path/to/mylog_file.log 2>&1

Field descriptions (from left to right):

0 – Minute (0-59)
03 – Hour (0-23)
* – Day of Month (1-31, * means all days of the month)
* – Month of Year (1-12, * means all months of the year)
sun – Day of the week (1-7 or sun-mon-tue-wed-thurs-fri-sat, * means all days of the week)

The program is run at that time and its output (along with error using 2>&1) is sent to a log file. This file is then needs to be declared as crontab file as following:

crontab my_crontab_file

How to run ex_ file by converting ex_ to exe file

Recently I came across a new type of file format which is “ex_” instead of “exe“. First I tried executing it by simply renaming it from “filename.ex_” to “filename.exe” but it did not work that way. “ex_” is actually compressed form of “exe” files and they need to be expanded. The most suitable way is to use command line. Follow the given steps:

  • Open the command prompt by going to “Start->All Programs->Accessories->Run” and then type “cmd” in Run window. Run can also be opened by pressing “Windows button + r
  • In the command prompt go to the folder where your file “filename.ex_” is stored. For ease I would suggest you to manually copy the file into C drive. Make a new folder named say “new
  • If you are in different directory than C then type “cd ..” and press enter. Do it a couple of time until you are :C directory.
  • Now use the command: expand filename.ex_ c:newfilename.exe
  • This command may result in following error: EXPAND.EXE Error Msg: Can’t Open Output File
  • The reason is very simple. You might not have write permission for the “filename.ex_” or for the folder “new” or for both
  • Right click on the file/folder and select properties. If the “Read Only” box is checked, de-select it.
  • Now run the previous command again. Use the expanded file in .exe format wherever you want.

Questions and Suggestions are welcomed. Enjoy :)

Sources: Microsoft

How to run or install .bin files in Ubuntu (Unix)

Being a beginner with Unix OS is a painful but interesting experience. I guess I learn almost everyday few things which might be very basic but definitely challenging for a newbie like me. Few days back I wanted to install Adobe Reader on my Ubuntu 10.10. Most of the ready to install packages for Ubntu are in .deb format which can be installed like an executable file in Windows. But this package was in .deb format. Interesting! I thought. This one is not click and install package but it requires no extra terrestrial effort for sure. Following steps should be taken:

  • Open the terminal and go in the directory where the /deb package is saved
  • Change the access rights / permission for the .deb file/package. Most easy way is to give all rights (read, write and execute) to all users by following command: chmod a+x name_of_file.bin
  • Now run the file with the following command: sudo ./name_of_file.bin

How to install and run Nessus on Ubuntu, Linux or other Unix like OS

installation on UbuntuNessus is a great network security tool but installing it correctly can be a real pain some time even for advance user. I faced few problems and that is why I noted down all the necessary and sufficient steps. The steps below are explained for Ubuntu but I think similar steps can be used for other Unix like OS.

Step1: Download the ubuntu package from the website:http://tenable.com/products/nessus/nessus-download-agreement
and Run it.
Step2: To start nessus, type this command in super user mode: sudo /etc/init.d/nessusd start
Output of this command will result in:

$Starting Nessus : .
takshak@takshak-NB:~$ Missing plugins. Attempting a plugin update…
Your installation is missing plugins. Please register and try again.
To register, please visit http://www.nessus.org/register

Step3. Go on the given link and resgister on the home version, check ur mail get the license and use the command specified in ur mail as super user. It will register the Nessus and will also downlaod the missing plugins.

Step4. Run the nessus by using command: sudo /etc/init.d/nessusd start
Running Nessus command nasl gives an error “nasl : command not found”

(i) Try this command: whereis nessus
output will look something like: nessus: /opt/nessus/bin/nessus

(ii) Check the path see if the mentioned directory is added in the path or not by using command: echo $PATH
If the output of the command is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
then the command is not added to the path

(iii) Add the dirctory to the path by using the command: export PATH=$PATH:/opt/nessus/bin

Step5: Then start nessus by using command: sudo /etc/init.d/nessusd start

Step6: To run a nasl file use: nasl -t target_ip filename.nasl