LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script for seeds in ns2 (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-seeds-in-ns2-894755/)

umair ahmed 07-30-2011 11:59 AM

Shell script for seeds in ns2
 
HI,I wanna run the same tcl script for ten different seeds using random number ,and then take out the average,how do I do that ?

David the H. 08-01-2011 09:46 AM

Well, you can start by giving us some details to work with. Don't just assume that we will know what you are talking about.

What is ns2?
What exactly is this tcl script? How do you "run it with random seeds", and what kind of output does it give?
What exactly is the "average" you want? Where in the output (I assume) are the numbers that need averaging? How exactly do you need them to be totaled and presented?
What environment are you using? Standard bash? Some other shell?


Finally, what have you tried already, if anything? Have you searched to see if anyone has already done what you want, or something similar that can be adapted to your needs?

szboardstretcher 08-01-2011 09:52 AM

I can't answer the rest, but I can answer a couple of David's questions.

NS2 is the linux network simulator. It's a nice program and capable of simulating an entire network rather than having to buy Cisco Lab equipment or something comperable.

TCL is the tool command language. Its used in a few different applications, but NS2 uses it as its network configuration language.

I hope this helps flesh out some background for the problem the OP is having. Cheers.

Edit: here are links:

http://isi.edu/nsnam/ns/
http://en.wikipedia.org/wiki/Tcl

Sorry for the wikipedia entry, I couln't find a better explanation. :study:

umair ahmed 08-13-2011 02:00 AM

[B]HI I posted the question
Quote:

HI,I wanna run the same tcl script for ten different seeds using random number ,and then take out the average,how do I do that ?
earlier ,which I couldnot explain fully ,still some information I need to gather than I will ask again ,but today I my question is this :
I am using setdest utility in ns2 to generate movement scenarios,it runs like this
umair@umair-VirtualBox:~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest$ ls

[I] addrmap-example calcdest.o Makefile make-scen-steadystate.csh scene1 scene-30-0-20 setdest setdest.h
calcdest emulate Makefile.in README scene-10-0-20 scene-40-0-20 setdest2.cc setdest.o
calcdest.cc gentracefiles make-scen.csh rng.o scene-20-0-20 scene-50-0-20 setdest.cc
umair@umair-VirtualBox:~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest$ ./setdest

usage:

<original 1999 CMU version (version 1)>
./setdest -v <1> -n <nodes> -p <pause time> -M <max speed>
-t <simulation time> -x <max X> -y <max Y>

OR
<modified 2003 U.Michigan version (version 2)>
./setdest -v <2> -n <nodes> -s <speed type> -m <min speed> -M <max speed>
-t <simulation time> -P <pause type> -p <pause time> -x <max X> -y <max Y>
(Refer to the script files make-scen.csh and make-scen-steadystate.csh for detail.)

umair@umair-VirtualBox:~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest$ ./setdest -v 1 -n 20 -p 10 -M 32 -t 300 -x 500 -y 674 >scenario

now i need to generate this multiple time with different parameters like changing -p pause time like 20,30,49 and so on so I got the shell script from the website
Code:

#!/bin/bash

dest_dir="scenario1"

if [ -d $dest_dir ]
then
        # Do nothing
        echo "'$dest_dir' is a directory"
else
        echo "Creating directory $dest_dir";
        mkdir --verbose $dest_dir
fi

setdest_loc=" ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest";


# Create the scenarios

for i in 0 10 20 40 100
do
        $setdest_loc -v 1 -n 25 -p $i -M 20 -t 100 -x 500 -y 500 > $dest_dir/scen-25-$i
done

echo ""
echo "Created the following files"
echo ""
ls -la $dest_dir/scen-25*

but when I run it ,it creates the directory but with empty files in it
I get the following results
scenario1' is a directory
./scenario.sh: line 21: ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest: No such file or directory
./scenario.sh: line 21: ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest: No such file or directory
./scenario.sh: line 21: ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest: No such file or directory
./scenario.sh: line 21: ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest: No such file or directory
./scenario.sh: line 21: ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest: No such file or directory

Created the following files

-rw-r--r-- 1 umair umair 0 2011-08-13 11:59 scenario1/scen-25-0
-rw-r--r-- 1 umair umair 0 2011-08-13 11:59 scenario1/scen-25-10
-rw-r--r-- 1 umair umair 0 2011-08-13 11:59 scenario1/scen-25-100
-rw-r--r-- 1 umair umair 0 2011-08-13 11:59 scenario1/scen-25-20
-rw-r--r-- 1 umair umair 0 2011-08-13 11:59 scenario1/scen-25-40

and all the created files are empty .

David the H. 08-13-2011 01:26 PM

The script isn't finding the location of your program. You're giving it a bad path.

Tilde (~) expansion doesn't happen inside quoted strings, so your variable ends up containing a literal "~". And tilde expansion happens before parameter expansion, so it remains literal when you use it later as a command.

Change this:
Code:

setdest_loc="~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest";
To this:
Code:

setdest_loc="$HOME/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/setdest"
You should also put quotes around all the variable expansions in your script.

Other than that, I don't see any major problems with the script. There are a couple of little things I'd do differently, but that's just me.

umair ahmed 08-14-2011 12:26 AM

@David the H.
thanks for the solution ,your solution worked for me but I found a different way myself and it's working ,
Code:

#!/bin/bash

dest_dir="scenario1"

if [ -d $dest_dir ]
then
        # Do nothing
        echo "'$dest_dir' is a directory"
else
        echo "Creating directory $dest_dir";
        mkdir -v $dest_dir
fi

# Create the scenarios
for i  in 0 10 20 40 100
do
script=cd ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/./setdest        -v 1 -n 25 -p $i -M 20 -t 100 -x 500 -y 500 > $dest_dir/scen-25-$i
done

echo ""
echo "created the following files "
ls  $dest_dir/scen-25*

don't know whether it is the rightful programming approach or not .( I only so a few shell tutorial on youtube)
you tell me which one is better and also
Quote:

There are a couple of little things I'd do differently, but that's just me.
what would've you done ,?

David the H. 08-14-2011 09:51 AM

That's certainly cleaner. But this is not right.

Code:

script=cd ~/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest/./setdest        -v 1 -n 25 -p $i -M 20 -t 100 -x 500 -y 500 > $dest_dir/scen-25-$i
This first sets a variable called "script" to the value "cd" that's never used. Perhaps that's just a mistake? Then it runs your setdest command, with an odd "." addition to the path. And you still haven't quoted the variables, like I suggested.

In any case, it really isn't much different from the first script. It still works as written. You've just removed the extra variable, which is one of the things I would've considered doing.

The modifications I'd make would be minor and mostly cosmetic. The script is mostly just a simple loop after all.

You might consider setting up the command as a function, which makes it easier to modify if needed. The options to it can be held in an array inside the function*.

I think I would simply cd into the destination directory first and run the command from there, to avoid having to worry about using the full path in the command.

Also, [[ is usually better than [, and adding the -p option to mkdir will allow you to modify the script to create further subdirectories, if needed. Finally, you should always define exit codes for a script. That lets you use it in other scripts.

Here's my idea:
Code:

#!/bin/bash

dest_dir="scenario1"

# Define a function for the command to run.  $1 is the input value to use.
setdest_loc() {

        # Store the options to the command in an array first, just to keep things readable.
        local -a options=( -v 1 -n 25 -p "$1" -M 20 -t 100 -x 500 -y 500 )

        "$HOME/ns-allinone-2.34/ns-2.34/indep-utils/cmu-scen-gen/setdest" "${options[@]}" > "scen-25-$i"

}

# Create the destination directory if necessary.
if [[ -d "$dest_dir" ]]; then
        # Do nothing
        echo "'$dest_dir' is a directory"
else
        echo "Creating directory $dest_dir"
        mkdir -pv "$dest_dir"
fi

#cd into the destination directory, and run the commands from there.
cd "$dest_dir" || echo "$dest_dir not available.  exiting." ; exit 1

# Create the scenarios.
# Call the function once for each number.

for i in 0 10 20 40 100 ; do
        setdest_loc "$i"
done

# List the output files.
echo
echo "created the following files "

ls ./scen-25*

exit 0

Again though, with a script as simple as this there's really not much purpose in worrying about the minor details. As long as it works and you've avoided any major flaws (like not quoting variables), you're ok.

~~~

*In general variables are for holding strings of data, not code. If you have a sequence of strings, you use an array. While you can hold individual elements of a command inside variables, the command as a whole should be set inside a function: http://mywiki.wooledge.org/BashFAQ/050


All times are GMT -5. The time now is 06:42 AM.