Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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...
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 08:06 AM.
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.
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 [ ... ].
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.