Problems with variables syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with variables syntax
# 1  
Old 04-27-2012
Problems with variables syntax

Hi there

I am really struggling Smilie to place a value in a variable with the following loop, having run out of ideas please can someone point me in the right direction?

We first read two PIDs of a program (say calc) into an array, then we loop reading the details of those processes into a value which increments each time the loop runs.

I just can't get proc$val to equal the output from`ls -ld /proc/${array[$val]}`

One example of how I have tried (and failed is below)

Code:
val=0
for i in {1..${array[*]}}
do
 
[ "proc$val"="`ls -ld /proc/${array[$val]}`" ]
 
((val++))
done


Last edited by radoulov; 04-27-2012 at 06:55 AM.. Reason: Code tags!
# 2  
Old 04-27-2012
Code:
val=0
for i in ${array[*]}
do
    proc$val="$(ls -ld /proc/$i)" # Check if you really want this to be ls -ld.
    ((val++))
done

# 3  
Old 04-27-2012
use
Code:
$((val++))

instead of this
Code:
((val++))

# 4  
Old 04-27-2012
Hi thanks for the reply

just tried in ksh enviroment
Code:
$proc$val="$(ls -ld /proc/9467)"
Error
ksh: proc0=dr-xr-xr-x 8 nathan nathan 0 2012-04-27 08:09 /proc/9467: not found [ no such file or directory]

if I omit the proc$val ie
Code:
$proc0="$(ls -ld /proc/9467)"

it works!

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 04-27-2012 at 07:51 AM..
# 5  
Old 04-27-2012
Try creating an array instead of multiple variables like proc0,proc1..etc

Code:
 
val=0
for i in ${array[*]}
do
    proc_output[$val]="$(ls -ld /proc/$i)" 
    ((val++))
done

# 6  
Old 04-27-2012
Just to be clear, the reason that:

Code:
for i in {1..${array[*]}}

Doesn't work is that brace expansion is the first thing that the shell tries to process *before* it's converted the variable into an actual value. So in the shell's "mind" you're trying to say for everything between 1 and the word "${array...}" which obviously doesn't make any sense.
# 7  
Old 04-30-2012
Back to my original issue, just forget the loop for now

just can't get proc$val to equal the output from `ls -ld /proc/${array[$val]}`

I found the following did work

Code:
 eval 'proc'$val=`'ls -ld /proc/{$array[$val]}'`

however I just want the time stamp from this string

Code:
 eval 'proc'$val=`'ls -ld /proc/{$array[$val]} | awk ' {print $7}' '`

fails

Code:
 awk: line 2: missing } near end of file

is this even possible? any ideas would be gratefullly recieved Smilie

Last edited by Scott; 04-30-2012 at 11:29 AM.. Reason: Use code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with substitution between two variables

To all geeks, What I want to achieve: 1. Accept two filenames from user and store the filenames in two variables (FILE1 and FILE2) 2. Check if files exisits. If doesn't, then exit 3. If files exist, look for a particular string in both files 4. If the string exists, then change the... (8 Replies)
Discussion started by: Deepak Tulsani
8 Replies

2. Shell Programming and Scripting

EVAL syntax problems

Hi there As part of a larger script I need to put the output of an ls into a variable which has an incremental number. ie nathan@nathan-Vostro-1700:~$ eval 'proc'$val='`ls -ld /proc/9467`' nathan@nathan-Vostro-1700:~$ echo $proc0 dr-xr-xr-x 8 nathan nathan 0 2012-05-02 09:21... (3 Replies)
Discussion started by: nathan.harris
3 Replies

3. Shell Programming and Scripting

Syntax to see if two variables match

I have a simple shell script to read create a mysql database as a particular user. Right now, I have the password set as a variable to make sure someone does not mistype the password. I want to take the hard coded password out of the file and make the user input the password twice to make they... (2 Replies)
Discussion started by: bouncer
2 Replies

4. Shell Programming and Scripting

Problems with expect and set variables

I'm writing a script that'll send a time-stamp to my backup server. I create a file with the name of the current date, send it to my server with scp and rm the file from the local computer. Individually these commands work fine and with a set name the expect scripts also work fine. The problem... (0 Replies)
Discussion started by: Ktesh564
0 Replies

5. Shell Programming and Scripting

Shell variables problems

hi, i need some help, the situation is this 1-file of variable enviroments DIR1=/tmp DIR2=otherdir/mydir 2-file of list of files (all the names references whic variables of first point) ${DIR1}/${DIR2}/onefile Well now i create a shell script whic... (5 Replies)
Discussion started by: chipcmc
5 Replies

6. Shell Programming and Scripting

Problems with Syntax

I'm an experienced programmer that is new to shell scripting. I'm putting together a shell script that erases all files of a certain age. These files are in directories provided on lines of a file that is provided via command line argument and takes any errors(permissions, etc) and emails them to a... (3 Replies)
Discussion started by: drew2002
3 Replies

7. Shell Programming and Scripting

problems calling out variables in a loop

good afternoon forums. i have a problem that ive been trying to work out all morning and cant seem to get my head around it. what i have in my script is individual letters saved in different variables. so if the word unix was saved then 'u' would be stored in the variable 'wvar1' 'n' in 'wvar2'... (7 Replies)
Discussion started by: strasner
7 Replies

8. Shell Programming and Scripting

Problems with syntax in a loop (AWK)

Hi guys, I'm trying to loop through a number of files that is set by whatever is in a field. eg. The idea is to split FILELIST down into fields, it could contain 1 - 999 fields and it's bar delimited. I thought simple, count the number of fields in the field and then loop... (1 Reply)
Discussion started by: Peejay
1 Replies

9. Shell Programming and Scripting

syntax for variables in sed

I always kind of wondered this but I have a variable that I want to use in a search and replace. Basically I want to search a file for the string in my variable and replace it with something fixed but I'm unsure of the variable rule in sed. Here's generally what I have: sed 's/$name/newname/g'... (15 Replies)
Discussion started by: eltinator
15 Replies

10. UNIX for Dummies Questions & Answers

Problems with sed and flat file variables

Hello All, It has been a loooooooooooong time since I had last used sed but decided to use it for a simple task I have . My goal is to use sed to read variables from a flat file then use those same variables in order to make some subsitutions. However what I am finding is that when the... (1 Reply)
Discussion started by: icalderus
1 Replies
Login or Register to Ask a Question