Friday, July 29, 2022

[SOLVED] What is ./lks.sh: Permission denied in Linux when running a shell script?

Issue

When I run lks.sh file in my system it show permission denied:

./lks.sh  bash: ./lks.sh: Permission denied

What do I have to do to get this shell script to run?

This is my .sh file:

lokesh = "wait"
if[$lokesh == "wait"]
        echo "$lokesh"

else
        sudo shutdown -h now

Solution

Your script has a few issues.

First the “Permission Denied” is most likely because your script does not have execute rights which would allow the script to actually run. So you need to chmod it like this:

chmod 755 lks.sh

And then you should be able to run it. FWIW, the 7 and 755 gives you—the owner—execute, read & write permissions while the 5 gives group members and others execute & read permissions. Feel free to change that to 744 so you are the only one who can edit that script but others—via 4—can read it. Or even 700 so you are the only one who can ever do anything with that script.

But that said, your variable assignment for this seems off:

lokesh = "wait"

In my experience, there should be no spaces around the = like this:

lokesh="wait"

Next the spacing of this is syntactically incorrect:

if[$lokesh == "wait"]

It should be:

if [ $lokesh == "wait" ]

And finally your whole if/else syntax is incorrect; no then and no closing fi. So here is your final, cleaned up script:

lokesh="wait"
if [ $lokesh == "wait" ]; then
    echo "$lokesh"
else
    sudo shutdown -h now
fi

That said, the most immediate issue is the execute rights issue, but the other things will definitely choke your script as well.



Answered By - Giacomo1968
Answer Checked By - Gilberto Lyons (WPSolving Admin)