Categories
Software Development Web Development

How to switch users in Linux Bash Shell script and execute multiple commands as different user

If you search you will find different answers to this. You can do this in multiple ways, here I will talk about 2 ways, single command and multiple commands.

First the idea is to switch from say root user to a named user you created or was created for you on your Linux server to run commands as not the root user. The reason you want to do this is so that everything isn’t owned by the root user. Or you are installing something like PHP Composer which barfs on you if you run it as root user.

You will see some saying to use su others saying to use sudo (some bs options etc.) You will also see really wrong answers on Stack. I have no idea why you would use sudo over su, you can google that. But I do know that su switches users. Here is an article goes into more detail of su vs sudo and when you use both.

Single command syntax

So the first way is to run a single command directly inline. If you are the root user you simply use su The syntax to do so is as follows:


su - username "commandToExecute [command options and arguments]"

It has been my experience that the Double ” Quotes are required or else the shell gets confused. You may be able to use single quotes if you don’t use any variables within the quotes.

Multiple commands syntax

To more easily issue multiple commands or long commands you need to use Linux heredoc syntax.
Heredoc uses <


su - $username <<SHT
     cd $serverDir
     php $composerFile install
SHT

Like I said you can use any Delimiter you want. It is tradition to use all caps for the word, it makes it easier to spot. The ending word (EOF here) has to have no spaces or words before it. You can list any number of commands within that syntax and all will be executed by the user.

NOTE: After the ending EOF the shell returns the user to whatever user you were/are logged in as before the lines of code. If you are logged in as root, you are returned to root. Also when you issue the su command you are moved out of the directory you are in. That is why I used cd to move back to the directory I needed to be in.

More links

More info about changing users on stack here.

Here is a link to heredoc syntax explanation and examples

More information and examples about heredoc in bash

Bash how write large amounts of text to a file

Categories
Resources Web Development

Linux su command not working, does nothing but show $ prompt

So you found the magic sauce did ya?  So you created a user with useradd or adduser and you try to switch over to that user in a terminal, probably logged in through ssh as root right.

You are trying to use the su – username command but all you get is a $. And not the good kind. The kind that no matter what you type all you get is another line with $ on it. This is a feature by the way so you can’t see files that don’t belong to the user…

Right now you are probably like

Wait. WTF is even happening?

As far as I can tell, if you are using Debian or Ubuntu, useradd/adduser defaults the users shell to /bin/sh but the skeleton files located in /etc/skel are all configured for bash.  I have no idea how the system gets the defaults, but it does no good to have your users default shell not pointing to bash.

How to fix this?

To fix it you need to change the users default shell to bash. Bash is usually located in /bin/bash or /usr/bin/bash For me it was /bin/bash. To change it you use usermod command like so


usermod -s /bin/bash username

That will change the shell your user gets when you type su – username. Now since Ubuntu/Debian and maybe other distros contain the configuration in the users .bashrc and .profile files everything will work as expected. When you switch to the user with the above command you are taken to their home directory.

Found more details!

More info

Ok I found more info while digging into the so called useradd docs.

-s–shell SHELLThe name of the user’s login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default.

So that is where Linux gets the default value for the user shell and apparently you can use the -s option when creating the user to specify the bash as the shell.

Digging deeper into the mystery sauce I find in my Mastering Ubuntu Server book ( awesome book ) the reason why. It states if you use adduser then their default shell is /bin/bash and if you use useradd (which I used) it defaults to /bin/sh

Links

how to change the default shell of an user in linux? – more info about usermod and changing the users default shell and how to figure out what shells you have installed and their locations.

Categories
Uncategorized

Bash how write large amounts of text to a file

I searched and tried for days to find the answer to this. All I wanted to do was be able to basically create a file and write text to it exactly as I had entered it in a shell script. Every suggestion on the internet was fubar.

Note : to run these commands, put them in a folder like test.sh and use chmod 755 test.sh to make it executable then type bash test.sh or sh test.sh or /.test.sh

I wanted to put something like this in a file from within a shell script.



Line one text
   Line two text
   line three text
   lets talk about some text

I tried everything. I googled for days and I finally found this article  where someone had basically the same need as me. Basically the syntax to put a bunch of text into a file from shell script is this.


#!/bin/bash
testFile=/home/akashicseer/tests/test-file.txt
if [ ! -e $testFile ]; then
    touch $testFile
fi
outside_var="Some text from outside hell"

cat <<TEST  >> $testFile
Line one text
   Line two text
   line three text
NEWVAR = values
lets talk about some $NEWVAR
or not
but look at some outside text $outside_var
TEST

This uses <<HEREDOC syntax but it also redirects the input with >>. This is the oddest syntax I have ever seen so I can’t explain why it works. I would expect the redirect to be at the end of the closing TEST, but that doesn’t work. Bash heredocs are the weirdest thing other than if statements I’ve come across. Learning bash has been like traveling back in time to the 70’s or 80’s the syntax is beyond odd.

Also you will notice I tried defining a variable in the heredoc. That doesn’t work. You can copy and paste the code above and see what I mean. You don’t get errors but the variable doesn’t expand. I don’t know if it is supposed to or not. Here is a link to some info about heredoc. However what you can do is define variables outside the heredoc and use them within, see the $outside_var.

If you read the “info about heredoc” link above( in links below too) it shows this alternate syntax which works too, and makes more sense. I honestly don’t know how or why  the above ugly mess works.


#!/bin/bash
testFile=/home/akashicseer/tests/test-file.txt
if [ ! -e $testFile ]; then
    touch $testFile
fi
outside_var="Some text from outside hell"

cat > $testFile <<TEST
Line one text
   Line two text
   line three text

   but look at some outside text $outside_var
TEST

This looks a little better to me than the other version. So there you have it that is how you write lots of text to a file.  I had to search for days to figure this out so I hope this saves at least one person some time.

Version 1 heredoc syntax
Version 2 heredoc syntax

Categories
Resources Software Development

How to make linux shell scripts wait for a command to finish before running another

I am writing this so when other people google how to do it, they have something to find to save them time.

For days I tried to figure out how to make sure a command finished before another was run. I couldn’t find any information anywhere. If you are like me you may be thinking ( or wondering if ) that the shell just zooms through the commands you put in a script file without waiting for each to finish. It seems like this because everything is rushing by so quickly you can’t read it.

For days I was running scripts to install and configure my servers and it kept hanging so bad I couldn’t even ping the server.

I was running the following for example

sudo apt-get update
sudo apt-get dist-upgrade -y
sudo apt-get reboot

And since my server instances were hard freezing right up, I figured it must be zooming right on through causing an error.

Well come to find out after much research and someone on twitter finally confirmed to me that the shell automatically waits for each command to finish before executing the next. The shell doesn’t automatically run all commands encountered at the same time.

Now I must figure out what is actually locking my server instances up.

now you know
Categories
Resources Software Development

Linux commands resources and links

Difference between Ubuntu apt-get autoclean, clean, autoremove commands – it is important to know the difference.

Cleaning up with apt-get – how to clean up with apt clean command

How To Upgrade Ubuntu Using Command Line?

Linux << heredoc syntax info – great link has lots of good linux information

Here is an excellent link to a shell command cheatsheet. This lists the most commonly used shell programs.

An A-Z Index of the Linux command line: bash + utilities. – about the same as the list of shell commands above.20 Shell Scripting Questions for Practice

Writing text to files from shell or shell scripts

Awesome linux software – a list/article about awesome useful linux programs
Understanding Shell Initialization Files and User Profiles in Linux

20 Shell Scripting Questions for Practiceuses opensshLinux visudo command

Linux /etc/profile file information

Understanding a little more about /etc/profile and /etc/bashrc20 Shell Scripting Questions for Practice

Why and How to Edit Your Sudoers File in Linux

LINUX DIRECTORY STRUCTURE:/ETC EXPLAINED

Tutorialspoint Linux tutorials and really good information.20 Shell Scripting Questions for Practice

How to Use Sudo and the Sudoers File – very good article

Ubuntu Environmental variables – Ubuntu Documentation link

How to prompt for input from user in a linux shell script – article 20 Shell Scripting Questions for Practicecovers two ways to prompt users for input in shell scripts.

How to prompt and read user input in a Bash shell script – A good example of a script that prompts users for input and uses it.

Bash read builtin command – this command is built into bash meaning available without installing etc.

Linux while loop explained with examples of alternate syntaxes

How to create a self signed SSL certificate 

20 Shell Scripting Questions for Practice – article showing how to prompt users for input and how to answer prompts.


Categories
Resources Software Development

BASH shell scripting links and resources

Bash scripting manual 5.1

Advanced Bash scripting guide

Bash script cheat sheet

Bash Scripting: Everything you need to know about Bash-shell programming

Respond to prompt in Bash Script – a tutorial on how to respond to prompts in Bash scripts.

How To Write a Bash Script That Answers Interactive Prompts 

MySQL: Run Query from Bash Script or Linux Command Line

How to Use Variables in Shell Scripting

Bash scripts and Mysql – a good article about working with Mysql in bash scripts, how to load SQL etc.

If statement syntax example coverage

More coverage and examples of Bash if/else statement syntax and use

Bash read builtin command used to prompt the user for a response and store it for use in the script

How to prompt for input from user in a linux shell script


How to prompt and read user input in a Bash shell script – more examples and information. Different from the above link.