LinuxQuestions.org
Register a domain and help support LQ
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 02-08-2010, 08:24 AM   #1
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Rep: Reputation: 17
Unhappy problem in writing a script?


hi dear all,

i have written a script in which, i changes a variable using a "while" loop. then i feed that variable to an input file. After that i run that file in parallal using mpi library.Script is as:>>>>

#!/bin/bash


STA=/home/sk/Desktop/exefile

sta=mpirun -np 6 $STA

# where -np is no. of processors,i took them=6

ES=30

while [ $ES -le 200 ]

do

echo "$ES" >> sourcefile
$sta< sourcefile > $ES.out
let ES=$ES+10

done

when i run the script .
i got the following error:
./variable.sh: line 5: -np: command not found.

how can i solve this problem?

with regards

skumar
 
Old 02-08-2010, 08:33 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
To assign the entire string mpirun -np 6 $STA to a variable you need to enclose it in quotes. In this case, where it includes a variable for which you want to substitute its value, you need double quotes
Code:
sta="mpirun -np 6 $STA"
The original command (statement) told bash to assign string mpirun to variable $sta and, with that assignment in effect, run command -np 6 $STA.
 
1 members found this post helpful.
Old 02-08-2010, 08:33 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
sta=mpirun -np 6 $STA
Here, to assign a string containing spaces to a variable, you have to use double quotes. Without them, the shell sets the variable sta to "mpirun" and tries to execute the rest as a command.

Edit: ...as catkin said just a bunch of seconds ago...
 
1 members found this post helpful.
Old 02-08-2010, 08:38 AM   #4
JimBrewster
Member
 
Registered: Feb 2010
Location: usa:/dev/random
Distribution: Slackware, Salix
Posts: 237

Rep: Reputation: 59
You need to quote the sta variable:
Code:
sta="mpirun -np 6 $STA"
 
1 members found this post helpful.
Old 02-08-2010, 08:57 AM   #5
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Original Poster
Rep: Reputation: 17
Thumbs up

Quote:
Originally Posted by colucix View Post
Code:
sta=mpirun -np 6 $STA
Here, to assign a string containing spaces to a variable, you have to use double quotes. Without them, the shell sets the variable sta to "mpirun" and tries to execute the rest as a command.

Edit: ...as catkin said just a bunch of seconds ago...
!! thank you very much to all of you !!

can you, please, tell/suggest me the materail/stuff required to write a script in linux. I have just started. i found many sites for shell scripting, but i don't understand them easily/completely.

With warm regards

skumar

Last edited by sonu kumar; 02-08-2010 at 09:06 AM.
 
Old 02-08-2010, 06:05 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
3 very good tutorials; read them and TRY stuff if you don't understand it. Nobody can magically make you a bash guru instantly.

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
Old 02-08-2010, 11:51 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by chrism01 View Post
Nobody can magically make you a bash guru instantly.
Truly. Like all languages (maybe more than some), fluency comes from practising, practising, practising ... and asking for advice when you get stuck
 
1 members found this post helpful.
Old 02-09-2010, 04:36 AM   #8
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Original Poster
Rep: Reputation: 17
Red face

Quote:
Originally Posted by catkin View Post
Truly. Like all languages (maybe more than some), fluency comes from practising, practising, practising ... and asking for advice when you get stuck
Thank you very much!!!

I find that in bash script, loops like while loop doesn't take fractional increment.
is there any remedy for it?

with Regards
skumar
 
Old 02-09-2010, 05:00 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by sonu kumar View Post
I find that in bash script, loops like while loop doesn't take fractional increment.
is there any remedy for it?
Can you post what you have tried? Bash only handles integers but there will be a way to do what you want to do.
 
Old 02-09-2010, 05:41 AM   #10
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Original Poster
Rep: Reputation: 17
Question

Quote:
Originally Posted by catkin View Post
Can you post what you have tried? Bash only handles integers but there will be a way to do what you want to do.
hi catkin and all linux users!!

i tried to increment a fractional variable by fractions.

Script is:

#!/bin/bash

echo "if u want 2 use fractional while loop, enter 8"
read what
if [ $what=8 ]; then
SN=0.10
# echo "1/5" | bc -l
while [ $SN -le 0.20 ]; do
echo "your no. is is $SN"
echo "Char is $SN" >> lab.f
let SN=$SN+0.1
done
else
echo "ok, right "
fi

error is:
if u want 2 use fractional while loop, enter 8
8
./sp.sh: line 8: [: 0.10: integer expression expected

if u want 2 use fractional while loop, enter 8
2
./sp.sh: line 8: [: 0.10: integer expression expected

Also it doesn't matter whether i enter 8 or any other number, i didn't get ok, right.

regards
skumar
 
Old 02-09-2010, 05:55 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Either use integers to control your loop and divide in "bc" to provide feedback to the user, or move the test inside bc.

bc does have comparison tests, print, read and while. The entire loop could be in bc but only because the loop doesn't really do anything.

You really need to avoid using floating point values to control loops. Even in languages that have floats, conversion errors between a floating point value and its internal representation can cause problems. E.G. don't compare two floating point values for equality. The result might be due to a conversion error.

Also for integer arithmetic, use [[ ... ]] in tests instead of [ ... ].

Last edited by jschiwal; 02-09-2010 at 05:56 AM.
 
1 members found this post helpful.
Old 02-09-2010, 06:37 AM   #12
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Original Poster
Rep: Reputation: 17
Red face

[QUOTE=jschiwal;3857658]Either use integers to control your loop and divide in "bc" to provide feedback to the user, or move the test inside bc.

hi dear all !

how can i use "bc" in this context?

also echo "if u want 2 use fractional while loop, enter 8"
read what
is not working properly.
skumar

Last edited by sonu kumar; 02-09-2010 at 06:39 AM. Reason: to add more
 
Old 02-09-2010, 06:54 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
[QUOTE=sonu kumar;3857674]
Quote:
Originally Posted by jread what[/COLOR
is not working properly.
Read what is probably working OK but testing it is not. Try
Code:
if [[ $what = 8 ]]
 
1 members found this post helpful.
Old 02-09-2010, 06:22 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
For numeric comparisons, that should be '-eq' not '=' http://www.tldp.org/LDP/abs/html/com...l#ICOMPARISON1.
I agree that [[ ]] is better than [ ] http://www.tldp.org/LDP/abs/html/tes...ml#DBLBRACKETS

Also, whilst assignments require no spaces around the operator ie

X=B

comparisons require a min of one space on each side of the operator (and the brackets) eg

if [[ $a -eq $b ]]
 
1 members found this post helpful.
Old 02-10-2010, 06:33 AM   #15
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Code:
jschiwal@qosmio:~/work> cat bcex                                  
#!/usr/bin/env bc                                                 
scale=2                                                           
sn=0.10                                                           
print "enter value: "                                             
value=read()                                                      
while ( value <= 0.2 ){
  print "Your no. is ", value, "\n"
  value+=0.1
}
print "ok,right\n"
quit

jschiwal@qosmio:~/work> ./bcex
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
enter value: 0.01
Your no. is .01
Your no. is .11
ok,right
jschiwal@qosmio:~/work>
Here is your example in bc instead of bash.
You could have lines like:
echo "scale=2; $value <= 10.5" | bc
and then test for a result of "0" or "1".

You could hide the use of bc in your bash program by using bc in functions.

Here is an example using sed in a function to keep just the integer part of a string. You can assign the result to an integer variable:
intpart(){ local value result; value="${1}"; result=$(echo "$value" | sed 's/[^[:digit:]].*//'); echo $result ;}
You could modify it to use bc to round of the value first.

Last edited by jschiwal; 02-10-2010 at 06:38 AM.
 
  


Reply


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
Problem in writing script capricorn80 Linux - Server 7 08-16-2007 07:30 AM
help writing script willinusf Programming 7 07-20-2004 12:37 PM
problem writing a script john8msu Linux - General 2 04-15-2004 04:59 PM
Help Writing a script. teeth44 Programming 2 10-14-2003 01:00 PM
Writing a Script Nyc0n Linux - General 2 05-27-2002 08:21 PM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration