0

I am making a script that allows users to save their settings in a text file. If they want to use the saved settings, they will need to input the name of the settings. Then the script will compare the user's input to find for the name in the text file. If it finds a matching name, it will extract the whole line from that text file. So far my code looks like this.

echo enter setting name
    read name
    #count=0
    while IFS= read line
    do
        if [ "$name" == "$settingName" ]
        then
            cp **/*"$ft1" $dir1
        else
            echo "file doesnt exist"
        fi
    done < preco.txt

The code i used to save the variables:

echo save settings?
    read decision
    if [ "$decision" = "y" ]
        then
        echo enter settings name
        read settingName

        echo $settingName $dir1 $ft1 >> preco.txt
    else
        echo "bye"
        exit

I want the $dir1 and $ft1 to input into cp

0

It would be nice if you included an example of the settings file, although we can guess from the code that saves the settings.

One problem with your code is that there is no "$settingName".

Something like this should work, assuming you don't have spaces in your parameters.

echo enter setting name
read name
found=false
while read settingName dir1 ft1
do
    if [ "$name" == "$settingName" ]
    then
        cp "$ft1" $dir1
        found=true
    fi
done < preco.txt
if ! $found; then
    echo "$name not found"
fi

The statement read settingName dir1 ft1 will read the three fields from your file preco.txt into the named variables.

The other point is, you don't want to display an error message for each line that doesn't match, you must read through the file and only display an error message if none of the lines matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.