Create a directory with command expansion: quoting issue - shell

I have the following typical problem with my workflow: I want to copy some files to a directory that is not yet created. Mostly I do it in this way:
cp *.JPG /some/path/[oops, I recalled that the directory does not exist, ^C]
mkdir /some/path/newdir
cp *.JPG /some/path/newdir
To avoid this I created a script called md that creates a directory and returns its path:
#!/bin/sh
mkdir -p "$*"
echo "$*"
Then my workflow looks like this:
cp *.JPG `md /some/path/newdir`<TAB><ENTER>
When I press <TAB>, md is executed, the directory created and line with backticks substituted with its path, then I can press <ENTER> and execute cp.
Everything works fine unless I have spaces in the path of the new directory. In that case, after expansion, I have unescaped spaces on my command line.
I tried to overcome this by putting the path in quotes like this (in md):
echo "'$*'"
But in this case after expansion I have quotes escaped:
cp *.JPG \'/Users/user/temp/one two three\'
Is there any way to avoid escaping of quotes on command expansion in zsh? I tried $() and different types of quotes (" instead of '), but nothing works.
Of course, I can just do it in this way:
cp *.JPG "`md /path/with spaces`"
But I want to save a couple of keystrokes, if possible.

I think the easiest way to solve that is by creating a script/function for the whole thing (I'd prefer a function, because you're presumably going to be using it only from an interactive shell session):
#Ensures Targetdir exists and then copies into it
#Usage: cp_t Targetdir FlagsAndFiles...
cp_t()
(
targetdir="$1"; shift || return 1
[ -d "$targetdir" ] || mkdir -p "$targetdir" || return 1
exec cp "$#" "targetdir"
)
(This is POSIX too, so you should be able to use it with other POSIX shells as well.)

Related

ksh - why presence of the "\" file in current working directory changes behavior of variable processing

I had an issue with third party ksh script.
Found out, that it was failing because of file named "\" in user home directory.
Here is a simple testcase:
$ mkdir -p ~/dir1 && cd ~/dir1 && touch '\' && x="\* a" && echo $x
\ a
$ mkdir -p ~/dir2 && cd ~/dir2 && x="\* a" && echo $x
\* a
The question is, why the presence of "\" file in a working directory changes the result.
Is this expected?
Thanks.
T.
Looks like the expected behaviour.
If you want the same behaviour in both cases, either use set -o noglob inside your script, or run the script with the -f option to disable file name substitution.
The default is that the * is a special character when interpolating so will match whatever file exists (in your case dir1 will contain only one real file with the name of the backslash character.)
The second directory dir2 has no real files so ksh just shows the pattern exactly as you typed it.

Questions about bash

Firstly, I'm wondering how to input information from the terminal into a variable in the script file. For example, lets say I wanted to do ./name.sh dave in the terminal instead of using read -p to ask for the name in the script. Secondly, I'm wondering how to go about creating a new directory and then copying files into that directory. I know how to use the mkdir command, but not how to copy files to that new directory.
Sorry if my wording is a bit bad I wasn't sure how else to ask the questions (this is my first day messing with bash.)
When you run:
./name.sh dave
the string dave will be the first positional argument in the script. You can access it with $1. To create a directory named dave and copy files into it, you might do:
#!/bin/bash
dir=${1:?}
mkdir "$dir" || exit
cp * "$dir"
A few things are a bit cryptic, and perhaps you might prefer:
#!/bin/sh
if test -z "$1"; then
echo "Parameter missing" >&2;
exit 1
fi
mkdir "$1" && cp * "$1"
Basically, you access the parameters via $1, $2, etc. The ${1:?} syntax is a shortcut that assigns the variable dir, but aborts the script if $1 is unset or empty. (eg, if you call the script without an argument.)
The rest seems pretty self-explanatory.
Suppose you wanted to specify the files to copy, so that ./name.sh dave would create a directory named dave and copy all files in the current directory to it (as above), but if you pass more arguments it would copy only those files. In that case, you might do something like:
#!/bin/bash
dir=${1:?}
shift # Discard the first argument, shift remaining down
mkdir "$dir" || exit
case $# in
0) cp * "$dir";;
*) cp "$#" "$dir";;
esac
Here, "$#" is the list of each argument, individually quoted. (eg, if you call the script with an argument that has spaces, it will properly pass that argument to cp. Compare that with cp $# $dir or cp "$*" $dir.) If you're just starting with shell scripts, I would advise you always be careful about quotes.

Execute command with backquote in bash shell script

I write up a little shell script in bash that allows me to execute commands in sub-directories. Here is the script
bat.sh:
#!/bin/sh
for d in */; do
echo "Executing \"$#\" in $d"
cd $d
`$#`
cd ..
done
With my following directory structures
/home/user
--a/
----x.txt
----y.txt
--b/
----u.txt
----v.txt
I expect the following command to list out the content of directories a and b when it is executed in the home directory
bat.sh ls
The result is
Executing "ls" in a/
/home/user/bin/bat.sh: line 6: x.txt: command not found
Executing "ls" in b/
/home/user/bin/bat.sh: line 6: u.txt: command not found
Any idea on what is going wrong here?
You don't want the back quotes; you want double quotes.
#!/bin/sh
for d in */
do
echo "Executing \"$*\" in $d"
(cd "$d" && "$#")
done
You are trying to execute the output of the command you pass, whereas you simply want to execute the command.
The use of an explicit subshell (the ( … ) notation) may avoid some problems with symlinks that jump to other directories. It is, in my (perhaps archaic) view, a safer way to switch directories for the purposes of executing commands.

In shell script, how to change current directory safely with variable?

The following shell script changes current the directory to the desktop.
v=~/Desktop/
cd $v
pwd # desktop
The following script changes the current directory to home directory instead of generating error.
cd $undefined_variable
pwd # home directory
echo $? # 0
I'm afraid that the script will remove important files if I misspelled a variable for new current directory.
Generally, how do you safely change current directory with variable in shell script?
Use:
cd ${variable:?}
if $variable is not defined or empty then bash will throw an error and exit. It's like the set -u option but not global through the file.
You can set -u to make bash exit with an error each time you expand an undefined variable.
You could use the test -d condition (checks whether the specified variable is a directory), i.e.
if [[ -d $undefined_variable ]]
then
cd $undefined_variable
echo "This will not be printed if $undefined_variable is not defined"
fi
See also here for further test options...
The Bourne Shells have a construct to substitute a value for undefined variables, ${varname-subtitution}. You can use this to have a safe fallback directory in case the variable is undefined:
cd "${undefined-/tmp/backupdir}"
If there is a variable named undefined, its value is substituted, otherwise /tmp/backupdir is substituted.
Note that I also put the variable expansion in double quotes. This is used to prevent word splitting on strings containing spaces (very common for Windows directories). This way it works even for directories with spaces.
For the gory details on all the shell substitution constructs (there are seven more for POSIX shells), read your shell manual's Parameter Substitution section.
You have to write a wrapper (this work in bash):
cd() {
if [ $# -ne 1 ] ;then
echo "cd need exactly 1 argument" >&2
return 2
fi
builtin cd "$1"
}
yes, that's shell
if you type cd without parameter it will jump to home dir.
You can can check the variable of null or empty before you cd command.
check like (cd only be called if targetDir is not empty):
test -z "$targetDir" || cd $targetDir
check like (cd only be called if targetDir really exist):
test -d "$targetDir" && cd $targetDir
Note: Thanks for -1, should read the last sentence too. So I added the real answer.

mkdir error in bash script

The following is a fragment of a bash script that I'm running under cygwin on Windows:
deployDir=/cygdrive/c/Temp/deploy
timestamp=`date +%Y-%m-%d_%H:%M:%S`
deployDir=${deployDir}/$timestamp
if [ ! -d "$deployDir" ]; then
echo "making dir $deployDir"
mkdir -p $deployDir
fi
This produces output such as:
making dir /cygdrive/c/Temp/deploy/2010-04-30_11:47:58
mkdir: missing operand
Try `mkdir --help' for more information.
However, if I type /cygdrive/c/Temp/deploy/2010-04-30_11:47:58 on the command-line it succeeds, why does the same command not work in the script?
Thanks,
Don
Change:
mkdir -p $deploydir
to
mkdir -p "$deployDir"
Like most Unix shells (maybe even all of them), Bourne (Again) Shell (sh/bash) is case-sensitive. The dir var is called deployDir (mixed-case) everywhere except for the mkdir command, where it is called deploydir (all lowercase). Since deploydir (all lowercase) is a considered distinct variable from deployDir (mixed-case) and deplydir (all lowercase) has never had a value assigned to it, the value of deploydir (all lowercase) is empty string ("").
Without the quotes (mkdir $deploydir), the line effectively becomes mkdir (just the command without the required operand), thus the error mkdir: missing operand.
With the quotes (mkdir "$deploydir"), the line effectively becomes mkdir "" (the command to make a directory with the illegal directory name of empty string), thus the error mkdir: cannot create directory'.
Using the form with quotes (mkdir "$deployDir") is recommended in case the target directory name includes spaces.
Change:
mkdir -p $deploydir
to
mkdir -p "$deploydir"
You can't have colons in file names on Windows, for obvious reasons.

Resources