variable issues


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable issues
# 8  
Old 02-04-2009
Assuming your shell splits words on white space characters:

Code:
$ v="1 2 3 4" t="a b c d"; set -- $t
$ for v in $v;do printf "%s/%s\n" "$v" "$1";shift;done
1/a
2/b
3/c
4/d

# 9  
Old 02-04-2009
Quote:
Originally Posted by kkkk
I wanted to try to use loop to get this done.

Code:
v="1 2 3 4"
t="a b c d"

while [ -n "$v$t" ]
do
  rv=${v##?}
  cv=${v%"$rv"}

  rt=${t##?}
  ct=${t%"$rt"}

  printf "%s/%s\n" "$cv" "$ct"

  v=${rv#?}
  t=${rt#?}
done

# 10  
Old 02-05-2009
You could use arrays to...

t=(`echo "1 2 3 4"`)
v=(`echo "a b c d"`)

for index in "${t[@]}"; do
echo $index/${v[$index-1]}
done
# 11  
Old 02-05-2009
Quote:
Originally Posted by drewnix
You could use arrays to...

Note that arrays are not available in all POSIX shells.
Quote:
t=(`echo "1 2 3 4"`)
v=(`echo "a b c d"`)

Why are you using echo and command substitution?

What's wrong with:

Code:
t=( 1 2 3 4 )
v=( a b c d )

Quote:

for index in "${t[@]}"; do
echo $index/${v[$index-1]}
done
# 12  
Old 02-05-2009
Quote:
Why are you using echo and command substitution?
That was part of the original posters requirements.
# 13  
Old 02-05-2009
Quote:
Originally Posted by drewnix
That was part of the original posters requirements.

Where do you see that? There's no mention of either in the original post.
# 14  
Old 02-05-2009
From post #3...

Quote:
Originally Posted by kkkk
Thanks cfajohnson. Your suggestions work fine but I am still having issue:

basically where is what I have

v=$(echo "1 2 3 4")
t=$(echo "a b c d")
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

Awk Issues - Not printing the 10th Variable.

All, I am attempting to print the tenth ($COPY2) varaibales into one file. But i am finding that all variables are being outputted except for $10. Can someone help!!!! Code Below ---------- echo $SERVER $IMAGE $IMAGEDAY $IMAGEMONTH $IMAGEYEAR $COPY1 $EXPIREDAY $EXPIREMONTH... (1 Reply)
Discussion started by: Junes
1 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Solaris

Scripiting quote and variable issues

Hello all I have an interesting problem here that I can't seem to figure out. Note this exists inside a script with the following header: m#!/usr/bin/sh What I am trying to do is build an automounting script for USB hdd's, the idea is that the user plugs their drive in then runs the script. The... (5 Replies)
Discussion started by: striker0010
5 Replies

6. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

if [ -f "$variable" ]; then issues, help!

Im trying to write a /bin/bash script that refreshes VMware Fusion using the built in snapshot capabilities. I am having an issue getting a variable to pass into the find argument of an "if-then" statement. Im thinking the problem might have something to do with the working directory of the... (8 Replies)
Discussion started by: afriend
8 Replies

10. Shell Programming and Scripting

Variable issues

Hey guys, I have just started getting into shell scripting, ive been self educating myself with it and have run into a snag. I am trying to make a very simple addition script. The script would be passed a number of parameters (numbers) and it would add them all together. I can do this fine... (2 Replies)
Discussion started by: bert682
2 Replies
Login or Register to Ask a Question