Reputation: 367
The following code is used to create a file in exactly location.However result of running test.sh abc is
touch: missing file operand
#! /bin/sh
#test.sh
location='~/configuration/'$1
echo $location
touch `$location`
Is there anything wrong?Thanks!
Upvotes: 1
Views: 7008
Reputation: 8587
Is location a folder or file ? if file -f if folder -d in below script --- bash should understand ~
#! /bin/bash
#test.sh
var1=$1;
location=~/configuration/$var1
echo $location
if [ ! -f $location ]; then
echo "not found touching $location"
touch `$location`
fi
Upvotes: 2
Reputation: 246847
When you put ~
in quotes, it loses its special meaning. You need:
#! /bin/sh
#test.sh
location=~/configuration/"$1"
echo "$location"
touch "$location"
You should get into the habit of always putting variables in double quotes (until you learn the specific times when you don't want quotes).
Upvotes: 2
Reputation: 852
There are several ways to do this, but the first fix is to change to
touch `echo $location`
However I think it might be simpler to do
touch ~/configuration/$1
Upvotes: 0
Reputation: 272297
By putting $location
in backticks, you're trying to execute the value of $location
and use that as the argument to touch
(see here, section 3.4.5) Just do:
touch $location
If you run #!/bin/sh
with the -x
value, you'll see more clearly what bash
is doing in your shell script. It's a very useful means to debug scripts.
Upvotes: 3