Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I'm trying to get a directory listing for use in a BASH script, however all of the sub-directories (the information that I want) are named according to the date ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Location
    Melbourne, Australia
    Posts
    4

    Bash, force variable to string


    Hi,

    I'm trying to get a directory listing for use in a BASH script, however all of the sub-directories (the information that I want) are named according to the date format: YYYY-MM-DD, eg 2005-06-10.

    If I have this line in the script:

    Code:
    let path="$(ls calls/)"
    path evaluates the directory name (eg 2005-06-10 => 1989), or if there is more than one directory, results in an error.

    I also tried using perl to get the directory listing then escaping the '-'s however that didn't seem to work either.

    Basically I want to force the 'path' variable to be a string, however as far as I can see, this can't be done using BASH.

    Any help would be greatly appreciated.

    -Pete

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I'm not quite sure what the problem was with Perl...

    I made a Perl script that looks for the same directory (2005-06-10), and prints it, which worked properly.

    I then tried piping its output into echo instead, which again properly did it as a string.

    So, if there's no easier way, why not just use a Perl script to grab the ls, then end the Perl script with:

    Code:
    system "./my_script $ls_output";

  3. #3
    Just Joined!
    Join Date
    Jun 2005
    Location
    Melbourne, Australia
    Posts
    4
    Thanks for the quick reply

    I hadn't thought of doing it that way.

    Got it to work now by doing as you suggested, created a separate perl script which gets the directory listing and then calls the other script. Not the neatest solution, but it works.

    Thanks again.

    -Pete

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Why not use path=$(ls calls/) ?

  6. #5
    Just Joined!
    Join Date
    Jun 2005
    Location
    Melbourne, Australia
    Posts
    4
    Why not use path=$(ls calls/) ?
    The directory name is 2005-06-10 and is being evaluated as an equation, ie, path becomes 1989 and not the directory name... unless I was messing something up...

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    It is being evaluated as a numerical expression because
    you use let.Don't use let.

  8. #7
    Just Joined!
    Join Date
    Jun 2005
    Location
    Melbourne, Australia
    Posts
    4
    Oh crap.

    Thanks, that works to.

    I've not done much BASH scripting before and was copying from an example... Might go and do some more reading.

    Thanks for your help.

    -Pete

Posting Permissions

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