cs288 scripting basics (1)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

31 Terms

1
New cards

how to declare and call upon a variable

how are vars stored as? what happens if multiple words in the var?

decl: var=”variable” , call with $var .

they’re stored as strings. if multiple words, store in quotes, otherwise there’ll be errors.

2
New cards

what are environment variables and four examples?

how do you find more?

they’re vars created by our system.

examples: HOME</code>,(pathtohomefromroot)<code>HOME</code>, (path to home from root) <code>LOGNAME (username of logged in user), RANDOM</code>(randomnumbetween0and32000),and<code>RANDOM</code> (random num between 0 and 32000), and <code>SECONDS (seconds since sessions tart).

we find more by using env command

3
New cards

variable expansion: length

${#var}

4
New cards

var expansion: slicing a part in the middle

${var:FROMINDEX:LENGTH} , where length includes the character at start index.

5
New cards

using double quotes vs single quotes to store variables

anything in single quote doesn’t allow variable resolution (so if you have a lil $var in there, it’ll print it verbatim instead of treating it like a variable)

6
New cards

variable expansion: slicing from index to the end

${var:2} slices from pos 2 to end.

7
New cards

how to take user input

read var stores it in var

8
New cards

what does shebang do?

tells linux to use the bash interpreter to run the script

9
New cards

what does this do: echo ${goon}2 if goon=”hello”

this would output hello2

10
New cards

which expansion is performed before all other expansions?

brace expansion

11
New cards

what does tilde expansion do? what does the tilde have to have?

replaces the value of ~ (must be UNQUOTED) with home directory pathname.

12
New cards

what are two ways to save values of commands inside variables?

var=`command`

and

var=$(command)

13
New cards

how to evaluate arithmetic expressions?

$((expression))

14
New cards

what are the 5 recognized arithmetic operators

+, -, /, *, %

15
New cards

what characters are a part of filename expansion?

* which matches any string including null str

? which matches any single character

[…] which matches any one of the enclosed characters

16
New cards

format of conditional statements?

if [ expression ]; then
	stmt
elif [ expression ]; then
	stmt
else
	stmt
fi


17
New cards

what are numerical comparison thingers? (hint: 4 “than” comparisons; 2 “equal” comparisons)

-lt, -gt, -le, -ge, -eq, -ne

18
New cards

what are the string comparison thingers? (4)

= if two strings are equal

!- if two strings are not equal

-n evaluate if string length is greater than zero

-z if string length is equal to zero

19
New cards

logical operators and format

  1. if [ expr ] && [ expr ]

  2. if [ expr -a expr2 ]

  3. if [ expr ] || [ expr ]

  4. if [ expr -o expr2 ]


20
New cards

for loop notation

for item in ITEM1 ITEM2 ITEM3
do
	meow
done

or

for item in {1..5}
do
	meow
done


21
New cards

how to use seq for for loops

basically range

for item in $(seq startnum endnum)
# could also be a var.
for item in $(seq 0 $max)

DANGER: it is inclusive for the end num, unlike range.

22
New cards

what is the non “seq” way to use range in for loops

{start..end} or {start..step..end}

23
New cards

while loop structure and two keywords inside it

structure:

while [ condition ]; do
	stmt
done

continue skips remaining part in current iteration and jumps to the next one

break terminates the loop

24
New cards

for arrays in bash, what are its limits and requirements on contiguous assignments

there are none for either.

25
New cards

what are the three ways to make an index array

  1. declare with strings inside (space separated NOT comma separated) pet=(cow “a horse” “donkey”)

  2. make by putting in elements pet[2]=”meower”

  3. brace expansion: pet=(a{1..3})


26
New cards

extracting one element for an array and extracting ALL elements for an index array?

one elem: arrayname[i]</code></p><p>allelem:<code>{arrayname[i]}</code> </p><p>all elem: <code>{arrayname[@]}

27
New cards

how to know how many elems there are in an index array?

${#arrayname[@]}

28
New cards

how to extract all assigned indices of an index based array

${!arrayname[@]}

29
New cards

how to extract only some elements from an index based array

{arrayname[@]:startpos where it extracts values starting at index startpos all the way to the end.

{arrayname[@]:startpos:length where it extracts values starting at index startpos and total of length indices.

30
New cards

unquoted array vs quoted @ array vs quoted * array

unquoted: arrayname[@]</code>and<code>{arrayname[@]}</code> and <code>{arrayname[*]} just extracts all content using space to divide it, so multi-word strings in your array are NOT preserved.

quoted *: ”arrayname[∗]”</code>extractsallcontentinaSINGLEstring.entirearrayisinonestring.bashtreatsitasonestring,eveninforloopssoitrunsonlyonce.</p><p>quoted@:<code>”{arrayname[*]}”</code> extracts all content in a SINGLE string. entire array is in one string. bash treats it as one string, even in for loops so it runs only once.</p><p>quoted @: <code>”{arrayname[@]}” extracts all content as elements in strings. multi-word strings are preserved.

31
New cards

howto add new elements to index based array?

how to del elements from index based array?

arrayname+=(elem1 elem2 … elemN)

do NOT ignore parenthesis, otherwise it just concatenates your input to the back of the first element in the array

delete: unset arrayname[index]