简体   繁体   中英

Shell script multiple if else statements

Here's the code but i don't know how to correctly put if else in that, Because it's not java or C#, i need help..... first i want to create a folder if exist then go to folder and create file if file already exist then append or replace text but if file don't exist creates one and then do it, and if folder don't exist then create a folder and then add file and add text and so on....

#! /bin/bash

echo "Enter folder name"
read folder
 
if [[ -d "$folder" ]]
then
        echo "Folder already Exits"     
        cd $folder
        echo "Now you are in your Folder named : $folder "
        echo "Enter file name"
        read file
        if [[ -f "$file" ]]
        then
                echo "File already exits"
                echo "Enter text you want to put in file"
                read fileText
                echo "$fileText" > $file
                echo "File text has been replaced with new text"
        else
                touch $file
                echo "File has been created"
                echo "Enter text you want to add"
                read FileText2
                echo "$FileText2" >> $file
        else
                mkdir $folder
                echo "You are inside folder"
                echo "Enter file name"
                read fileName
                echo "Enter text to append in file"
                read text
                echo "$fileName" >> $text
        fi
fi
 

You aren't finishing the if [ -f "$file" ] so the else is not paired with the if [ -d "$folder" ] . Change this:

    else
            mkdir $folder
            echo "You are inside folder"
            echo "Enter file name"
            read fileName
            echo "Enter text to append in file"
            read text
            echo "$fileName" >> $text
    fi
fi

to this

    fi
    else
            mkdir $folder
            echo "You are inside folder"
            echo "Enter file name"
            read fileName
            echo "Enter text to append in file"
            read text
            echo "$text" >> $filename
    fi
#!/bin/bash
echo "Enter folder name"
read -r folder
if [[ -d "$folder" ]];then
echo "The folder already exists with name: $folder"
echo "Enter file name"
read -r file
cd "$folder" || exit
if [[ -f $file ]];then
echo "The file already exists with name: $file"
echo "Enter the text to append in $file"
read -r text
echo "$text" >> "$file"
else
touch "$file"
echo "File is created, Enter the text to append in $file"
read -r text
echo "$text" >> "$file"
fi
else
mkdir "$folder"
echo "New folder is created, Enter the filename"
read -r file
cd "$folder" || exit
touch "$file"
echo "Enter text to append in $file"
read -r text
echo "$text" >> "$file"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM