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