Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-24-2005, 09:25 PM   #1
Registered: Oct 2005
Distribution: Slackware
Posts: 44

Rep: Reputation: 15
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.
Old 11-24-2005, 10:59 PM   #2
Registered: Jun 2005
Location: q3dm7
Distribution: Mandriva 2010.0 x86_64
Posts: 338

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

See 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).
Old 11-25-2005, 12:11 AM   #3
Registered: Oct 2005
Distribution: Slackware
Posts: 44

Original Poster
Rep: Reputation: 15
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.
Old 11-25-2005, 12:46 AM   #4
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Rep: Reputation: 35
  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-
              -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.


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
Bash scripting pete1234 Programming 1 09-27-2005 02:48 AM
bash scripting questions nix101 Linux - Newbie 2 09-04-2005 10:26 AM
Bash scripting questions irfanhab Programming 2 06-07-2004 01:13 PM
bash scripting -=MaGo=- Programming 16 08-30-2003 08:07 PM
Bash scripting NSKL Linux - General 2 06-08-2002 01:10 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration