Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    30

    Unhappy Unanswered: getting started:a simple ls program

    i'm just getting started with shell programming.

    to start with, i tried to write a script which shows the result of ls.

    Code:
    #! /usr/local/bin/bash
    
    echo "beginning of ls"
    
    `ls -l `
    
    echo "end of ls"
    
    exit 0
    It returns
    beginning of ls
    ./test.sh: total: command not found
    end of ls

    It is because "total" was outputed from "ls -l"

    Thus i changed my code to `ls -l` 2>/dev/null

    Although i get no errors, i get no result of "ls -l" too..
    just a simple of:
    beginning of ls
    ./test.sh: total: command not found
    end of ls

    How can i execute "ls -l", at the same time displaying the result on screen, without asking the shell to execute lines displayed on screen (for example : total)

    Thanks

  2. #2
    Join Date
    Jul 2004
    Posts
    30
    changed my code to the following

    Code:
    LIST=`ls -l`
    echo $LIST
    It works, but without newline.
    Any other ways to implement it?

    Thanks

  3. #3
    Join Date
    Jun 2004
    Posts
    29

    Why the backticks?

    backticks are used when you want to put the output of a command into a variable. If you just want the output on stdout (the screen) use
    Code:
    #! /usr/local/bin/bash
    
    echo "beginning of ls"
    
    ls -l 
    
    echo "end of ls"
    
    exit 0

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    ...and in your original 'echo $LIST' attempt, you could have quoted $LIST to retain the whitespace/newlines. e.g. echo "$LIST"

  5. #5
    Join Date
    Jul 2004
    Posts
    30
    Quote Originally Posted by Damian Ibbotson
    ...and in your original 'echo $LIST' attempt, you could have quoted $LIST to retain the whitespace/newlines. e.g. echo "$LIST"
    Thanks very much damian!!!
    I've grown up a bit from your guidance!

Posting Permissions

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