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 ...
- 02-07-2012 #1Just 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:
So with these two commands, entered individually: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
The output to done.txt is:Code:name=`find -maxdepth 2 -mindepth 2 -type d -printf '%P\n'` echo "${name//\//-}">done.txt
That is EXACTLY what I want.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
BUT.... if I write it as a script like this:
It fails.Code:#!/bin/sh -l name=`find -maxdepth 2 -mindepth 2 -type d -printf '%P\n'` echo "${name//\//-}">done.txt
My output to the terminal is this:
What am I missing here?Code:./listfinished: 4: Bad substitution
If I write the script without the substitution, it works fine, albeit not really formatted like I would like.
- 02-07-2012 #2
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:
Try using /bin/bash in the shebang line and see if that works.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.DISTRO=Arch
Registered Linux User #388732
- 02-07-2012 #3Just 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?
- 02-07-2012 #4
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.
- 02-07-2012 #5Just Joined!
- Join Date
- Feb 2012
- Posts
- 4
That makes good sense.
Thank you both for the teachings.


Reply With Quote