How can I know which shell I am using? - linux

I am writing a shell script. The tutorial that I am reading have the first line like this :
#!/usr/bin/env bash/
but it isn't working for me. (error : no such directory)
How can I find out which bash I am using and where is it located?
Appreciate for any advice and help.
Thanks a lot. It works now.
solution is #!/usr/bin/env bash
Another problem: Why it just can't read the word 'restart'
my code in the start.sh:
#!/usr/bin/env bash/
RESTART="apachectl restart"
$RESTART
I does not work.
Usage: /usr/local/apache2/bin/httpd [-D name] [-d directory] [-f file]
[-C "directive"] [-c "directive"]
[-k start|restart|graceful|graceful-stop|sto p]
[-v] [-V] [-h] [-l] [-L] [-t] [-S]
Options:
-D name : define a name for use in <IfDefine name> directives
-d directory : specify an alternate initial ServerRoot
-f file : specify an alternate ServerConfigFile
-C "directive" : process directive before reading config files
-c "directive" : process directive after reading config files
-e level : show startup errors of level (see LogLevel)
-E file : log startup errors to file
-v : show version number
-V : show compile settings
-h : list available command line options (this page)
-l : list compiled in modules
-L : list available configuration directives
-t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)
-S : a synonym for -t -D DUMP_VHOSTS
-t -D DUMP_MODULES : show all loaded modules
-M : a synonym for -t -D DUMP_MODULES
-t : run syntax check for config files
why is it like that? it seems that it can read the word restart.
Thank you all! I have fixed it now.
solution: edit the file in unix (vim/nano and whatever but not in windows)
Thank again :)

Yet another way: echo $SHELL.

If you remove the / from bash/, it should work.

You ca try the following command
which bash
at a shell. Then put
#!<the output of which bash>

To find out where bash is, issue the command:
type bash
at your command prompt. and to make sure it is always found by your script use:
#!bash
this has the problem that some other bash may be found and used, which could be security issue, but I have been doing this for years.

Remove the extra character(s) you have at the end of lines. No slash is required and
dos2unix yourscript will remove the unwanted CRs.
#!/usr/bin/env bash

Actually better would be to open a new question for your restart problem.
Most probably you are not at the directory where the restart command is
defined or restart is not in your path. Try putting the whole path.

Related

sudo: command not found

I am trying to execute screen as another user using sudo.
I'm using the command:
echo 'userpassword' | /usr/bin/sudo -u 'myuser' -S '/usr/bin/screen -ls'
Any help found on the internet says that the sudo clears the environment variables (like PATH). So I decided to use the full path to the applications but I'm still getting the command not found error.
Error:
sudo: /usr/bin/screen -ls: command not found
Sudo is installed on the system.
Screen is installed on the system.
For sudo, I have tried the -E and -H flag but it doesn't help.
I tried to set PATH variable using something like this:
... | /usr/bin/sudo -u 'myuser' -S 'env PATH=$PATH; /usr/bin/screen -ls'
Supposedly the $PATH was suppose to expand before the command executes but I was getting other errors...
Can someone provide a command that will let me execute commands as another user and explain what each part of the command does so I can understand it?
Thanks.
Try,
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH
Probably you replaced the path variable while trying to set a new path variable.
Going forward, do 'echo $PATH' before adding a new path variable.
There doesn's seem to be any need to encapsulate command in quotes, without them it even works.
echo 'userpassword' | /usr/bin/sudo -u 'myuser' -S screen -ls

CoreOS - Cloud-Config not saving file

I'm trying to write an "initial" cloud-config file that does a bit of setup before my default Cloud-Config file replaces it and takes over. This is what it looks like, however whenever it runs the "clustersetup.service", it can't find the clustersetup.sh file that was supposed to save. Course if I run this from a terminal it works just fine. What am I doing wrong?
#cloud-config
coreos:
etcd:
addr: $private_ipv4:4001
peer-addr: $private_ipv4:7001
fleet:
public-ip: $private_ipv4
units:
- name: clustersetup.service
command: start
content: |
[Unit]
Description=Cluster Setup
[Service]
ExecStartPre=/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~/clustersetup.sh
ExecStart=/usr/bin/bash ~/clustersetup.sh
ExecStop=/usr/bin/bash
Paths specified by systemd cannot be relative. Try this again specifying the full path /home/core/clustersetup.sh.
In my distribution (ubuntu), bash is in /bin. One thing you could do is:
ExecStartPre=/bin/bash -c '/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~/clustersetup.sh'
ExecStart=/bin/bash -c ~/clustersetup.sh'
I think you will get the proper expansion of the ~ when pushing it through the shell. However, ~ will be relative to the process id executing the script (I don't know for certain that is core). If you wanted to be sure, you could:
ExecStartPre=/bin/bash -c '/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~core/clustersetup.sh'
ExecStart=/bin/bash -c ~core/clustersetup.sh'
I haven't tested this. I agree with #Brian in that the explicit path would be a better idea. In general it is best not to get a shell involved with execution.

Bash: sourcing file as user from script

I am creating a script meant to be run as superuser that reads a file and runs a number of scripts on behalf of all users. The important bit is this:
sudo -u $user -H source /home/$user/list_of_commands
However, whether I encose the command with quotesor not, this fails with:
sudo: source /home/user/list_of_commands: command not found
I have even tried with the . bash builtin:
sudo: . /home/user/list_of_commands: command not found
Of course running source outside a sudo environment works. I thought there might be a PATH problem, and I tried to bypass it by providing the full path to source. However, I cannot find the executable: which source returns which: no source in (/usr/local/sbin:usr/local/bin:usr/bin). So I'm stuck.
How do I make a script source a file as a user?
source is a builtin not a command, use it with bash -c:
sudo -u $user -H bash -c "source /home/$user/list_of_commands"

Why this linux command can affect the environment variables?

When I changed my current user to admin using
sudo su admin
I found that the environment variable changed too. What I intend to do is to change my user to admin with the env not changed.
Then I found a command as follows:
sudo bash -c "su - admin"
This command does indeed what I want, but I googled about bash -c, with no clue to why this command can do that for me. Could anyone give me a clear explanation? Thanks a lot.
first you should read the sudo manpage and set theses options in the /etc/sudoers file or you can do it interactively (see second below).
default sudoers file may not preserve the existing $USER environment unless you set the config options to do so. You'll want to read up on env_reset because depending on your OS distribution the sudo config will be different in most cases.
I dont mean to be terse but I am on a mobile device..
I do not recommend using sudo su .. for anything. whomever is sharing sudo su with the public is a newb, and you can accomplish the same cleaner with just sudo.
with your example whats happining is you are starting a subshell owned by the original user ("not admin") . you are starting the subshell with -c "string" sudo has the equivelant of the shell's -c using -s which either reads the shell from the arg passed to -s or the shell defined in the passwd file.
second you should use:
$ sudo -u admin -E -s
much cleaner right ? :)
-u sets the user, obviously
-s we just explained
-E preserves the orig user env
see for yourself just
$ echo $HOME # should show the original users /home/orig_user
$ env
your original env is preserved with none of that sudo su ugliness.
if you were interested in simulating a users login without preserving the env..
$ sudo -u user -i
or for root:
Might require -E depending on distro sudoers file
$ sudo -s
or
$ sudo -i
-i simulates the login and uses the users env.
hopefully this helps and someone will kindly format it to be more readable since im on my mobile.
bash with -c argument defines below.
-c string
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
Thanks & Regards,
Alok

TWiki & Cygwin configuration

I'm trying to configure the cygwin to work with TWiki, I have to input this in the Cygwin bash shell:
mount -b -s c:/...
but it doesn't recognize the "-s", I already fixed the problem with -b, it changed the syntax and now is "mount -o binary" for the "mount -b"part. But now it says unknown option with the "-s" anyone?? help?? or what does the -s mean, so I can look it up :S
I would suggest that you don't use cygwin. Strawberry perl and native GnuWin32 ports of zip/rcs etc work better.

Resources