LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-29-2012, 01:20 PM   #1
eamesj
Member
 
Registered: May 2006
Posts: 51

Rep: Reputation: 1
Defining variables/arrays in csh from stdn/user input


hi all,

I'm wanting to use csh (yes, csh) to build an array of variables similar to the argv command below:

#! /bin/csh -f
foreach i ($argv[-1])
@ argnum ++
echo '$argv['$argnum'], argument:' $i
end

which gives me variables for $argv[1], $argv[2], etc

But i want these to come from user input so that it does:

Enter list:
texta
textb
textc

or
Enter list:
texta textb textc

gives a set of variables for later use
${variable1}=texta
${variable2}=textb
${variable3}=textc

How can i replace argv with user input?
Thanks,

Last edited by eamesj; 12-29-2012 at 02:44 PM. Reason: clarification
 
Old 12-30-2012, 11:56 AM   #2
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
Post user input loop with bash

OK, looks like csh is a non-starter - Trying to do the same thing but with bash but as im unfamiliar with bash i need a little help.

I need to ask user for a list, take the replies and perform an action on each of them.

Enter list:
texta
textb
textc

or
Enter list:
texta textb textc


foreach $input
anotherscript $text[a-c]

can anyone help?
Thanks,
 
Old 12-30-2012, 09:57 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
First of all, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


I don't know much about csh and its variants, but I do know that it's not a good idea to use it for scripting, if at all. It just has too many parsing problems.

Top Ten Reasons not to use the C shell

(The site also has a more detailed primer on using the csh shell. Just go up a level.)


As for bash scripting, a much better choice , I suggest starting with the Bash Guide here:

http://mywiki.wooledge.org/BashGuide

Note that the bourne shell syntax and built-in features are quite different in many ways. There's no foreach, for example, just for.


In any case, loading an array from user input is quite easy:

Code:
echo 'Enter your array elements one at a time, followed by newlines.'
echo 'Type "exit" to finish.'

while read -r input ; do

	[[ $input == exit ]] && break
	array+=( "$input" )

done

echo 'You entered the following array elements:'

printf '[%s]\n' "${array[@]}"
You just need to specify some condition for exiting the loop.

Edit: Come to think of it, it can be even easier if the entries are all single words, and you want to enter them on one line. You only need a single read command with the -a option.

Code:
echo 'Enter your array elements, separated by spaces:'
read -r -a array
You can even set it up to use a different separator, if you don't want to worry about whitespace:

Code:
echo 'Enter your array elements, colon-separated:'
IFS=: read -r -a array

Last edited by David the H.; 12-30-2012 at 10:16 PM. Reason: as stated
 
Old 12-30-2012, 10:12 PM   #4
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
solved :)

Fantastic David the H(ero)

Works a treat, clearly bash is the way to go. Thankyou
 
Old 12-30-2012, 10:32 PM   #5
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
Hi david
ah, and to use these elements individually in a following script? how can i split the array ?

while read -r input ; do

[[ $input == exit ]] && break
array+=( "$input" )

done

script ${array[@]} will attempt to run the script on texta textb textc

do some kind of for statement?
 
Old 12-30-2012, 11:20 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Read all about arrays here:

How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005

In general, a simple for loop will do the job nicely.


We'll be here if there's anything you can't figure out for yourself.


(PS: Don't forget the code tags!)
 
  


Reply

Tags
array, csh, stdin


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting preset variables by user input in a bash script Lateralus138 Programming 12 09-27-2012 02:33 AM
using read and setting variables from user input string tekgek Programming 1 11-27-2008 01:10 AM
defining environmental variables? sarayazdani LinuxQuestions.org Member Intro 1 08-05-2008 04:08 AM
changing variables using user-input Kai' Linux - General 3 04-29-2008 06:04 PM
Arrays - User input - newbie = [ ohfaney Programming 6 05-01-2006 10:14 PM


All times are GMT -5. The time now is 03:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration