LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-26-2012, 05:56 AM   #1
surferdude
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
for loop


Hi Guys and Girls

I'm trying to do a for loop....

OSNAME="Gentoo 2012 CentOS 5.4 Windows 7 Professional"

for OS in $OSNAME
do
echo "hey" "$OS"
done

The output looks like...

hey Gentoo
hey 2012
hey CentOS
hey 5.4
hey Windows
hey 7
hey Professional

What I want it to look like is...

Hey Gentoo 2012
Hey CentOS 5.4
Hey Windows 7 Professional

How can I do this?

I can put something at the end of each OS name like Gentoo 2012bb CentOS 5.4bb. Make sense?

surferdude
 
Old 07-26-2012, 06:12 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Code:
OSNAME=("Gentoo 2012", "CentOS 5.4", "Windows 7 Professional")
for OS in "${OSNAME[@]}"; do echo "hey" "$OS"; done
 
Old 07-26-2012, 06:49 AM   #3
_val_
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
Another aproach

Code:
#!/bin/bash

oses=( Fedora, "Red Hat", "Debian", "Arch Linux", Windows )
echo -ne "There are ${#oses[@]} Operating Systems Availiable"
echo
for os in "${!oses[@]}"; do printf '%s\n' " MyOS ${oses[os]}"; done
 
Old 07-27-2012, 06:54 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Don’t use commas, they will be part of the string. Each word is just assigned to one field in the array.
 
Old 07-27-2012, 07:20 AM   #5
_val_
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by Reuti View Post
Don’t use commas, they will be part of the string. Each word is just assigned to one field in the array.
Totally agree with you. Comma's are not needed. Probably a bad habbit from other prog/script languages.
 
Old 07-27-2012, 12:27 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.


It's vital in scripting to understand how the shell handles arguments and whitespace:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


A for loop processes one "word" at a time. When you expand the variable, it gets broken into words based on whitespace. Unless you quote it, in which case the whole string gets treated as a single word.

http://wiki.bash-hackers.org/syntax/ccmd/classic_for
Don't Read Lines With For


This is why you should never store lists of things in a single variable. That's what arrays are for.

The above posts show generally how to do it, but see here for more details:

http://mywiki.wooledge.org/BashFAQ/005


Finally though, for the simple requirements listed above, we don't even need a loop, just printf, which has implicit looping built in.

Code:
oses=( "Gentoo 2012" "CentOS 5.4" "Windows 7 Professional" )

printf 'Hey %s\n' "${oses[@]}"

Last edited by David the H.; 07-27-2012 at 12:32 PM.
 
1 members found this post helpful.
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration