Results 1 to 6 of 6

Thread: How to write ls -l command in shell script?

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default How to write ls -l command in shell script?

    hi...
    i have a question, can help me?
    please.

    Write a script that will do ls -l command on each of its arguments only if the argument is the name of regular file. This script will have a for loop, and inside the loop will an if test that uses one of the file test options.

    Script's name: listfiles.sh

    Presume you have files named xyz and abc, and a directory named mydir. If you runs the script:

    ./listfiles.sh xyz mydir abc nosuchfile

    You will get ouput like this, since xyz and abc are the only two regular files:

    -rw-r--r-- 1 LinuxBox users 1234 2010-09-11 12:34 xyz
    -rw-r-xrw- 1 LinuxBox users 2234 2010-09-09 12:35 abc
    Last edited by Reid; 15th September 2010 at 09:06 AM.

  2. #2
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    I solved the problem already

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Glad you solved it, could you share? Sounds like you're taking a course of some sort?

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    The code is very simple.

    Here is my solution:

    Code:
    #!/bin/bash
    
    for FILE in $*
    do
     if [ -f $FILE ]
      then 
        ls -l $FILE
     fi
    done
    Last edited by nixcraft; 16th September 2010 at 10:32 AM.

  5. #5
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,512
    Thanks
    17
    Thanked 799 Times in 511 Posts
    Rep Power
    10

    Default

    Same thingy using GNU find:
    Code:
    find . -type f -ls
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  6. #6
    Senior Member cfajohnson's Avatar
    Join Date
    May 2009
    Posts
    190
    Thanks
    0
    Thanked 44 Times in 36 Posts
    Rep Power
    10

    Default

    Quote Originally Posted by Reid View Post
    The code is very simple.

    Here is my solution:

    [code]#!/bin/bash

    for FILE in $*

    That will fail if any arguments contain whitespace. Use "$@":

    Code:
    for FILE in "$@"
    Code:
    do
     if [ -f $FILE ]
      then 
        ls -l $FILE
     fi
    done

    And quote the variables (for the same reason):

    Code:
    do
     if [ -f "$FILE" ]
      then 
        ls -l "$FILE"
     fi
    done

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. chsh command - change user shell from a script
    By gsb1bee in forum Shell scripting
    Replies: 1
    Last Post: 30th April 2009, 10:33 AM
  2. how to Write Terminal command data into a file
    By vazzdoin in forum Coding in General
    Replies: 1
    Last Post: 27th March 2009, 04:30 PM
  3. How do I Write a shell script
    By seaman77 in forum Shell scripting
    Replies: 1
    Last Post: 11th February 2009, 02:30 AM
  4. Execute UNIX Command / Shell Script Once In The Future
    By permalac in forum Shell scripting
    Replies: 2
    Last Post: 27th August 2008, 08:13 PM
  5. Run mv command from ftp shell script
    By subin in forum Shell scripting
    Replies: 6
    Last Post: 2nd May 2008, 09:44 AM

Tags for this Thread

Posting Permissions

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