Saturday, April 14, 2012

Linux 9 - Working the Command Line

  • Bash
    • Shell environment
    • PATH
    • environment variables
    • history
    • completion
  • commands

The shell that a user uses is defines in /etc/passwd

testuser:x:1001:1001:testuser user:/home/testuser:/bin/bash

The settings for how bash behaves are stored in a couple different places

/etc/profile

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

if [ "$PS1" ]; then
if [ "$BASH" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi

# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi

In red it refers to how to shell would look (with a $ sign or a # sign depending on user)

From this file it reads all the scripts in /etc/profile.d/

.profile

This is a local profile
root@debian:~# cat .profile 
# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi

mesg n
root@debian:~#
Basically it tells to look in ~/.bashrc file for info

.bashrc

There are some aliases, we can do more if we uncomment it. If you wanted to change how your prompt look like or aliases this is where youd do it.

In your home directory it’s hidden so only with ls -a

root@debian:~# cat .bashrc 
# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022

# You may uncomment the following lines if you want `ls' to be colorized:
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'

To tie commands to one another ( && / || / ; )

root@debian:~# ls ; pwd ; uname r
backup.mbr Desktop Downloads Pictures Templates VMWARE
dayoff.zip Documents Music Public Videos
/root
2.6.325amd64
root@debian:~# ls && pwd && uname r
backup.mbr Desktop Downloads Pictures Templates VMWARE
dayoff.zip Documents Music Public Videos
/root
2.6.325amd64
root@debian:~# ls || pwd || uname r
backup.mbr Desktop Downloads Pictures Templates VMWARE
dayoff.zip Documents Music Public Videos
root@debian:~#
; irrespective of EXIT_CODE
&& if EXIT_CODE is 0 run next command
|| OR

 

To make files executable scripts ( chmod +x )

root@debian:~# blackbox
bash: blackbox: command not found
root@debian:~# cat > blackbox --> output to the file blackbox
echo HELLO --> simple command
hit CTRL+D to exit when done --> finished
root@debian:~# chmod +x blackbox --> set executable bit
root@debian:~# ls l blackbox --> check exec bit is on
rwxrxrx 1 root root 11 Feb 24 14:37 blackbox
root@debian:~# ./blackbox --> run executable
Hello
root@debian:~# cat blackbox --> cat the contents
echo Hello

Linux only looks for executable files in your path environment variable set in your shellscript config file

root@debian:~# env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

it will run executable files ONLY if it finds them in PATH

root@debian:~# pwd
/root
root@debian:~# ./blackbox

this will RUN because of the ./ which  means means current directory

Instead of typing the whole path of /root/blackbox we use ./ to specifiy current working directory

To add environment variable ( export )

root@debian:~# ABC=123
root@debian:~# export ABC --> on other systems SET is similar to EXPORT
root@debian:~# env
ABC=123
root@debian:~# echo $ABC
123
root@debian:~#

To add a PATH to the variable PATH

root@debian:~# PATH=$PATH:/home/user/bin
- PATH equals the variable PATH plus :/home/user/bin
root@debian:~# export PATH
- export PATH means write the variable PATH

To add a permanent PATH entry

Change the .bashrc in your home dir

To get rid of the bogus variable ABC

unset ABC

History

History command is : history
–by pressing the up arrow it will walk you thrgouh the history
–by using ! (exclamation command) and the number of the history entry it will run
–by using TAB you can autocomplete

uname ( print system information )

uname
–uname → tells you the os you are running
–Linux
–uname -a→all available information
–Linux debian 2.6.325amd64 #1 SMP Mon Jan 16 16:22:28 UTC 2012 x86_64 GNU/Linux

man


–man history→ docs
–man -k → search all the man pages for a keyword
–man -k file→ similar to apropo

exec ls ( execute a file )


It runs it in a new shell without honoring the user's settings
run command and exit the shell
a lot of time a shell script will call something with exec, you don't want the bash shell to hang around

 


No comments:

Post a Comment