Unix
Unix
Editors, Scripting, Etc.
Linux/Unix Editors
There are several command line programs we can use in Linux
The ones we will focus on this term are:
Text Editors:
VI
Emacs
Compilers
gcc
g++
Debugger
gdb
Options
VI and VIM
vim is an offshoot and more advanced version of vi
general.asu.edu is set up that if you run vi, you get vim instead
vim is designed around a “navigate then edit” workflow
You change modes from navigation to editing and back when you are working on a file
This is very different from what you might be used to
EMACS
Editing MACroS
Emacs is more of a traditional text editor where you are always in both navigation and edit mode at the same time
Emacs is highly configurable and offers more features
Which one is right for you?
The one that you can grock …
Both editors are going to be nothing like you’ve worked with
There is NO GUI
Emacs as a pseudo ASCII GUI, but honestly it can be more confusing then helpful at times
until you really get used to it
Both are a bit “arcane” and rely heavily on memorizing keyboard shortcuts
Which on is right for me?
I’m an EMACS guy
EMACS is highly configurable
If you set it up correctly you can have in software consoles so you can edit your code and compile/run it in the same place
It can have syntax highlighting as well
You can also split your view into multiple windows and load multiple files, terminals, etc.
VIM
Basics
VIM has two modes – navigation/command mode and insert mode
Successful use of VIM requires use of both modes
In navigation mode you navigate through your document:
Arrow keys move the cursor
H J K L also move the cursor if you don’t want to leave homerow
To enter insert mode you press the i-key
Insert mode allows for you to edit text
You can still navigate while editing text, but you don’t have access to the same features
To leave insert mode hit ESC
Saving your file
In navigation mode the ‘:’ will give you access to other commands
To save your file you type : then w to “write” the file
To save and quit it’s :wq
To quit without saving it’s :q!
Navigation/Command Mode
x
delete a character
dw
delete from this point to the beginning of the next word
space delimeted
d?w
deletes ? words – space delimited
dd
deletes a whole line
?dd
deletes ? lines
Cut/Copy/Paste
The previous slide showed dw and dd
These are stated as “delete” … more accurately these are “cut”
When you dw or dd you are cutting and you can paste it elsewhere
p
put previously cut text after the cursor
y
copy (yank) text for p to put
NOTE this copies a full line of text …
Double note – if you screen wrap, it’s still the same line of text
Navigation/Command Mode
u
undo last command
U
undo whole line
CTRL-R
redo
Navigation/Command Mode
w
jump to the beginning of the next word
e
jump to the end of the next word
?w / ?e
jump to the beginning/end of next ? words
0
jump to beginning of line
?
opens a special find prompt for forward search
highlights all instances of a pattern you provide
will also move the cursor
/
opens a special find prompt for backward search
highlights all instances of a pattern you provide
will also move the cursor
Visual Mode
For more precise copy and pasting you can enter visual mode
v
enter visual mode
esc to exit
y to yank/copy it
Visual mode begins from where you cursor currently resides
You can use your navigation keys to highlight what you want to work with
Anything highlighted can be copied with y
Yanked data can be pasted with p
That’s enough
So that’s enough to do most basic text editing in VIM
It’s a bit odd and takes some getting used to
“power typers” tend to prefer VIM since they don’t have to ever remove their hands from their keyboard to do everything
Until you master it, I recommend keeping a “cheat sheet” handy
There’s tons of them online
https://www.fprintf.net/vimCheatSheet.html
EMACS
I’ll try not to be biased
EMACS
First things first … EMACS has lots of weird keyboard short cuts
A lot of what you will do revolves around hitting some combination of CTRL and several keystrokes afterwards
CTRL-X is a starting point for many things in emacs
Unlike VIM – EMACS doesn’t have two different modes, you’re always “on”
EMACS
First thing you’ll probably notice is that EMACS looks a little more like editors you are already used to
There’s a friendly menu bar across the top … which isn’t directly accessible via mouse
We can access the menu system through F10
This text based menu system takes some getting used to
It does many of the things you would expect it to:
File – deals with files, buffers and frames
Edit – copy, paste, etc
Buffers – allows you to manage all open files/buffers
etc.
But we’re more interested in the keys!
emacs
So, we’re going to get to know the CTRL key very well…
In most emacs documentation you’ll see the prefix C- which means CTRL key and other key
To get help in emacs you type C-h
You can access an in emacs tutorial of emacs with
C-h t
That is CTRL-H followed by T
The pretty much universal “oh crud stop this” sequence is ESC ESC ESC
File Commands
C-x C-f
Find file – prompts for a file name to load or create
C-x C-s
Save the buffer into the associated filename
C-x C-w
“Save As”
Prompts for a filename to save contents of buffer
Useful Commands
C-x u
undo
ESC x
allows you to manually type in a command if you know it
C-g
stop current command I’m working on!
similar to ESC ESC ESC
C-x C-c
save buffers and exit emacs
Special Movement
Arrow keys will get you where you want to go … but …
C-a
move to beginning of line
emacs (like vim) considers everything the same line until there is a new line
C-e
move to end of line
C-f
C-b
move forward or backward one character
Not really sure the advantages of this
Special Movement
C-n
C-p
Move the cursor to next or previous line
C-v
Move cursor forward by one screen
ESC v (note … escape then v, not ESC-v)
Move cursor back by one screen
ESC f
ESC b
move forward or backward one word
Delete/Cut/Copy/Paste
C-d
delete character under cursor
This is an actual deletion, not a cut
ESC d
delete an entire word
Killing and Yanking
We don’t cut and paste in emacs … we kill things and yank them back from the afterlife!
C-k
kill (cut) entire line
C-y
yank (paste) whatever is in the kill-ring (clipboard)
C-@ (CTRL-SHIFT-2)
Sets a “start” mark for a region highlight for killing
C-w
kill the region
ESC w
copy the region into the kill-ring instead of killing it
still use yank to paste
Searching
C-s
search forward for text
C-r
search backwards
Repeat the key combinations for either to advance to the next result
ESC %
Find and replace
! – replace all
y – replace this instance go to next
n – skip this instance
q – stop
E – edit replacement string
Getting buff with buffers
We can split the screen within emacs to allow us to display and use multiple buffers at the same time
C-x 2
Split the buffer into two horizontal buffers
This can be repeated multiple times
C-x 3
Split the buffer into two Vertical buffers
C-x 0
close this buffer/end this split
C-x o
Switch to another window
C-x k
Close a buffer (but don’t close its window)
4 buffers from 0
C-x 2 – split into two horizontal
cursor remains in top buffer
C-x 3 – split top into two verticals
There are now 3 windows
C-x o
C-x o
change cursor position to bottom buffer
C-x 3 – split bottom into two verticals
You now have four windows to work in!
You can load different files/etc. into each individually
Other fun stuff …
There’s a TON of stuff you can do with emacs
You can configure it to work with terminals
You can build in scripting interpreters
You can get syntax highlighting, etc.
You can switch each buffer’s mode to reflect which language you are working in
Emacs can be customized by scripting in LISP
Like VIM – I really recommend having a cheat sheet until you get used to emacs
Getting Active
Activity
I’ve committed a terrible terrible sin …
May the Rock-gods forgive my trespass …
For I have sorted and removed duplicate lyrics from Queen’s Bohemian Rhapsody
Then I added lines from Radio Gaga
I want you to use either VIM or Emacs to put this horror right!
Use your skills to cut, copy and paste your way back to re-assembling the lyrics of Bohemian Rhapsody
Upload your fixed file to the dropbox when you’re done
This counts in the attendance grade portion
Scripting Unix
Hacking BASH
Section thanks to …
http://linuxcommand.org/
There’s a ton of material … we’re only going to hit the high points …
Check this site out for MORE
Non-script, scripts
Aliasing
You can use the alias command to create aliased keywords to perform simple tasks
alias l=‘ls –l’
alias today=‘date +“%A, %B %-d, %Y”’
You can remove an alias with
unalias
unalias today
Pseudo-scripting, scripts
You can also create functions within the shell to perform actions
functions allow us to create simple sub-scripts
They are usually created within an existing script or settings file
If we had access to our profile we could add the function:
function today {
echo “Today’s date is:”
date +”%A, %B %-d, %Y”
}
Basics … So … this is a little obvious …
So to create a bash script … all you really do is create a text file with your favorite editor and then chmod +x the file
The file will now be considered executable
You can put a series of shell commands into the file to execute in sequence
Hello World
Often the best place to start any language is with hello world
So here we go…
Create a file named hello.sh
Add the lines:
#!/bin/bash
Echo “Hello World”
To run this code we need to make it executable
chmod +x hello.sh
Run the code with ./hello.sh
That’s our basic pipeline
Let’s do another one
Open your favorite text editor
Create a file “myScript.sh”
Type in these commands
Save the file
try to run it:
./myScript.sh
chmod +x myScript.sh
Now run it
./myScript.sh
#!/bin/bash
#myScript.sh
pwd pwd
ls
ls -a
ls -al
cd ..
pwd pwd
ls
ls -al
Shell Script
In doing that we created a simple shell script
We can use any shell command that we need to
At face value this is pretty good for automating frequently done tasks
For instance
compiling code
removing backup/auto generated files
deleting a directory structure
creating a directory structure
etc.
Shell Scripting
Basic output
The echo command is used for basic output
echo “Hello world!”
echo ‘Hello world!’
“” and ‘’ are largely equivalent with a few difference
“” allows for substitutions (later)
‘’ does not
both suppress wildcards
\ is the escape character
Variables
We can also expand our scripting to use some more commands
Variables
Variables are created by simply giving an identifier and assigning a value
Variables are weakly typed and can contain any information
Must start with a letter
No Spaces
Underscore is allowed
No punctuation
No keywords
Nuances of BASH #1
So … you’re going to find out in a hurry that BASH scripting is not like programming other things
You are using BASH commands in the script, which means you are tied to their syntax and how they want to be used
So one nuance we have with variables is the syntax:
Do NOT put spaces around the =
myVar = 10 will cause an error/problem
myVar=10 is proper syntax
Shell Scripting
Substitution
The $ character is used to cause a substitution to happen
This means “how we get a value out of a variable or something else”
Such as wanting to output the contents of a variable as part of a string
a=10
b=20
c=$(( $a + $b ))
echo “the total is: $c”
Shell scripting
Invoking commands:
To invoke a command and get its result to store in a variable we have to use a special notation:
$()
myPath=$(pwd)
This will allow you to run any shell command and possibly capture the output
Note – unless you work the syntax correctly, this can/will dump all the output into one variable
files=$(ls -1)
Results in the entire ls -1 concatenated into the variable files
Constants
Constants can be created by prefixing the variable with readonly
readonly MYPATH=$(pwd)
Confusion $() vs ${}
$() is a synonym for running a command and getting its output
When we use $() we are saying “run this command and get it’s output” so we don’t use it to extract variable data
${} on the other hand, is a disambiguation mechanism, but otherwise similar to $ by itself
It does empower us to use variables in concatenations
$var gets
Input
The basic input command is read and it reads from stdin
read
echo -n “Enter some text > ”
read text
echo “You entered: $text”
Doing Math
The shell can only do INTEGER math!!
Math is performed via a parentheses notation, brackets or let:
$((
For complex math you have to use the built-in calculator program bc
Now bc is pretty complex and even allows its own scripting as well … so yeah … look that up later
Schell Scripting
Functions can exist in scripts
Use the function keyword and {} to define the body
Parameter passing is via position and not explicitly done through syntax like other languages
Parameters are stored in $# order based on position
First parameter – $1
Second – $2
… etc.
function foo
{
echo $1
}
Functions – Locals and returns
The local keyword makes a variable local to a function
local result=0
Returning a value is complicated
You can use a substitution to capture any stdout results
This is probably the ‘best’/easiest option
function myfunc()
{
local myresult=’some value’
echo “$myresult”
}
result=$(myfunc) # or result=`myfunc`
echo $result
Functions – returning
We can also return by feeding back on a passed in parameter:
function myfunc()
{
local __resultvar=$1
local myresult=’some value’
eval $__resultvar=”‘$myresult'”
}
myfunc result
echo $result
We capture the variable name that the user passes in into __resultvar
Then we use eval and substitution to force the value to store into the variable name that was passed in …
Functions – returning
There is a return keyword
But it doesn’t do what we want it to do
The return keyword ends function execution, but the value returned is simply a code to say if the function was successful or not
Granted we can exploit that somewhat … but not completely
#!/bin/bash
# Setting a return status for a function
print_something () {
echo Hello $1
return 5
}
print_something Mars
print_something Jupiter
echo The previous function has a return value of $?
./return_status_example.sh
Hello Mars
Hello Jupiter
The previous function as a return value of 5
Shell Scripting
Conditionals
The if statement exists in shell scripts as well
This is more like a BASIC if where you define a block through an if and an end-if
Syntax:
if condition; then
do stuff
fi #endif
Shell Scripting
Else
if condition; then
do stuff
else
do other stuff
fi #endif
Else if
if condition; then
do stuff
elif condition; then
different stuff
else
do other stuff
fi #endif
Conditionals – test
test is used to perform T/F decisions and is usually used with if statements
syntax:
test
[
Partial list of Tests we can do
Expression Description
-d file True if file is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-L file True if file is a symbolic link.
-r file True if file is a file readable by you.
-w file True if file is a file writable by you.
-x file True if file is a file executable by you.
file1 -nt file2 True if file1 is newer than (according to modification time) file2
file1 -ot file2 True if file1 is older than file2
-z string True if string is empty.
-n string True if string is not empty.
string1 = string2 True if string1 equals string2.
string1 != string2 True if string1 does not equal string2.
Loops
The loop has a nearly identical syntax:
while test; do
commands
done #end while
There is a variant until … do … done
This will loop until a condition is met
Basically it loops on false instead of true
Arrays
Arrays are a bit complicate in BASH, but can be very useful for parsing through command results
Creating a basic array:
myArray=( 1 2 3 4 5 )
Creates and array with 5 things in it … notice the lack of commas
Get the size of the array:
size=${#myArray[@]}
That’s some syntax …
Arrays
We can assign into array indexes with []
myArray[0]=500
We can iterate through with a loop
index=0
while [ $index –lt 10 ]; do
myArray[$index]=$index
done
index=0
while [ $index –lt 10]; do
echo ${myArray[${i}]}
/docProps/thumbnail.jpeg