LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Shell Scripting Help Request (https://www.linuxquestions.org/questions/linux-newbie-8/bash-shell-scripting-help-request-865320/)

Bouldernative 02-27-2011 11:06 AM

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.

stress_junkie 02-27-2011 11:31 AM

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.

bigrigdriver 02-27-2011 11:31 AM

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

michaelk 02-27-2011 11:45 AM

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.

PTrenholme 02-27-2011 11:50 AM

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.

Bouldernative 02-27-2011 12:12 PM

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.

stress_junkie 02-27-2011 12:16 PM

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.

Bouldernative 02-27-2011 12:46 PM

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.

Bouldernative 02-27-2011 08:02 PM

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.

chrism01 02-27-2011 10:22 PM

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

PTrenholme 02-28-2011 09:41 PM

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.

grail 03-01-2011 01:49 AM

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.

Bouldernative 03-02-2011 04:50 PM

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]

grail 03-03-2011 01:24 AM

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 ;)


All times are GMT -5. The time now is 07:39 PM.