LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2011, 08:30 AM   #1
snaveen58
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
what will be the echo output in this scenario


cmd ()
{
xyz=(abc def ghi jkl mno)
echo ${xyz[$2]}
}

I am sending some numbers from a function
cmd 12 59 30 25

I am bit confused what output did the echo would throw? I am new to shell scripting. I am sorry if this question is not valid.
 
Old 11-11-2011, 09:48 AM   #2
trappa01
LQ Newbie
 
Registered: Dec 2009
Posts: 20

Rep: Reputation: 8
The echo won't show anything!
xyz will be an array where xyz[0]='abc' , xyz[1]='def', xyz[2]='ghi' .....etc

by calling the function with arguements 12, 59, 30 and 25, you are saying that $1=12, $2=59, $3=30 and $4=25

You then try to echo the 60th entry in the xyz array ( or xyz[59] ) which it doesn't have so there is nothing to display.

if you called the cmd function with cmd 2 1 3 0 it would have shown xyz[1] or "def"

I hope this is clear.
 
Old 11-11-2011, 10:34 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,942

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by snaveen58 View Post
Code:
cmd ()
{
xyz=(abc def ghi jkl mno)
echo ${xyz[$2]}
}
I am sending some numbers from a function
cmd 12 59 30 25

I am bit confused what output did the echo would throw? I am new to shell scripting. I am sorry if this question is not valid.
Why don't you just run the script and see what it does?
 
Old 11-11-2011, 11:54 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Also you can put set -x at the top of your code to have the shell show you what it is doing.
 
  


Reply


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
[SOLVED] Echo or search through terminal output Shark82 Linux - Software 4 06-16-2011 09:41 AM
echo two command output in the same line chuikingman Linux - Software 8 03-31-2011 07:20 PM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 08:02 PM
Change echo output elainelaw Linux - Newbie 4 07-21-2010 08:14 AM
When I use xargs with echo, why does the output contain an extra space? web_janitor Linux - General 2 08-20-2009 05:18 PM


All times are GMT -5. The time now is 06:58 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration