LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-27-2011, 12:06 PM   #1
Bouldernative
LQ Newbie
 
Registered: Feb 2011
Location: Boulder, CO
Distribution: Ubuntu
Posts: 5

Rep: Reputation: 0
Question Bash Shell Scripting Help Request


Greetings,

I am working on a simple script that should take two command line arguments, a [number] and a [name]. The first thing the script should do is check to make sure that no more and no less than two command line arguments have been entered when calling the script - an error message should be delivered if the condition is not true.

If two args have been entered, then the message 'processing "scriptname"' should appear, where scriptname is the name of the script being called. The script should then write to the screen "Hi [name]!", and should write this phrase [number] of times.

For example, the command $ myscript 2 joe would produce the output:
Hi Joe!
Hi Joe!

I have read the manual many times looking for examples, and I am very close by virtue of my own efforts. Further, I have searched these forums and others for good examples, which have also gotten me very close. Still my script is not completing the objective, and I am wondering if someone could point me in the right direction. Script:

scriptname="hw3-13"
echo "Please enter the number and then the name:"
read NUMBER NAME
if test $# -gt 2 -o $# -lt 2
then
echo "Please enter no more than or no less than two arguments!"
exit 2
else
echo "Processing $scriptname"
fi
for a in {1..$NUMBER}
do
echo "Hi $NAME\!"
done

Thanks in advance for any assistance.
 
Old 02-27-2011, 12:31 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
The first thing that I notice is that you say that the script should accept arguments provided when the script is called but you implement a read command inside the script. That doesn't make any sense.

All of your specifications can be met using built in functions like $0. Read the bash manual page. Search for the section named Special Parameters.
Code:
man bash
You can search for phrases in the manual page reader the same way that you search for phrases in vi/vim. Press the / character then type the phrase then press the Enter key.
 
Old 02-27-2011, 12:31 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,877

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
You could replace this
Quote:
if test $# -gt 2 -o $# -lt 2
with this
Quote:
if [ $# -ne 2 ]; then
and get the same result.

And this
Quote:
for a in {1..$NUMBER}
do
echo "Hi $NAME\!"
done
could be shortened to this
Quote:
echo Hi $NAME
echo Hi $NAME
 
Old 02-27-2011, 12:45 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,260

Rep: Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624
Welcome to LinuxQuestions.
Is this a homework assignment?

Per the LQ rules please do not ask us to do your homework for you. However, We can help you with specific questions.
 
Old 02-27-2011, 12:50 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You need to read the manual section about $ expansions. You're trying to re-invent the wheel.

Try this:
Code:
#!/bin/bash
if [[ $# != 2 ]]
then
  echo "Please enter exactly two arguments!"
  exit 2
else
  echo "Processing $(basename $0)"
fi
NUMBER=$1
NAME=$2
[[ $((${NUMBER} + 0)) -le 0 ]] && echo "The first argument must be a positive number. \""${NUMBER}"\" is not." && exit 1
for ((a=1;a<=${NUMBER};++a))
do
  echo "Hi ${NAME}!"
done
Here's how you make the script executable and run it. (I called it "bn," not your strange name.
Code:
$ chmod +x bn
$ ./bn 2 Joe
Processing bn
Hi Joe!
Hi Joe!
P.S. I hope this wasn't a homework problem. I posted a suggested solution because you showed some work, but LQ's general policy is that students learn better by doing their homework themselves.
 
Old 02-27-2011, 01:12 PM   #6
Bouldernative
LQ Newbie
 
Registered: Feb 2011
Location: Boulder, CO
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Well, this is a homework problem, but I purposefully did not ask for a solution to the problem. I did as much of the work as I could on my own, until my situation came to the point that I was going cross-eyed reading the book and I was essentially spinning my wheels. I asked for someone to point me in the right direction specifically so that I could find the solution for the problem myself. At no time did I ask for the solution in and of itself.

I understand and respect the spirit and intent of the no homework policy. I did everything I could to arrive at my own solution, but what am I supposed to do when I get to a point that I am stuck? At some point even a student has to be able to ask for some pointers.
 
Old 02-27-2011, 01:16 PM   #7
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
There is nothing wrong with asking for help with homework provided that you did attempt to do the work yourself. Evidently PTrenholme does not understand the LQ guideline in this matter.

Bouldernative you did exactly the right thing and you have every right to expect guidance from us.
 
Old 02-27-2011, 01:46 PM   #8
Bouldernative
LQ Newbie
 
Registered: Feb 2011
Location: Boulder, CO
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks.

StressJunkie, thank you for your patience and understanding. I noted that someone posted a solution to the problem and I am not even going to look at that as I do not want someone to do my work for me. I appreciate your direction, and I am going to jump back in to see what I can find.

I will post more detailed questions if I become stuck again.
 
Old 02-27-2011, 09:02 PM   #9
Bouldernative
LQ Newbie
 
Registered: Feb 2011
Location: Boulder, CO
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Along the same lines...another question about shell scripting in bash...

So, I have been working on the following shell script:

#!/bin/bash
# Make sure at least one file name is entered, if not throw error
if test $# -lt 1
then
cat <hw3-14_error
fi
# Define counter vars
regfile=0
scriptfile=0
# verify the \# of passed params
echo "There are $# files in your list!"
# Iterate through entered params to match file type keyword
# Increment appropriate file type
for ((a=1;a<=$#;++a))
do
if [[ file $a == executable ]]
then
scriptfile=++scriptfile
echo $scriptfile
elif [[ file $a == regular ]]
then
regfile=++regfile
echo $regfile
else
echo "File is not a recognized file type!"
fi
done
echo "There are:"
echo "regular files: ${regfile}"
echo "Script files: ${scriptfile}"

The object is to read file names from the command line, determine the file type and then increment a counter for one of two files types. My trouble comes in understanding the proper syntax for the 'file' command. I have read the man page for 'file' and feel that I am using the right command to accomplish the task. However, I am getting the following error message:

phil@ubuntu:$ ./hw3-14 hw3-13 hw3-12 hw3-11 hw3-10
There are 4 files in your list!
./hw3-14: line 15: conditional binary operator expected
./hw3-14: line 15: syntax error near `$a'
./hw3-14: line 15: `if [[ file $a == executable ]] '

What I am wondering is whether the 'file' command produces text-based output that I can compare either 'executable' or 'regular' to in order to classify the file type. It seems, based on the error message that I trying trying an improper comparison in the noted expression. A pointer in the right direction would be appreciated.
 
Old 02-27-2011, 11:22 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
All bash cmds are also runnable directly at the cmd line; saving them to a 'script' file is just a convenience.
The best thing to do with eg 'file' cmd is try it directly eg
Code:
file prtdiag_v.txt

prtdiag_v.txt:  ascii text
NB: it should be the same for you, but FYI, my example is actually ksh on Solaris; that's what's in front of me right now.
As you can see, it rtns both the filename AND the type, so you'll have to parse that before comparing.
Code:
# embed absolute string inside double quotes
if [[ $type == "executable" ]]
I recommend these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/


See section 8.2 http://tldp.org/LDP/abs/html/ops.html for increment operations

HTH
 
1 members found this post helpful.
Old 02-28-2011, 10:41 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Re your last question, the file command returns a whole string, not a single word, so a == test will almost always fail. Look at the ~= test to search for a "regular expression" match. (Note the option that must be set for ~= to work.)

Also, review the use of "back-quote" (`) and the $(command args) constructions for redirecting standard output to a variable that you can use in your code.
 
Old 03-01-2011, 02:49 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
From a quick look over these questions and answers, it seems no one has asked you to place your code in tags to make it easier to read and
maintain your formatting, so, please place code in [code][/code] tags to do the aforementioned task.

I have a few pointers for both your questions asked:

Based on post #1:

Have a look at the (()) construct when doing numerical comparisons

Based on post #9:

Look at same option as above when doing mathematical operations
As PTrenholme has pointed out $() along with =~ should get you on the road for your ifs.
 
Old 03-02-2011, 05:50 PM   #13
Bouldernative
LQ Newbie
 
Registered: Feb 2011
Location: Boulder, CO
Distribution: Ubuntu
Posts: 5

Original Poster
Rep: Reputation: 0
Thank to everyone for their help.

Thank you to everyone who responded. I sincerely appreciate the help in figuring this out. I am not completely done, but well on my way.

@grail, when you refer to code tags, are code tags simply to delineate between my text and the code? In any event, will do on future posts. Thanks again!

BN

QUOTE=grail;4274858]From a quick look over these questions and answers, it seems no one has asked you to place your code in tags to make it easier to read and
maintain your formatting, so, please place code in [code][/code] tags to do the aforementioned task.

I have a few pointers for both your questions asked:

Based on post #1:

Have a look at the (()) construct when doing numerical comparisons

Based on post #9:

Look at same option as above when doing mathematical operations
As PTrenholme has pointed out $() along with =~ should get you on the road for your ifs.[/QUOTE]
 
Old 03-03-2011, 02:24 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Quote:
@grail, when you refer to code tags, are code tags simply to delineate between my text and the code?
Correct

And if you are wondering why your quote did not work, you missed the first square bracket
 
  


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
Help with Bash shell scripting HLM01 Linux - Newbie 7 01-31-2008 05:23 PM
Bash shell scripting using if else nautz Linux - General 10 11-07-2006 10:47 AM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 12:58 PM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 06:24 PM
some bash shell scripting eltongeoff Linux - Newbie 2 10-22-2003 02:10 PM


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