Find the answer to your Linux question:
Results 1 to 5 of 5
I am trying to write the names of some directories to a text file. The directory structure is like this: Code: |_FINISHED |_2-1-12-605pm |_2897-COOPER-ROAD-OTSEGO-MI-12345_PROPROPERTY_020112 |_452-E-TEMPLE-AVE-KALAMAZOO-MI-49004_MACKYLTD_020112 |_9098-BAILEY-HOPKINS-MI-49328_AMNRENTALS_020112 |_123-OAK-ST-WAYLAND-MI-24680_PROPROPERTY_020112 So with these ...
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    4

    Help, please. Bash commands work in terminal, but not in script.

    I am trying to write the names of some directories to a text file.
    The directory structure is like this:
    Code:
    |_FINISHED
        |_2-1-12-605pm
           |_2897-COOPER-ROAD-OTSEGO-MI-12345_PROPROPERTY_020112
           |_452-E-TEMPLE-AVE-KALAMAZOO-MI-49004_MACKYLTD_020112
           |_9098-BAILEY-HOPKINS-MI-49328_AMNRENTALS_020112
           |_123-OAK-ST-WAYLAND-MI-24680_PROPROPERTY_020112
    So with these two commands, entered individually:
    Code:
    name=`find -maxdepth 2 -mindepth 2 -type d -printf '%P\n'`
    echo "${name//\//-}">done.txt
    The output to done.txt is:
    Code:
    2-1-12-605pm-2897-COOPER-ROAD-OTSEGO-MI-12345_PROPROPERTY_020112
    2-1-12-605pm-452-E-TEMPLE-AVE-KALAMAZOO-MI-49004_MACKYLTD_020112
    2-1-12-605pm-9098-BAILEY-HOPKINS-MI-49328_AMNRENTALS_020112
    2-1-12-605pm-123-OAK-ST-WAYLAND-MI-24680_PROPROPERTY_020112
    That is EXACTLY what I want.

    BUT.... if I write it as a script like this:
    Code:
    #!/bin/sh -l
    
    name=`find -maxdepth 2 -mindepth 2 -type d -printf '%P\n'`
    echo "${name//\//-}">done.txt
    It fails.

    My output to the terminal is this:
    Code:
    ./listfinished: 4: Bad substitution
    What am I missing here?
    If I write the script without the substitution, it works fine, albeit not really formatted like I would like.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I suspect that the problem is that the shebang line of your script is executing /bin/sh. You probably use /bin/bash for your shell.

    When you execute /bin/sh, even though it's often the same program as bash, it runs a shell with fewer features:
    Code:
           If bash is invoked with the name sh, it tries to mimic the startup behavior of  histori‐
           cal  versions  of  sh  as closely as possible, while conforming to the POSIX standard as
           well.  When invoked as an interactive login shell, or a non-interactive shell  with  the
           --login  option,  it  first  attempts to read and execute commands from /etc/profile and
           ~/.profile, in that order.  The --noprofile option may be used to inhibit this behavior.
           When  invoked as an interactive shell with the name sh, bash looks for the variable ENV,
           expands its value if it is defined, and uses the expanded value as the name of a file to
           read and execute.  Since a shell invoked as sh does not attempt to read and execute com‐
           mands from any other startup files, the --rcfile option has no effect.   A  non-interac‐
           tive  shell  invoked  with the name sh does not attempt to read any other startup files.
           When invoked as sh, bash enters posix mode after the startup files are read.
    Try using /bin/bash in the shebang line and see if that works.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Feb 2012
    Posts
    4
    What the crap.

    That was it. I never new that. Thanks for that. Alot.

    Any reason I should ever use /bin/sh over /bin/bash?

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    It may be, that there is no bash on a target system.
    For example:
    - A freebsd has sh and tcsh. bash would need to be installed.
    - embedded devices also do not neccessarily have bash

    So it depends on how portable you want your script to be.

    btw:
    Because bash is not in /bin/bash on e.g. freebsd, you could use this to gain some portability:
    Code:
    #!/usr/bin/env bash
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Feb 2012
    Posts
    4
    That makes good sense.

    Thank you both for the teachings.

Posting Permissions

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