User:Eleanorg/Thematic1.1/Command Syntax 2

From XPUB & Lens-Based wiki

PIPES, ALIASES, SHELL SCRIPTS

see commandlinefu.com for reference & recipes Do a basic shell scripting tutorial as homework

grep Searches for a specific phrase - you can pipe ls to it, to only list files with a certain phrase in them

ps Gives a snapshot of all processes running - you can pipe it to less to make it more searchable (eg, type / to search for a keyword)

ps aux | grep -re gedit - 'ps' with option aux to show all running processes; piped to grep and searching for the word 'gedit', using -re option for a recursive search. Use this to search for programs that seem to have crashed, etc.

alias Lets you 'save' commands with a shortcut. To save the above command with shortcut 'pgrep', type: alias pgrep="ps aux | grep -re gedit" (Only saved for the current session)

To replace 'gedit' with a variable, save an alias with $1 instead of a specific search term: alias pgrep="ps aux | grep -re $1" To use it, type: alias helloworld or alias w00t etc

To save aliases for more than one session, put them in the file .bashrc (in home directory).


SHELL SCRIPTS

Your system also uses shell scripts when it boots the machine, etc. While you use Bash, a user-friendly shell, your system uses one called / You can write a script to be executed by any shell you like, but / is the system's favourite - it's fast because it doesn't have user-oriented bells & whistles.


// Good practice groundwork

- Make a folder in home called 'bin' where you store them; tell bash.rc (Bash 'bible' in home dir) to interpret paths as starting from home directory.

Add this line anywhere in the .bashrc file: PATH=$PATH:$HOME/bin

(Note - this file is itself a shell script.)


- File extension doesn't matter (but use .sh to make it easier for you to find them). What matters is the first line in the file, which tells computer to interpret the following lines as a shell script. The first line should be:

  1. !/bin/sh

This tells the computer to push the script to /sh - this ensures the script will go to a generic posix shell (the original Unix shell standard, known as / or sh), so you can share your programs with ppl who may be using other shells (eg zsh).

- Try to use only 'pure' sh syntax, rather than Bash or Zsh specific syntax ('Bashims').

- Shell scripts use variables, indicated with $ as in php. For example, try typing 'echo $USER' to see your username printed to the screen. Note: all scripts are case-sensitive.

Making programs executable

- Change the 'mode' of the file from the default of non-executable to executable by typing this in the terminal:

chmod +x helloyou.sh

This means you can now run the program simply by typing ./helloyou.sh (from any directory) rather than having to type 'sh helloyou.sh'. You can see if a program is executable by looking at the folder contents in the full view (ls -l), which will show the file permissions. Your shell script should have an 'x' next to each user role, meaning it can be executed. Voila!

SED

sed is a program for transforming text.

getopts


dpkg-reconfigure tzdata - (as root) to change the timezone

MANUALLY MOUNTING USBS (usually as root, unless daily user has permissions to mount/unmount) mount - manually mount an external device umount usb - manually unmount USB

CUSTOM BOOTING change what happens after booting- controlled by file /etc/rc.local You can automate, for example, booting straight into a fullscreen browser, etc. Useful for presentations/exhibitions

ping google.com - tests if you're online by 'pinging' any website