The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > UNIX Desktop for Dummies Questions & Answers
Google UNIX.COM


UNIX Desktop for Dummies Questions & Answers Questions regarding GNOME, KDE, CDE, Open Office, etc go here. All UNIX and Linux Newbies Welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inserting variables from 2 files to create 1 file rochitsharma UNIX for Advanced & Expert Users 1 03-17-2008 06:33 AM
script to create multiple instances of a user account across LPAR's kcampbell Filesystems, Disks and Memory 1 10-19-2006 07:42 PM
Pass multiple variables to SQL script jagannatha Shell Programming and Scripting 1 10-25-2002 06:45 AM
multiple variables micromicrin Shell Programming and Scripting 1 03-19-2002 06:47 PM
multiple DISPLAY variables QUartz Ite UNIX for Advanced & Expert Users 3 10-08-2001 06:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Mar 2008
Location: EHT, NJ
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
trying to create a script with multiple variables...

I have created a script that prompts the user to enter three variables that are seperated by a space as the delimiter.

It then performs a command 3 seperate times for each variable entered.

I want the script to llow the user to enter as many variables as they may like and the script to perform the commandwith each variable entered, each time.

the scripts promt starts as
example of script:

read AP1 AP2 AP3

{command + $AP1}
{Command + $AP2}
{Command + $AP3}



they are later used as $AP1 $AP2 $AP3

How can I change this with one read command that will use a loop to repeat the command to each variable entered?

Thank you for you help in advance.

italy87
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-20-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,279
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
use an array - note the "command" line will not work, it's an example:
Code:
#!/bin/ksh
echo "enter stuff \c"
read dummy
set -A array $dummy
let i=0

while [[ $i -lt ${#array[*]} ]]
do
     <command> ${arr[i]}
     let i=$i+1
done
Reply With Quote
  #3 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Oct 2007
Posts: 75
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hello, I couldn't help wonder how I would do it in bash. This one lets the user enter all arguments first, one line at a time, then operates on the arguments.

It is more verbose than it should be, only to show usage of arrays... replace "echo "executing command"..." etc with Your actual command.

Code:
#!/bin/bash
echo Enter arguments, just press enter when finished.
cnt=0
while [ true ]; do 
	echo -n "Enter argument $(($cnt+1)): "
	read x[$cnt]
	[ "${x[$cnt]}" == "" ] && break
	cnt=$(($cnt+1))
done

echo Processing $cnt arguments

cnt=0
while (($cnt < ${#x[@]}-1 ))
 do
     echo "executing command on argument $(($cnt+1)) which is: ${x[cnt++]}"
 done

#echo ${x[@]}
/Lakris
Reply With Quote
  #4 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Location: EHT, NJ
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Talking

OK, I should provide more info. first I am running in ksh.

I have a file with one line of a series of words that are three letters long, using s space as the delimiter.

I want to run a script which will retrieve those words and assign them to a variable, which i can run with my command in a loop until each word has been used, individually each time in the command until each three letter word has been used.

the file looks like this:

AIN OTB ALK SJA ALS HAH INA (etc..)

I want to run the command with all three letter word individually.

The commands i want to perform is already designed. I just need to insert the words in the command in a loop to do it each tmie. note the file may change with the count of words but it will always be three letter words. therefore it has to count the words each time it is run. i was thinking I would cat the file, pipe it to a wc -w and use that as the count for how many time to perform the loop. (I.e. wordct="cat <filename> | wc -w") with this variable I have a defined count of how many words each time.

I hope this helps.
Reply With Quote
  #5 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Aug 2007
Posts: 40
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
reading them as one variable and the segregating will help

Last edited by raman1605; 03-21-2008 at 10:02 AM. Reason: Took wrong meaning of the query
Reply With Quote
  #6 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Location: EHT, NJ
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
any suggestions on how to do that?
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101