Results 1 to 7 of 7

Thread: ls -l work differently when used in bash script

  1. #1
    Join Date
    Sep 2007
    Location
    UK
    Beans
    371
    Distro
    Ubuntu 11.04 Natty Narwhal

    ls -l work differently when used in bash script

    Hi guys

    I have a script I'm writing. The part I'm having trouble with takes a folder as an input from the user and lists the contents. When I use the the ls -l command over SSH in a command prompt it lists the destination contents of folders and links.

    When I use the same command in a bash script it does not list the links.

    Any idea how i can solve this

    Code:
    read -e -p "Destination > " Destination
      echo "$Destination"
        mDIRS=`ls -l $Destination | egrep '^d' | awk '{print $9}'`
        echo
        echo_f "## Previewing ##"
        #loop through the directories
        for mDIR in $mDIRS
        do
        echo "$mDIR"
        done
    Always do things the hard way. After a while you learn how to make the hard way the easy way.

    My blog: http://foreverythingit.co.uk

  2. #2
    Join Date
    Sep 2007
    Location
    UK
    Beans
    371
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: ls -l work differently when used in bash script

    I've realised the egrep '^d' limits it to only showing directories. I'm googling but cant find the correct syntax for directories and links '^l' any ideas
    Always do things the hard way. After a while you learn how to make the hard way the easy way.

    My blog: http://foreverythingit.co.uk

  3. #3
    Join Date
    Sep 2007
    Location
    UK
    Beans
    371
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: ls -l work differently when used in bash script

    ok never mind at a suggestion of the missus who is looking over my shoulder, and has never even used linux, I have deleted the egrep command completely and its working as intended
    Always do things the hard way. After a while you learn how to make the hard way the easy way.

    My blog: http://foreverythingit.co.uk

  4. #4
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: ls -l work differently when used in bash script

    Using ls in a script is a very bad idea and can lead to all sorts of unexpected behaviour.

    What is it you are trying to achieve? I'm sure we can come up with a better solution.

    PS - If you are new to bash scripting I've recently discovered this great site. Just paste your script and it will let you know of any syntax errors and bad practices it sees.
    Last edited by Cheesemill; June 30th, 2013 at 10:36 PM.
    Cheesemill

  5. #5
    Join Date
    Sep 2007
    Location
    UK
    Beans
    371
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: ls -l work differently when used in bash script

    part of the script removes the folder and recreates it empty. The ls is there to allow the user to optionally view the contents before proceeding.
    Always do things the hard way. After a while you learn how to make the hard way the easy way.

    My blog: http://foreverythingit.co.uk

  6. #6
    Join Date
    Sep 2007
    Location
    UK
    Beans
    371
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: ls -l work differently when used in bash script

    by the way that checker is pretty sweet I've bookmarked it thanks a lot
    Always do things the hard way. After a while you learn how to make the hard way the easy way.

    My blog: http://foreverythingit.co.uk

  7. #7
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: ls -l work differently when used in bash script

    Quote Originally Posted by Cheesemill View Post
    PS - If you are new to bash scripting I've recently discovered this great site. Just paste your script and it will let you know of any syntax errors and bad practices it sees.
    I'm not new to bash scripting, but that is a pretty sweet site. Thanks for the link!
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

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
  •