0

The code I have is supposed to ask for the user to enter a name of a directory they wish to create, then it is supposed to ask to edit files within the directory, however when after I create the directory the script just doesnt continue, i can't see any errors, but it's always easier for a fresh set of eyes to critique code.

I have also added files to the directory but it never asks me if I wanted to edit them.

#!/bin/bash

#Testing to see if input is empty 
if [ $# -lt 1 ]; then
    echo "Empty Directory will be created"
fi

#Get the name of the directory by the user, also creating a variable named directory 
read -p "Please enter the name of the drectory you wish to create: " directory

#Check if the directory exists, if it doesn't it will be created in the Home folder
if [ ! -d ~/$directory ]; then
#Creating the directory if it doesnt exist
    mkdir ~/$directory/
fi

#Create files individually in the directory 
for i in "$@"; do
    touch ~/$directory/$i
#Asking the user if they wish to edit the files they have created inside the directory
    read -p "edit file $i (Y/N)? " edit
#If they answer yes then read the lines entered by the user

if [["$edit" = "Y" || "$edit" = "y"]]; then
    line=""

    #Stores the amount of words added to the file
    count=0

    #Reads the lines enetered by the user 
    echo "Please enter your text to be added into the file (Enter \"end\" to exit the editing):"
    read line

    #The script will keep reading the words entered in the file until the user initiates the end command "end"

        while ["$line" != "end"]; do
    
        #repeat the words entered into the file
        echo "$line" >> ~/directory/$i
    
        #Get the amount of words entered into the file
        count=$(($count + $(wc -w <<< $line)))
    
        #read the next line from user input
        read line 
        
    done
    echo "$count words have been written to the file"
    
fi
done

Blue Moon
  • 35
  • 3
  • You'll want to make sure that the user is in their `~/directory` when running the script. If they are running the script from anywhere else on the system `~/directory`, then the files aren't going to be there. – Nasir Riley Nov 30 '20 at 22:46
  • 5
    [Why are bash tests so picky about whitespace?](https://unix.stackexchange.com/questions/117438/why-are-bash-tests-so-picky-about-whitespace) - also you may find [www.shellcheck.net](https://www.shellcheck.net/) helpful – steeldriver Nov 30 '20 at 22:46
  • 1. Some variables are not quoted 2. `~` is not recommended to be used in scripts 3. `set -x` almost always helps – Artem S. Tashkinov Nov 30 '20 at 23:29
  • @NasirRiley so I should be in the folder I created directory and then run the script? – Blue Moon Nov 30 '20 at 23:46
  • @BlueMoon Yes. In fact, just to make sure, they should be in `/home/user/directory` as the tilde may not be expanded depending on the environment. – Nasir Riley Nov 30 '20 at 23:54
  • What do you intend by `for i in "$@"`? – roaima Dec 01 '20 at 00:08
  • @roaima it is supposed to make files in the directory one by one, thats why I have touch ~/$directory/$i. Like im using mkdir to create the directory, this portion is supposed to add files in the directory one by one, but nothing ever gets created – Blue Moon Dec 01 '20 at 00:30
  • Your `"$@"` is the set of files/directories you specify on the command line when you run your program. If you don't provide any, then the loop will never happen – roaima Dec 01 '20 at 00:44
  • Okay, so I made it work sort of, I can create a folder, lets say name, but it creates a file inside of name called name, then when I run the script again it creates a new folder with what ever i entered and calls it that name with a file inside it, rather than making a directory and just creating files over and over within that directory. I did this by changing "$@" to "$directory" – Blue Moon Dec 01 '20 at 00:57

1 Answers1

2

Change this line

if [["$edit" = "Y" || "$edit" = "y"]]; then

With this line:

if [[ "$edit" = "Y" || "$edit" = "y" ]]; then

You are missing a space after [[ and before ]].

Also: Better use $HOME instead of ~

LincolnP
  • 496
  • 4
  • 15