Question

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.

Was it helpful?

Solution

Proabably you need a /.

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

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top