Variable setting help please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable setting help please
# 8  
Old 06-28-2005
This still produces 0
# 9  
Old 06-28-2005
Post the shell and os you are using.

And also the exact script you are running.

Just to alert you

the quotes in

L=`expr $L + 1`

are backticks. The one found above the TAB key on your keyboard.

Vino
# 10  
Old 06-28-2005
@pixelbeat: correct! There is a difference between "$l" (lowercase L) and "$L" (uppercase L) the OP seems not to be aware of.

Another error is the line:

Code:
 L='expr $1 + 1'

Apart from the fact that "expr" shouldn't be used any more and that it should be replaced by "(( .... ))" "L" is set to some value given at the commandline (the "$1") increased by 1. Since the value of the commandline doesn't change it gets set to the same value over and over again

bakunin
# 11  
Old 06-28-2005
#!/bin/sh

L=0

while read line
do

L=`expr $L + 1`
echo $L

done < dinseh.sql

echo $L
# 12  
Old 06-28-2005
I think pixelbeat is right.
In this case, most of shell folks a subshell to run the loop block.
try ksh.
# 13  
Old 06-28-2005
Quote:
Originally Posted by penfold
#!/bin/sh

L=0

while read line
do

L=`expr $L + 1`
echo $L

done < dinseh.sql

echo $L
Another distant possibility
Code:
--dinseh.sql is empty--

Smilie

What does

Code:
wc -l <dinseh.sql

give ?

Vino

Last edited by vino; 06-28-2005 at 06:56 AM..
# 14  
Old 06-28-2005
At least you last version should be working.

Still there are some things to say about:

/switches ranting, style-aficionado mode on

Don't use the backticks any more, this is outdated style and not necessary in the Korn shell. If you want to execute a command, then put the output of that command into a variable instead of

Code:
 x=`command`

use

Code:
 x="$(command)"


Second, instead of "expr" or "let" you should use the "(( .. ))" construct, which is easier to read and encouraged by David Korn. Instead of

Code:
 x=$(expr $x + 1)
and
let x=1

better write:

Code:
 (( x = x + 1)) or (( x+= 1 ))
and
(( x = 1  ))

Yes, I already know i can be a PITA, no neeed to point that out. ;-))

/switches ranting mode off, reverting back to a humble person

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Setting a variable in unix

Hi , Whenever i want to start tomcat server I need to go to <tomcatdir>\bin and execute startup.sh file. How would i make this happen whenever i start my machine? Also want to know How would i set the variable at startup. for example I want to set JAVA_HOME and JRE_HOME whenever the... (3 Replies)
Discussion started by: pinga123
3 Replies

2. Shell Programming and Scripting

Setting a variable within if block

Hi, i have a variable which i would like to set inside an if block for example IS_VAR=0 if then IS_VAR=1 fi echo IS_VAR the last echo statement gives 0.So setting variables in the if block doesnt have effect outside the block?Is there any workaround for this? Thanks , Padmini (11 Replies)
Discussion started by: padmisri
11 Replies

3. Programming

Setting Environment variable..!

Hi, I already have one CPP program which invokes the C program.And the C program contains whole function definitions..!This is a working program..I have to enable the logs in both CPP as well as in the C program ..!So I am reading the enviornmental variable log path from the CPP and doing the... (2 Replies)
Discussion started by: Kattoor
2 Replies

4. Shell Programming and Scripting

Help with setting a variable!

I am working within a while loop and i am trying to set a variable that will read out each count of the files. the problem is the count variable i have set up gives me a total and not the individual count of each file. in the data area there is 4 abc.dat and 1 def.dat. how can i do this??? ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

5. UNIX for Dummies Questions & Answers

setting a variable

In my script, I have the following command.... du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}' it returns the value 383283 I want to modify my script to capture that value into a variable. So, I try doing the following... var1=`du -sk `ls -ltd sales12|awk '{print... (5 Replies)
Discussion started by: tumblez
5 Replies

6. Shell Programming and Scripting

Setting variable

How do you set a varible with information that contains a string and also another variable? For example: subject="Attention: $name / This $type needs your attention" The $xxxx are of course other variables that I instantiated earlier. Is it like Java where you have to use double quotes and... (1 Reply)
Discussion started by: briskbaby
1 Replies

7. UNIX for Dummies Questions & Answers

Setting a variable (need syntax help)

I need some syntax help (working in a bash shell) I have a variable which is a filename with an extension, and I need to create another variable with the same name but a different extension To explain, the input file should be called something like "filename.L1" and the output file should be... (1 Reply)
Discussion started by: Slanter
1 Replies

8. UNIX for Dummies Questions & Answers

Setting a variable

I want to set a variable to be any number of dashes. Rather than doing the following: MYVAR="------------------" I'd like to be able to set the variable to, say, 80 dashes but don't want to have to count 80 dashes. Is there a way to do this? (2 Replies)
Discussion started by: photh
2 Replies

9. Programming

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies

10. UNIX for Dummies Questions & Answers

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies
Login or Register to Ask a Question