Showing posts with label BASH. Show all posts
Showing posts with label BASH. Show all posts

Thursday, August 18, 2016

I/O Direction for Bash

This is a good article on I/O Redirection. I have always been interest to redirect stderr to stdout. So here is it 2>&1


 2>&1
      # Redirects stderr to stdout.
      # Error messages get sent to same place as standard output.
        >>filename 2>&1
            bad_command >>filename 2>&1
            # Appends both stdout and stderr to the file "filename" ...
        2>&1 | [command(s)]
            bad_command 2>&1 | awk '{print $5}'   # found
            # Sends stderr through a pipe.
            # |& was added to Bash 4 as an abbreviation for 2>&1 |.

Tuesday, April 28, 2015

File Test Operators

If you writing BASH, you may want to look at the File Test Operators written by Advanced Bash-Scripting Guide:

Thursday, December 25, 2014

cannot change directory to /home/user1. Permission denied on NFS mount

If you encountered the "cannot change directory to /home/user1. Permission denied on NFS mount" when you do a su --login user1. Do check the base directory permission. If the owner and group /home is root.root, do remember to chmod 755

# ls -ld /home
drwx------ 7 root root 8192 Dec 22 15:13 home

Change the permission to
# chmod 755 /home
drwxr-xr-x 7 root root 8192 Dec 22 15:13 home

Monday, December 8, 2014

Changing local and group ownership

I usually use the following commands to change ownership of a file or directory

# chown username.usergroups myfile.

But sometimes the users name contain a fullstop in the username. In that case, the following
# chown user.name:usergroups myfile

Thursday, October 30, 2014

killing all the processes belonging to a single user

IF you need to kill all the processes belonging to a user, you may want to consider this command which

# pkill -u user

Alternatively, you can log on as the user whom you wish to eliminate his/her jobs, you can use the command. Remember to logon as the person and not as root or you will kill your processes
$ kill -9 -l

Monday, July 7, 2014

Examine shell and environment variables using set command

Set Command is different from the printenv environment is that the Set Command display both the shell and environment environment. One interesting feature is that it display the output in alphabet order. For more information on printenv. See Examine the Linux Environment using printenv

$ set | less

AMBERHOME=/usr/local/amber10
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=157
CVS_RSH=ssh
DIRSTACK=()
EUID=500
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/me/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/me
HOSTNAME=headnode.cluster.com
HOSTTYPE=x86_64
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LD_LIBRARY_PATH=......
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=46
LOADEDMODULES=
LOGNAME=me
LS_COLORS='no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:'
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/me
MAILCHECK=60
MANPATH=:/opt/xcat/share/man:
MKL_HOME=/opt/intel/mkl/10.2.6.038
....
....
....
....
....

Examine the Linux Environment using printenv

If you wish to examine the environment variables, you can use the command printenv command

$ printenv | less

MODULE_VERSION_STACK=3.2.9
MANPATH=:/opt/xcat/share/man:
HOSTNAME=headnode.cluster.com
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=172.20.4.28 53794 22
SSH_TTY=/dev/pts/3
PERL_BADLANG=0
USER=me
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
LD_LIBRARY_PATH=......................................
XCATROOT=/opt/xcat
MODULE_VERSION=3.2.9
MAIL=/var/spool/mail/me
PATH=...........................
INPUTRC=/etc/inputrc
PWD=/home/me
LANG=en_US.UTF-8
MODULEPATH=/usr/local/Modules/contents
LOADEDMODULES=
MKL_HOME=/opt/intel/mkl/10.2.6.038
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SHLVL=1
HOME=/home/me

PBS_DEFAULT=headnode.cluster.com
LOGNAME=me
CVS_RSH=ssh
SSH_CONNECTION=xxx.xxx.xxx.xxx 22
MPI_HOME=/usr/local/mpi/intel
MODULESHOME=/usr/local/Modules/3.2.9
LESSOPEN=|/usr/bin/lesspipe.sh %s
AMBERHOME=/usr/local/amber10
G_BROKEN_FILENAMES=1
module=() {  eval `/usr/local/Modules/$MODULE_VERSION/bin/modulecmd bash $*`
}
_=/usr/bin/printenv

Monday, March 3, 2014

Using Ctrl-R to search for terminal history

Press Ctrl and R together and the linux terminal will give  (reverse-i-search)`': Type a search word inside and the command will try to find the most recent command match

For example, If I input 'ssh' (without the quotation)
(reverse-i-search)`':`ssh': ssh node-c00


For more information
  1. Ctrl+R to search and other terminal history tricks

Monday, January 13, 2014

Using the command “ls” commonly used options and arguments

ls has some interesting format which is very useful. Here are some arguments which you can use. For example, I like
$ ls - ltF

-a --all All files include those with .
-l Display results in long format
-r --reverse Reverse order while sorting
-S Sort results by file size
-t Sort by modification time.
-F --classify Append an indicator character to the end of each listed name
References:
  1. The Linux Command Line (by No Starch Press)

Saturday, January 11, 2014

Encountering bash: ELF: command not found

I encounter one of the user having this issue "ELF: command not found" There is a good writeup and explanation on this phenomenon "bash: ELF: command not found" by Solution Blog

Monday, November 19, 2012

Timestamp for BASH history

If you run to "record" a time-stamp history on your BASH command so that you can do a better tracing of what was run at certain time. It is quite easy to implement.

$ echo 'export HISTTIMEFORMAT="%h/%d - %H:%M:%S "' >> ~/.bashrc

$ source .bashrc

$ history

  ....
  ....
  997  Nov/20 - 13:46:11 vim .bashrc
  998  Nov/20 - 13:46:17 source .bashrc
  999  Nov/20 - 13:46:26 ls
 1000  Nov/20 - 13:46:36 exit
 1001  Nov/20 - 13:46:50 ls
 1002  Nov/20 - 13:46:54 ssh server-c00
 1003  Nov/20 - 13:47:15 ls
 1004  Nov/20 - 13:47:19 ls -al
 1005  Nov/20 - 13:47:27 history
 1006  Nov/20 - 13:56:58 history
 1007  Nov/20 - 13:57:02 history|more
 1008  Nov/20 - 13:58:47 history|more

Monday, January 23, 2012

Using type to check how each command is interpreted

If you are hoping to check how each command is interpreted, then use the BASH BuiltIn command TYPE

To print all the places that contain an executable name you are looking for, use "type -a", you will bring out the alias and the path of the icc. In this case, there is no aliase so the alias did not surface up in the command line
$ type -a icc
icc is /opt/intel/composerxe-2011.2.137/bin/intel64/icc
To check the type of file the executable is categorised ie alias, keyword, function, builtin, file,you can use the command
$ type -t icc
file

For more usage of type, see the man page.

Monday, July 4, 2011

bash: scp: command not found

I've receive this error when I was trying to scp to a remote machine using root account. I've got this error.
bash: scp: command not found
lost connection

This error is similar to the
  1. Resolution for bash orted command not found and
  2. Unable to source path during non-interactive ssh session
If I interactive directly with the remote machine like ssh into the machine using root, I can enter into the machine. But not with scp.

I'm assuming you have in your /etc/ssh/sshd_config already allows
PermitRootlogin yes

Step 1: To solve this issue, find out where is your scp command resides in your remote computer
# which scp

Step 2: Put the path into your root .bashrc 
export PATH=/usr/bin:{$PATH}

Saturday, June 25, 2011

Running multiple commands

1. Executing Commands in Sequence (Independently)
(Put semi-colon between commands)

# ./configure; make; make install
*The subsequent commands will continue to execute independently whether the prior commands fail or not. This will have impact on subsequent commands
2. Executing Commands in Sequence (Interdependent)
(Put && operator in between commands)
# ./configure && make && make install
* The && is a logical operator. In other words, if the prior commands fail, the subsequent commands will fail

3. Executing Commands Either / Or
# ./configure || echo "something not working in configure"
*  If the 1st command fails, execute the 2nd commands

Saturday, June 18, 2011

Detaching a process in the foreground from the shell

If you forget to use Screen Session to allow you to terminate your shell session without terminating the job that have been running in the foreground, and it is too late to do a nohup command, you may want to consider these alternatives.

You can use the shell command called disown that detach the process from theshell that started it. Here's how to do it

Step 1:
Use Ctrl + Z to suspend the running program

Step 2:
Type bg to restart it running in the background
# bg

Step 3: Get a list of jobs running in your shell
# jobs

Step 4: Detach it from the shell
# jobs %1
(Remember to put the % before the job number which in this example is 1)

Tuesday, May 17, 2011

Bash debugging

To help you with debugging your BASH scripts, you can use set this option which will print command traces before executing command

# set -o xtrace

Try it and you will love it.

For further notes, do read the
  1. Debugging Bash scripts by Bash Guide for Beginners

Saturday, April 23, 2011

Understanding the difference between .bash_profile, .bash_logout and .bashrc

Important Notes on bash file
  1. These files in your home directories have special meaning to bash, providing a way to setup the account environment when you log in and when you invoke another bash command
  2. If the files are not found in your home directory, you are using the default system on /etc/profile
  3. Most important is the .bash_profile which is read and the command in it is executed by bash every time you logged on to the system.
  4. .bashrc is invoked when you start a new shell or by typing bash on the command line.
  5. If you just need to have the same command run regardless of  a subshell or a login shell, you can easily source .bashrc and hence execute .bashrc from within .bash_profile
  6. .bash_logout is read and executed every time a login shell exits.

Monday, June 29, 2009

Understanding Bash (Part 2)

I like to customise my BASH prompt to be a bit more colourful and informative. You can also do it. But before that you must understand some basis syntax. There are more to the options below


Selected Options for Bash Prompt:
\h (Hostname)
\u (Username of the current user)
\w (current working directory, with the top-level directory as ~)
\W (base name of the current working directory, with top-level directory as ~)


How do we get BASH to look like below under CentOS
[root@test ~/scratch/tmp]$
# vim .bashrc
# export PS1='\e[1;33m[\u@\h \w]\$ \e[m'

\e[a;bm \e[m
(marks the start of the character the colour apply to.)
(a;bm is the color code)
(a can have a value of both 0 and 1. 1 will be the lighter colour of the 0)


For Color Codes, see Changing Colour for ls on BASH Blog

Saturday, June 27, 2009

Understanding Bash (Part 1)

When you logged on there are 4 files that are associated with BASH

  • .bash_history (list of previously entered commands)
  • .bash_logout (run a script when Bash exit)
  • .bashrc (basic settings for prompt actions)
  • .bash_profile (additional configuration. If .bash_profile is not present, the user account will use /etc/profile)

Adding a Directory to a Path
To add a directory to a path, edit the .bash_profile
$ vim .bash_profile

------------------------------
PATH=$PATH:/usr/local/bin
export PATH
------------------------------

Putting Aliases
One of the appropriate place to put aliases is in .bashrc