Categories
Software Development

Why doesn’t bash script recognize aliases

Here I won’t be doing much explaining, just listing links so people can read about this befuddling issue.

It often boggles my mind how differently you must write shell scripts vs the command line commands. It is often very inconsistent, I hate inconsistencies.

Basically Aliases within Linux shell are not recognized without some fancy hacky code, WOOHOO. So you will lose your mind if you are trying to set and use aliases in shell scripts.

You can set aliases all day long, but your scripts wont use them.

Yes you read that correctly. You can set aliases in your script, even right before you want to use it and Linux is like GTFO, I have no idea what that is. It even fails without a notice/error most times. What you can do is set a normal variable and use it in place of an alias though.

Say you had a script named do-this-thing.sh  and it was located deep in a directory like /etc/directory/directory/directory/directory/do-echo “$yarnBin” > /etc/profile.d/server-alias.shthis-thing.sh
You could do the following in BASH


not_alias=/etc/directory/directory/directory/directory/do-this-thing.shalias not working inside bash shell script

bash not_alias

The above would execute the do-this-thing.sh file. You can also permanently set aliases in your Shell script. This is handy even if you can’t use the aliases in your script directly, you can use them in the terminal command line later.

To permanently set aliases alias when not working inside bash shell script place them in your .bashrc file for the user you are logged in   This is usually located in /home/username/.bashrc  or you can put it in the user profile .profile file or other places.
How you do this varies by Linux shell.

Links to more info

Why doesn’t my Bash script recognize aliases?

Alias not working inside bash shell script

Creating permanent executable aliases

How to create permanent Linux Aliases

How to create a permanent Bash alias on Linux/Unix 

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

What does a dollar sign followed by a square bracket $[…] mean in bash?

I saw something similar to this in some code in one of my books
var=$[ $var1 - $var2 ]

I wanted to know what it did and why it was used. I’m a perfectionist with OCD.

Turns out it is deprecated from the BASH language.
Originally $[] was used to do math in Bash scripts to do Math known as arithmetic expansion.

So the new way in BASH is to use the following syntax.

var=$(( $var1 - $var2))

Basically what this syntax does is it allows you to do math more easily. Without the above syntax you have to escape certain characters like >< With the above syntax you can basically do math without escaping plus youcan use post-increment $var++, post-decrement $var– , logical and &&, logical or || bitwise math etc. It really helps you out.

Further links, resources and information

More info on stackexchange Same as the link above

Another good source of info about the (()) syntax as used in if and while statements is found in the book Linux Command Line and Shell Scripting Bible.  starting on page 325 If you don’t own the book I highly suggest it. I’ve found one errata so far and that is what this post is about.

More links and resources to BASH scripting

More links and linux resources

Categories
Software Development Web Development

Linux bash scripting command substitution aka $(command)

Linux has this syntax that looks like so:
$(command)

This is called command substitution. This allows you to get information about the execution of the command instead of having it it directed to STDOUT aka the terminal screen as usual.

That is very useful actually because you can run a command and store the output in a variable and use it anywhere you want later.

A simple example you can easily play with:

DIR_LISTINGS=$(ls -al)
echo $DIR_LISTINGS

This is so simple you don’t even have to add it to a script you can run it straight in your terminal in any directory you user owns.

More information can be found in this excellent book Linux command line and shell scripting bible page 277

More info about command substitution.

Bash manual reference.

Categories
Resources Software Development

Debian Ubuntu Linux debconf resources and information

The debconf programmers tutorial – excellent tutorial on what debconf is and how to use it.

debconf documentation

Using debconf to configure a system – article about using debconf, gives a little more explanation of what it is.

Installing MySQL with debconf – good article

Categories
Software Development Uncategorized

Linux xclip command makes command line life easier

I discovered a new tool today while adding my ssh keys to Github, something called xclip.

The xclip command makes it easy to capture output to the clipboard so you can paste it to another location like into a browser or word file etc.

The github docs above have you use it to copy your RSA key and save it for pushing your repository so you don’t have to supply a user name and password.

xclip -selection clipboard < ~/.ssh/id_ed25519.pub

The above tells xclip to put the contents of id_ed25519.pub into the clipboard. You can then use ctrl + v to paste it anywhere you need it.

Here is more info and examples on how to use xclip

Link to some man page  type info about the options it accepts as arguments.

Another link to similar info about xclip it’s options etc.