LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting questions (https://www.linuxquestions.org/questions/programming-9/bash-scripting-questions-386270/)

stryka 11-24-2005 08:25 PM

Bash scripting questions
 
I'm starting my adventure in bash scripting, teaching myself. Now i've got alot of basics down, but there are a few things that are iffy, and I haven't came across each function in my tutorial. So here are my questions:

When echo -n "blah" is used, what purpose does -n server?

Using if and elif, what purpose do -z, -x, and ! = or if ! [] serve?

If you can toss any more - functions out there that will assist me that would be great.

Also, for my first simple program, I am coding an iptables script. I am wanting to have the user input several ports seperated by a space. i.e: 21 22 23 113
All in one line. How would I do this? $1 $2 $3 $4 and so on, or am I way off. I just started yesterday, and I am trying to catch on quickly. Thanks for the help guys.

Snowbat 11-24-2005 09:59 PM

man echo: -n do not output the trailing newline
Use -n when you want following output to appear on the same line.

See http://www.gnu.org/software/bash/man...ref.html#SEC77 for details of -z, -x, and !=
[...] will match any of the enclosed characters so I'd guess ![...] would match anything except the enclosed characters.

For your iptables script, do you want to prompt the user for input after the program starts or do you want the user to start the program with the port data as arguments? Either are possible (or both - you could check if command line arguments were provided and if none are given, prompt the user for input).

stryka 11-24-2005 11:11 PM

Alright, thank you. I hadn't came across that reference yet, and it has what I was looking for right in it.

As for the iptables script, I would like to get user input after the program has started. That's my goal for this script.

dr_zayus69 11-24-2005 11:46 PM

Code:

  read  [-ers]  [-u  fd] [-t timeout] [-a aname] [-p prompt] [-n nchars]
      [-d delim] [name ...]
              One  line  is  read  from  the standard input, or from the file
              descriptor fd supplied as an argument to the -u option, and the
              first  word  is  assigned to the first name, the second word to
              the second name, and so  on,  with  leftover  words  and  their
              intervening separators assigned to the last name.  If there are
              fewer words read from the input stream than names, the  remain-
              ing names are assigned empty values.  The characters in IFS are
              used to split the line into words.  The backslash character (\)
              may  be used to remove any special meaning for the next charac-
              ter read and for line continuation.  Options, if supplied, have
              the following meanings:
              -a aname
                    The  words  are  assigned  to  sequential indices of the
                    array variable aname, starting at  0.  aname  is  unset
                    before  any  new  values are assigned.  Other name argu-
                    ments are ignored.
              -d delim
                    The first character of delim is used  to  terminate  the
                    input line, rather than newline.
              -e    If  the  standard input is coming from a terminal, read-
                    line (see READLINE above) is used to obtain the line.
              -n nchars
                    read returns after reading nchars characters rather than
                    waiting for a complete line of input.
              -p prompt
                    Display  prompt  on  standard  error, without a trailing
                    newline, before  attempting  to  read  any  input.  The
                    prompt  is displayed only if input is coming from a ter-
                    minal.
              -r    Backslash does not act  as  an  escape  character.  The
                    backslash is considered to be part of the line.  In par-
                    ticular, a backslash-newline pair may not be used  as  a
                    line continuation.
              -s    Silent  mode.  If input is coming from a terminal, char-
                    acters are not echoed.
              -t timeout
                    Cause read to time out and return failure if a  complete
                    line  of input is not read within timeout seconds.  This
                    option has no effect if read is not reading  input  from
                    the terminal or a pipe.
              -u fd  Read input from file descriptor fd.

              If  no  names  are  supplied,  the line read is assigned to the
              variable REPLY.  The return code is zero, unless end-of-file is
              encountered,  read  times out, or an invalid file descriptor is
              supplied as the argument to -u.

that is from man read (looks like it brings up a general bash programming man) Hope that helps.


All times are GMT -5. The time now is 08:03 AM.