LinuxQuestions.org
Register a domain and help support LQ
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 03-23-2015, 03:01 AM   #1
harrynelson
LQ Newbie
 
Registered: Mar 2015
Posts: 1

Rep: Reputation: Disabled
help with applying passing parameters


i need to complete this exercise but my code has some issues
HERE is the PRoblem:
Create a script that can accept ANY amount of numbers from the command line. Process the numbers one at a time, where numbers greater than 10 print “large”, numbers less than or equal to 10 print “small”
E.g. process 5 10 15 would print
small
small
large

and here is my code so far
if [ $@ -le "10" ]
then
echo "smaller"
else
echo "bigger"
shift
fi
if [ $@ -le "10" ]
then
echo "smaller"
else
echo "bigger"
shift
fi
if [ $@ -le "10" ]
then
echo "smaller"
else
echo "bigger"
shift
fi
if [ $@ -le "10" ]
then
echo "smaller"
else
echo "bigger"
shift
fi

any help would be greatly appreciated
 
Old 03-23-2015, 05:01 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
As you do not know the number I would suggest a for loop with a simple if inside.

Also, please use [code][/code] tags around code and data
 
Old 03-23-2015, 05:25 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Additionally you need to read the man page of bash to check the meaning of $@. I think you ought to use $1 instead...
Also the problem is not the assignment itself, but probably your solution is not yet completed.
http://www.cyberciti.biz/faq/bash-for-loop/
 
Old 03-23-2015, 09:16 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Google or Yahoo for BASH examples emplying getopts.

Other added suggestions if you don't choose to use getopts, or just in general:
  1. See links at the bottom of my signature for BASH guides as well as a blog on the subject
  2. Use "set -xv" to enable debug within your script
  3. Use #? to determine the number of arguments passed to your script
  4. Consider using a function to parse your argument list first prior to running the bulk of your script
 
Old 03-23-2015, 09:54 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,931

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
Welcome to LinuxQuestions.

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

By using a for loop (have you learned about loops yet) as suggested you can iterate through the list using a single if then else statement. well there are several ways to write your assignment. getopts is overkill. Don't want to confuse the OP...

I will also add that the shift command as posted will move the i.e shift the arguments to the left by 1. So $2 will be $1. You need to check $1 vs $@. You can use $@ in the loop.

Last edited by michaelk; 03-23-2015 at 10:13 AM.
 
1 members found this post helpful.
  


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
passing parameters to linphone linuxonbute Linux - Software 0 01-19-2015 06:44 PM
[SOLVED] passing parameters to trie atlantis43 Programming 12 02-14-2014 04:12 PM
Passing kernel parameters magicalshashank31 Linux - Newbie 3 08-17-2011 11:58 AM
Passing parameters to a makefile. kerenLinuxForum Linux - Software 3 09-01-2008 09:24 AM
need help passing parameters in C++ hedpe Programming 1 10-21-2007 11:58 AM


All times are GMT -5. The time now is 04:32 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