+ Reply to Thread
Results 1 to 3 of 3

Thread: I/O in shell scripts

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    I/O in shell scripts

    This is my fourth and final tutorial on bash shell scripting. This tutorial will go over the I/O facilities of shell, namely echo, printf, read, and I/O redirection.


    echo is used to direct a variable or text to the standard output of a shell script. The echo command has the following options:

    -e interpret backslashed characters
    -E don't interpret backslashed characters
    -n omit the final newline


    The read command is used to read user input into a shell script. It has the syntax:

    read inputvariable;

    The input the user enters is stored in the variable inputvariable.

    Example:

    Code:
    echo "Enter a filename.";
    read filename;
    if [ ! -e $filename ]
    then
    	echo "$filename does not exist."
    fi

    printf is like echo, except that it allows for formatted text. It also allows for several escape sequences (not all of which work with echo, I've found).

    The syntax for printf is:

    printf "text %format-specifier text" formatted;

    Some of the format specifiers used in printf are:

    %d - Decimal integer
    %.preE - Exponential (scientific) notation with pre precision.
    %.pref - Floating point number to pre decimal places
    %o - Octal value
    %u - Unsigned decimal value
    %x - Lowercase hexidecimal value
    %X - uppercase hexidecimal value

    Escape sequences:

    \a Alert sound
    \b Backspace
    \c Omit final newline
    \f Form feed
    \n Newline
    \t Tab character
    \v Vertical tab
    \0nnn 7-bit ASCII character for octal value nnn
    \xHH 7-bit ASCII character for hexidecimal value HH


    Example:

    Code:
    #!/bin/bash
    printf "%.3E\n" 1500000;
    printf "%.5f\n" 4.95;
    printf "%x\n" 143;
    printf "%u\n" -1;
    printf "\x7e\n";
    Outputs:

    Code:
    1.500E+06
    4.95000
    8f
    18446744073709551615
    ~

    I/O redirection:

    There are six kinds of I/O redirection in Unix. Four of them are familiar to most Unix users: the >, <, >>, and | redirectors. Here is a rundown:

    > Send the standard output of the preceding command to the following file, overwriting its original contents
    < Get standard input for the preceding program from the following file
    >> Append the standard output of the preceding command to the following file
    | send the standard output of the preceding command to the following program, known as piping

    There are two other types of I/O redirectors: here-documents and file descriptors.


    Here-documents:

    A here-document is when you write a macro script inside a shell script to control an interactive program, such as ed or ftp. The syntax is as follows:

    Code:
    program $input << EndOfScript;
    program commands
    EndOfScript;
    Here, EndOfScript is the marker used to terminate the macro script.

    As an example, here is a here-document script that uses ed to append text to a file:

    Code:
    #!/bin/bash
    ed $1 << EndOfScript;
    a
    $2
    .
    w
    q
    EndOfScript

    File descriptors:

    A file descriptor is something that is generally only of concern to systems programmers as it controls the low level functioning of programs. Every time a Unix program is run, there are three file descriptors involved: standard output, standard input, and standard error. The first is called stdin and is given the number 0. The second is called stdout and is given the number 1. The third is called stderr and is given the number 2. The following script sends the standard output of cd / to the file file1.txt:

    Code:
    #!/bin/bash
    echo "Text to add to file1.txt.";
    cd / > ~/Desktop/file1.txt 1>&0;
    echo $0;
    That concludes my tutorial.
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: I/O in shell scripts

    Good Tutorial +rep

  4. #3
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: I/O in shell scripts

    Good work, +rep!
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. need help with scripts
    By Artulo in forum PHP Development
    Replies: 5
    Last Post: 06-08-2011, 08:00 AM
  2. Talking to a running MC server using shell scripts ??
    By bbqroast in forum Linux Applications
    Replies: 6
    Last Post: 05-12-2011, 02:26 PM
  3. Flow control in shell scripts
    By DarkLordoftheMonkeys in forum Linux Tutorials, Guides and Tips
    Replies: 1
    Last Post: 11-14-2009, 02:17 PM
  4. PHP Scripts
    By BlaineSch in forum PHP Development
    Replies: 13
    Last Post: 06-09-2009, 06:41 PM
  5. Scripts, scripts and more scripts
    By TheCEOandPresident in forum General Programming
    Replies: 4
    Last Post: 05-25-2009, 05:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts