Bash script login as root

hi,
i am using opensuse 12.1. i am a bash newbie. i want to write a simple bash script that will login as root (super user) [then execute some simple commands]. is there a way i can do that, without having to hard code my password in the script?
any help would be appreciated.
thanks,
db

It is possible to run terminal commands as root from a bash script if they can be run at the start of your openSUSE session, or later as a standard user. If the command will not work with the terminal sudo command, it will not work with my bash script. To run commands as root at the start, before you log in, consider using the after.local file. I have a blog on how to get this working:

systemd and using the after.local script in openSUSE 12.1 - Blogs - openSUSE Forums

To run commands as root, after you have logged in have a look at this one:

asroot version 1.14 allows you to run a program (with options) as a root user - Blogs - openSUSE Forums

If you need to use kdesu or gnomesu, my asroot bash script is not going to help you out. Always consider becoming root as something you only do when required and not something you do all of the time.

Thank You,

Normally you won’t log in as root to execute commands. You simply excute a script including your commands as root. When you log in, you open an 'interactive shell which might not be initialized in the same way as the non-interactive shell that is started when you run a script. The built-in variable $- can be evaluated to find out if the shell is interactive or not. When you open a shell in a terminal - not necessarily a login shell - it contains the letter ‘i’. These two commands show you the difference between an interactive an a non-interactive shell:


$ printf "echo $-
" | bash -
himBH

$ printf "echo \$-
" | bash -
hB

Now which one is an interactive shell? (Think twice!) :wink:

If you don’t mind telling us which kind of commands you want to execute, we might be able to tell you what’s the most appriate moment and the best place to put your script.

thank you for the replies. well, i have matlab installed. and in orde rto start matlab, i have to logon as the super user, then change directory, then type ‘./matlab’ to start matlab. i basically want to write a bash script that will login as the super user, then run a matlab program. i want to also schedule the running of this bash script (using cron) so i runs daily at a specified time. hope this helps!
db

If you need to be root to run matlab, you did something wrong when you
installed it I would say. Matlab is an end user program, which does not
need root rights to run.
You should fix that mistake (whatever it was) instead of working around
it by running it as root.


PC: oS 12.1 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.4 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

You might be interesting in reading post #6 on this page: MatLab or not?

Am 01.08.2012 02:26, schrieb please try again:
>
> You might be interesting in reading post #6 on this page: ‘MatLab or
> not?’ (http://tinyurl.com/c5cnwdg)

Sounds like the main reason to run it as root is to bypass license
restrictions, I would not really consider that as legal.


PC: oS 12.1 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.4 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

actually, you are correct. while i do have a licensed version, something may have gone awry in the installation…after reinstalling, i can now run it without logging in as super user!
thanks!
db

Am 01.08.2012 04:36, schrieb blackbird sr71:
>
> after reinstalling, i can now run it without logging in as super
> user! thanks! db
>
Glad to hear that. Btw my comment about bypassing the license not being
legal was not directed to you, it was a comment on the link from
please_try_again where the user in that link described that by running
as root he is able to bypass the limitation of his license being bound
to one special user on the system.


PC: oS 12.1 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.4 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

That’s the kind of problem you get (sooner or later) with this kind of software. :frowning:

Am 01.08.2012 13:06, schrieb please try again:
>
> blackbird_sr71;2477591 Wrote:
>> actually, you are correct. while i do have a licensed version, something
>> may have gone awry in the installation…after reinstalling, i can now
>> run it without logging in as super user!
>> thanks!
>> db
>
> That’s the kind of problem you get (sooner or later) with this kind of
> software. :frowning:
>
>
+1
a close look to the free alternatives could be a viable alternative
using myself GNU Octave now for years I never missed Matlab (last time I
used Matlab myself was now probably 6 or 7 years ago).
There is also Scilab (has a gui and something which is a little bit
similar in spirit to Simulink).


PC: oS 12.1 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.4 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10