0

I have recently just made this script:

 if test -s $HOME/koolaid.txt ; then
  Billz=$(grep / $HOME/koolaid.txt)
  echo $Billz
  else
 Billz=$HOME/notkoolaid
 echo $Billz
 fi



 if test -d $Billz ; then
  echo "Ok"
 else touch $Billz
fi

So basically, if the file $HOME/koolaid.txt file does NOT exist, then Billz will be set as $HOME/koolaid.txt. It then sucesfully creates the file.

However, if I do make the koolaid.txt then I get this

mkdir: cannot create directory : No such file or directory

Any help would be appreciated

DeaIss
  • 2,525
  • 7
  • 27
  • 37

2 Answers2

1

Here is a difference between content of a variable and evaluated content... if your variable contains a string $HOME/some - you need expand it to get /home/login/same One dangerous method is eval.

bin=$(grep / ~/.rm.cfg)
eval rbin=${bin:-$HOME/deleted}
echo "==$rbin=="

Don't eval unless you're absolutely sure what you evaling...

clt60
  • 62,119
  • 17
  • 107
  • 194
  • I still don't understand why my command doesn't work. I would rather not meddle around in eval for now, especially as I am beginner and as you said it can be dangerous? – DeaIss Jun 20 '13 at 21:04
  • 1
    @oOTesterOo because the **string** `$HOME` need to be expanded... see my (working) example.. – clt60 Jun 20 '13 at 21:10
0

Here are a couple things to fix:

Start your script with a "shebang," such as:

#!/bin/sh

This way the shell will know that you want to run this as a Bourne shell script.

Also, your conditional at the top of the script doesn't handle the case well in which .rm.cfg exists but doesn't contain a slash character anywhere in it. In that case the rbin variable never gets set.

Finally, try adding the line

ls ~

at the top so you can see how the shell is interpreting the tilde character; that might be the problem.

catfood
  • 4,267
  • 5
  • 29
  • 55
  • Thanks for the quickly reply! I have added the shebang line now. Also, I am assuming that it DOES have the slash character for now. I will try and make this line more specific later, but for now it just seems that it wont even create the directory I want once the variable has been changed. I still can't find the problem – DeaIss Jun 20 '13 at 20:07
  • 1
    What happens with "ls ~" in the script? Is ~ doing what you expect it to? – catfood Jun 20 '13 at 21:10