13

I need to create a bash script to create and mount a drive. So, two simple commands. Both "work" when entered at the command line.

The script is created and executed every time a normal user logs in, so I need a method to make this script executable at that time. So far, I cannot get this to work. For example, the first part mkdir /vvv/gggg doesn't proceed because the script is not executable (I'm guessing).

Hope this makes sense. Is this possible? Any thoughts on how to make this work will be appreciated.

Update:

Thanks for your responses. I probably should add some additional information other than that I'm new to Linux.

I'm using an open source virtual desktop application called Ulteo. This App runs on top of Ubuntu and has very little support - that why i'm here. Basically, I'm learning by fire.

So, there application has a login script management function where I can tie a script to a user. A simple windows script with net use works perfectly fine. However, when I try to apply a Linux bash script nothing happens.

I'm thinking that because I need to perform a chmod +x against the script first to make the script executable, this is why its failing. By the way, Ulteo runs in a chroot jail. I've created a script, saved it and could not find the script. I searched both inside and outside the chroot jail.

I like the approach by dan08 to have the initial script reference another script that i can find and make executable manually. Would I run into the same problem?

Does this additional information clarify the situation? Thanks in advance.

Photos attached.

![login scipt management console][1]

![windows scripts that work][2]

![simple linux script that doesn't work][3]

Sorry I Can't post images yet

6
  • Are you going to write a cron job to run the script every time a user is logged in? This guide should help you with making them executable too. Sep 9, 2014 at 12:20
  • 1
    "The script is created and executed every time a normal user logs" why? I would fix this since it sounds unlogical to me. I would feed the script the user as a parameter and leave the script untouched after created.
    – Rinzwind
    Sep 9, 2014 at 12:27
  • 1
    There is a strange mix of tenses in the question that leaves me confused about what currently happens and what you'd like it to do. It would help if you could be a little more specific about what the current situation is.
    – Oli
    Sep 9, 2014 at 12:27
  • Please edit your question and show us the script. Also show us the output of ls -l scriptname. You should also clarify how exactly the script is created and why you don't just create it once and leave it alone. Finally, your normal user shouldn't have write access to /var and should therefore not be able to create directories there.
    – terdon
    Sep 9, 2014 at 12:44
  • 1
    Post the the image links in the comments. We'll import them via (suggested) edits.
    – muru
    Sep 9, 2014 at 23:00

5 Answers 5

20

There are two things you need to do:

  1. Reference the script interpreter at the beginning of the script:

    #!/bin/bash
    
  2. Set the permissions to make it executable:

    chmod +x myscript.sh
    
3

Rather than making your script executable, you can take a different approach and change the way you call the script.

script.sh is enough to run an executable script.

However

It is possible to run a non-exectuable script if you specify the program to run it.

So /bin/bash script.sh or bash script.sh or python script.py will all run non-executable scripts.

So my suggestion would be to edit the way that the script is called, and specify the binary used to run it.

1
  • On my side i usually make them excutable, because if you don't do that you can't relaunch the file from itself. As example, python os.execv(__file__, sys.argv) would not work, and for sure there are more situations like that. If security is a goal, just use it from root/sudo instead let users to run binaries. Also, if the goal is security, you don't want them to be able running scripts, because you can harm the system anyway... executable or not. So I don't see why is important to avoid make them executables.
    – m3nda
    Nov 27, 2015 at 17:19
0

Create a program in C,C++ or Python etc. and use the system command to execute the bash script.

For example a C program would look like:

#include "stdio.h"
int main(int argc, char const *argv[])
{
    system('chmod +x path/yourbashscript.sh');
    system('path/yourbashscript.sh');
    return 0;
}

To execute the program at startup add it to the list of startup programs. Open Startup Applications from your dash and add the compiled executable to the list by clicking add button.

7
  • 2
    -1: This is not really an answer. Why would python be any better than a shell script? Let alone C! In any case, you're not actually showing what this program would look like and you're not explaining how to add a program to the startup program list.
    – terdon
    Sep 9, 2014 at 12:45
  • The answer to your first question can be given by explaining how a programming language is different from a scripting language as for the rest I have edited my answer
    – akxer
    Sep 9, 2014 at 12:57
  • Thanks for the edit, -1 removed. However, using a C program to launch a shell script on Linux is completely unnecessary and adds needless overhead. Why not just add the script itself to the startup applications? Or, if you really want to use C why not implement it directly? Creating a wrapper in C that calls a shell script under Linux makes no sense at all unless you need the script to be run with the SUID bit set. Even in that case, there are usually simpler solutions.
    – terdon
    Sep 9, 2014 at 13:05
  • 1
    -1: This would still fail if the script didn't have execute permissions. You'd have to call bash path/yourbashscript.sh.
    – muru
    Sep 9, 2014 at 13:06
  • 1
    Which raises the question, why not run bash path/yourbashscript.sh directly instead of compiling and running this C program?
    – muru
    Sep 9, 2014 at 13:23
0

You can make your bash script executable by running the following in the command line.

cd ~/path/to/file
chmod +x nameoffile.sh

Then, making it executed at start up can be done by adding it to your list of startup programs as @akabhirav said.

1
  • It seems to me the question (somewhat) shows that he is familiar with how to chmod. But he is asking for a method to make it executable from within the script itself (so he does not have to do this manually). Though I still question the method: recreating a script on the fly is not the way to do this.
    – Rinzwind
    Sep 9, 2014 at 13:54
0

SHC is a generic shell script compiler. It takes a script, which is specified on the command line and produces C source code. The generated source code is then compiled and linked to produce a stripped binary.

The compiled binary will still be dependent on the shell specified in the first line of the shell code (the shebang: #!/bin/sh or such), thus SHC does not create completely independent binaries.

SHC itself is not a compiler such as cc. Rather encodes and encrypts a shell script and generates C source code with the added expiration capability. It then uses the system compiler to compile a stripped binary which behaves exactly like the original script. Upon execution, the compiled binary will decrypt and execute the code with the shell's -c option.

1
  • What is the point of this if the executable still requires the original shell environment? How is this any better than just adding a #! and chmod +x?
    – xiota
    Aug 3, 2018 at 21:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .