-2

In my code here everything works fine. Except the echo=$folder command in the if else statement ends up doing nothing!

How would I fix this?

#!/bin/bash
echo "Enter the path to the folder. If already in folder type 1."
read input
echo
echo
if [ $input == 1 ];then 
folder=$PWD;echo="$folder"
else
folder=$input;echo="$input"
fi
cd $folder
ls
echo
echo
echo "File Name"
read file
sudo chmod +x $file
echo
echo
echo Done
exit
2

Utsav gave you the correct solution. The issue was that you were assigning to a variable called echo.

However, I'll give some further suggestions for improving your script.

At the moment, you can't choose a folder whose name is 1. Also, the script is needlessly interactive without having to do any interaction with the user at all. If the user, for example, gave a folder name on the command line when they invoked the script, you would not have to ask for a folder name at all. Only when the user did not supply the name of a folder would you need to use the current working directory:

#!/bin/sh

folder="$1"

if [ -z "$folder" ]; then
    printf 'No folder given, using "%s"\n' "$PWD" >&2
    folder="$PWD"
fi

Then, why force the user to type in the name of a file in that folder at all? You can have them pick a file from a menu:

select file in "$folder"/*; do
    printf 'Making "%s" executable with "sudo chmod +x"\n' "$file"
    sudo chmod +x "$folder/$file"
    break
done

Complete script that will skip the menu if a valid file was given on the command line:

#!/bin/sh

folder="$1"

if [ -z "$folder" ]; then
    printf 'No folder given, using "%s"\n' "$PWD" >&2
    folder="$PWD"
elif [ -f "$folder" ]; then
    # $folder is actually a file, chmod it and we're done
    sudo chmod +x "$folder"
    exit
fi

if [ ! -d "$folder" ]; then
    printf 'No such folder: %s\n' "$folder" 2>&1
    exit 1
fi

select file in "$folder"/*; do
    printf 'Making "%s" executable with "sudo chmod +x"\n' "$file"
    sudo chmod +x "$folder/$file"
    break
done

If this script is called script.sh, then one would be able to run it in the following ways:

$ ./script.sh                  # asks for file in current folder
$ ./script.sh myfolder         # asks for file in "myfolder"
$ ./script.sh myfolder/myfile  # quietly chmods "myfolder/myfile"
  • Interesting you got me thinking you know how say I type sudo without any arguments it says usage: sudo -h | -K | -k | -V usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user] usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command] usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-u user] [VAR=value] [-i|-s] [<command>] usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-u user] file .. – Darth4212 Jul 2 '17 at 18:32
  • Would it be possible to add something like this? Like --current would use current directory. And -d could be used to specify a directory? Sorry if this is putting you out in any way I just want to learn more about stuff like this and you seem pretty knowledgeable. – Darth4212 Jul 2 '17 at 18:35
3

Replace echo="$folder" with echo "$folder"

Same for echo="$input"

Here echo is acting as a variable. = will try to assign value to it, and not run the echo command it self. If after echo="$folder" you run echo $echo, you will pwd value.

Reproducing your result;

/home/test$ folder="$PWD"
/home/test$ echo="$folder"
/home/test$ echo "$echo"
/home/test
  • Thank you very much. I initially didn't think to realize this. I am sort of new to shell scripting so I probably will need to work on it. I – Darth4212 Jul 2 '17 at 11:23

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.