문제

I am trying to write a script that will allow the user to input the path of the directory to which they want to store a file. I have something that looks like this:

#! /bin/bash
echo "Enter the directory path"
read varpath
varfile="Filename"
echo "This is a file" > "$varpath$varfile"

what I want is a file in the directory path the user entered called "Filename" and has the line "This is a file" However, I am getting an error saying that there is not such file or directory. Can someone tell me whats wrong, or if there's an alternate solution to this.

Thanks.

도움이 되었습니까?

해결책

Proabably you need a /.

echo "This is a file" > "${varpath}/${varfile}"

You can also echo the variables to make sure they are correct

다른 팁

Few things you might want to handle:

  1. The user input directory has the required folders, if not create them using mkdir -p command.

  2. The user enters dir path that either ends with / or not. If not, you might want to add it yourself.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top