Domanda

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.

È stato utile?

Soluzione

Proabably you need a /.

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

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top