LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-30-2011, 12:59 PM   #1
umair ahmed
LQ Newbie
 
Registered: Jul 2011
Location: karachi,pakistan
Distribution: ubuntu
Posts: 13

Rep: Reputation: Disabled
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 ?
 
Old 08-01-2011, 10:46 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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?
 
1 members found this post helpful.
Old 08-01-2011, 10:52 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,383
Blog Entries: 1

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
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.

Last edited by szboardstretcher; 08-01-2011 at 10:54 AM.
 
Old 08-13-2011, 03:00 AM   #4
umair ahmed
LQ Newbie
 
Registered: Jul 2011
Location: karachi,pakistan
Distribution: ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Smile

[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 .

Last edited by umair ahmed; 08-13-2011 at 06:44 AM.
 
Old 08-13-2011, 02:26 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
1 members found this post helpful.
Old 08-14-2011, 01:26 AM   #6
umair ahmed
LQ Newbie
 
Registered: Jul 2011
Location: karachi,pakistan
Distribution: ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Smile

@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 ,?
 
Old 08-14-2011, 10:51 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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
 
1 members found this post helpful.
  


Reply

Tags
ns2


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
script tcl NS2 MounaRM Linux - Networking 0 01-10-2011 09:49 AM
NS2 Script wanted. Will pay :D zeeshan12 Linux - Networking 2 06-09-2008 09:40 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 06:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration