Difference between revisions of "Shells"

From Mu2eWiki
Jump to navigation Jump to search
Line 28: Line 28:
 
*[http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html BASH Programming - Introduction HOW-TO], a shorter but less complete introduction.
 
*[http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html BASH Programming - Introduction HOW-TO], a shorter but less complete introduction.
 
*[http://www.tldp.org/LDP/abs/html/index.html Advanced Bash Scripting Guide], starts at the beginning put goes more quickly to advanced ideas.
 
*[http://www.tldp.org/LDP/abs/html/index.html Advanced Bash Scripting Guide], starts at the beginning put goes more quickly to advanced ideas.
 +
 +
==Beginner Cheat Sheet==
 +
 +
===Files and directories===
 +
 +
<pre>
 +
cp <x> <y> % copy <x> to <y>
 +
mv <x> <y> % move <x> to <y>
 +
mkdir <x> % makes new directory <x>
 +
pwd %shows current directory
 +
cd <x> % goes into folder <x>
 +
rm <x> % remove file <x>
 +
ln -s <x> <y> % makes a soft link between real file <x> and local pointer <y>
 +
ls % lists documents in current directory
 +
cat <file> % list the whole file
 +
more <file> % types <file> in chunks, <space> goes to next chunk
 +
less <file> % similar to more
 +
head <file> % types the first N lines
 +
tail <file> % types the last N lines
 +
find * % list all of the files
 +
</pre>
 +
 +
 +
===Process management===
 +
 +
<pre>
 +
CTL-Z % pause the current process and return to console
 +
bg % allow the process you just paused to run in background
 +
jobs % shows what you're running
 +
ps % shows processes
 +
 +
<c> > file % output of command c goes to file <file>
 +
<c> >& file % errors from command c goes to file <file>
 +
<c> >> file % appends output of <c> onto end of <file>
 +
<c> >>& file % appends error of <c> onto end of <file>
 +
</pre>
 +
 +
===Help===
 +
 +
<pre>
 +
man <c> % help on command <c>
 +
<c> -h % sometimes has help this way as well
 +
</pre>
 +
 +
===Editing and strings===
 +
 +
<pre>
 +
sed s/<a>/<b>/ <file> >
 +
grep <a> <b> % print out all lines in  <b> which contain string <a>
 +
sort <a> % sort the file a
 +
diff <a> <b> % print out the difference between <a> and <b>
 +
xemacs <a> % edit the file <a>
 +
</pre>
 +
 +
===Environmentals===
 +
 +
<pre>
 +
export <X>=<y> % will make $X refer to <y>
 +
export PATH=<y>:${PATH} %will append <y> to $PATH
 +
</pre>
 +
 +
 +
important environmentals are:
 +
 +
<pre>
 +
$HOST    % this computers's name
 +
$USER    % your username
 +
$HOME % your home area
 +
$PWD             % the current directory
 +
$PATH             % where unix looks for code to execute
 +
$PYTHONPATH % where unix looks for python modules to import
 +
$LD_LIBRARY_PATH % where unix looks for shared libraries
 +
</pre>
 +
 +
You normally want to append to the PATH. Just setting them to <x> wipes out all the other stuff they were already set to.
 +
 +
  
 
[[Category:Computing]]
 
[[Category:Computing]]
 
[[Category:Infrastructure]]
 
[[Category:Infrastructure]]

Revision as of 14:13, 6 April 2018

We use the bash linux shell exclusively for the interactive command line and shell scripting. In general, this is the most powerful shell and the professional's choice.

Setup scripts

  • ~/.bash_profile is executed once when you login.
    • When you first get a mu2e account, you will be given an example which should only contain the following modifications
    • It should source a "products/.../setup.sh" command (see details [[1]] which defines the PRODUCTS environmental, which allows you to access UPS products
    • source ~/.bashrc
  • ~/.bashrc is executed for interactive non-login shells
    • When you first get a mu2e account, you will be given an example which would usually be modified
    • set the prompt
    • set any aliases
    • a good style is to leave the example untouched except to source a separate file (such as ~/.mybashrc) with your customizations. This always makes it clear what you are adding.
  • we do not use ".login" or ".profile" which are variants for .bash_profile
  • we do not recommend setting up UPS packages in the bash login scripts since this can get confusing
  • the lab systems will also usually execute /etc/profile.d/*.sh and /etc/bashrc

Standards

  • scripts that intended to be executed by the average user at the command line, should be added to the user's path and can be named with no extension, so it looks built-in.
  • scripts that are used in complex packages, or for special purposes, such as in monitoring, should have extension ".sh"
  • scripts intended to be sourced should have the extension ".sh".

References

The Linux Documentation Project publishes a number of guides that are available online. We suggest:

Beginner Cheat Sheet

Files and directories

cp <x> <y> 	% copy <x> to <y>
mv <x> <y>	% move <x> to <y>
mkdir <x> 	% makes new directory <x>
pwd 		%shows current directory
cd <x>		% goes into folder <x>
rm <x> 		% remove file <x>
ln -s <x> <y>	% makes a soft link between real file <x> and local pointer <y>
ls 		% lists documents in current directory
cat <file> 	% list the whole file
more <file> 	% types <file> in chunks, <space> goes to next chunk
less <file>	% similar to more
head <file> 	% types the first N lines
tail <file>	% types the last N lines
find *		% list all of the files


Process management

CTL-Z	% pause the current process and return to console
bg 	% allow the process you just paused to run in background
jobs 	% shows what you're running
ps 	% shows processes 

<c> > file	% output of command c goes to file <file>
<c> >& file	% errors from command c goes to file <file>
<c> >> file	% appends output of <c> onto end of <file>
<c> >>& file	% appends error of <c> onto end of <file>

Help

man <c>	% help on command <c>
<c> -h	% sometimes has help this way as well

Editing and strings

sed s/<a>/<b>/ <file> > 
grep <a> <b>	% print out all lines in  <b> which contain string <a>
sort <a>	% sort the file a
diff <a> <b>	% print out the difference between <a> and <b>
xemacs <a>	% edit the file <a>

Environmentals

export <X>=<y> 		% will make $X refer to <y>
export PATH=<y>:${PATH} 	%will append <y> to $PATH


important environmentals are:

$HOST     % this computers's name
$USER    % your username
$HOME 		% your home area
$PWD 	            	% the current directory
$PATH 	            	% where unix looks for code to execute
$PYTHONPATH 	% where unix looks for python modules to import
$LD_LIBRARY_PATH	% where unix looks for shared libraries

You normally want to append to the PATH. Just setting them to <x> wipes out all the other stuff they were already set to.