Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ln complains about no such file or directory

I'm new in shell programming on macosx and have a little problem. I've written the following shell script:

#!/bin/sh

function createlink {
source_file=$1

target_file="~/$source_file"

if [[ -f $target_file ]]; then
    rm $target_file
fi

ln $source_file $target_file
}

createlink ".netrc"

When I'm executing this script I get the message ln: ~/.netrc: No such file or directory and I don't know why this happened! Do you see the error? Thanks!

like image 848
Jan Baer Avatar asked May 18 '13 09:05

Jan Baer


People also ask

How do you solve No such file or directory?

To solve No Such File Or Directory Error in Python, ensure that the file exists in your provided path. To check all the files in the directory, use the os. listdir() method.

Why am I getting no such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.

How do I resolve No such file or directory in Linux?

First, make sure you execute the program with the correct path. If you make a typo on the directory or file name, you will get this error or give the wrong path. If you are executing the file with a relative path ( ../../file ), try executing with the absolute path ( /path/to/file ) instead.


1 Answers

The issue is that tilde expansion will not happen as the path is in a variable value (tilde expansion happens before variable expansion). You can ameliorate this issue by using $HOME instead of ~. That is

target_file="${HOME}/${source_file}"

This should solve your problem.

Further reading: EXPANSION section of man bash

like image 124
Samveen Avatar answered Nov 10 '22 23:11

Samveen