got up to python scripts
This commit is contained in:
parent
21f0424e48
commit
444e9d7ddd
1 changed files with 144 additions and 3 deletions
|
|
@ -8,6 +8,9 @@
|
|||
- [[../toc.org][TOC | Missing Semester]]
|
||||
- [[https://www.youtube.com/playlist?list=PLyzOVJj3bHQuloKGG59rS43e29ro7I57J][Playlist: Missing Semester]]
|
||||
|
||||
- Curr: https://youtu.be/kgII-YWo3Zw?si=Hy3Vic8blG1mOc7F&t=1357
|
||||
|
||||
|
||||
*** timestamps
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: timestamp
|
||||
|
|
@ -16,7 +19,7 @@
|
|||
#+attr_html: :class playlist
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=54s][00:54 - control flow functions]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=227s][03:47 - sequential execution]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=425s][07:05 - standard input]] *current*
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=425s][07:05 - standard input]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=444s][07:24 - error code]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=517s][08:37 - logical operators]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=603s][10:03 - concatenate commands]]
|
||||
|
|
@ -24,7 +27,7 @@
|
|||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=675s][11:15 - process substitution]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=935s][15:35 - comparison operator]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=1173s][19:33 - curly braces]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=1359s][22:39 - python script]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=1359s][22:39 - python script]] *current*
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=1707s][28:27 - man command]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=2175s][36:15 - finding files]]
|
||||
+ [[https://www.youtube.com/watch?v=kgII-YWo3Zw&t=2190s][36:30 - grep]]
|
||||
|
|
@ -98,7 +101,8 @@ mcd() {
|
|||
|
||||
- this will create a new directory and cd into it as per the definied functioned
|
||||
|
||||
*** variables
|
||||
** error codes
|
||||
*** commands to access codes or values
|
||||
- $0 -- name of the script we are currently in
|
||||
- if you run this in the shell it will display 'bash'
|
||||
- $1->$9 -- the first through ninth argument given to a script
|
||||
|
|
@ -112,3 +116,140 @@ mcd() {
|
|||
ls -lah
|
||||
sudo !!
|
||||
#+end_src
|
||||
|
||||
*** code values
|
||||
- boolean
|
||||
- true returns an error code of 0
|
||||
- false returns an error code of 1
|
||||
|
||||
** using $@ and $*
|
||||
- represents all the arguments passed to a script or a function as separate, individual strings. It preserves each argument as a distinct item, even if the argument contains spaces.
|
||||
|
||||
*** key points about $@
|
||||
1. All Arguments: It refers to all the command-line arguments passed to the script.
|
||||
2. Preserves Spaces: If an argument contains spaces, it is treated as a single argument.
|
||||
3. Usage: It is commonly used in a loop to iterate over each argument.
|
||||
|
||||
*** example
|
||||
#+begin_src bash
|
||||
./myscript.sh file1.txt "file with spaces.txt" file3.txt
|
||||
#+end_src
|
||||
|
||||
then $@ will be
|
||||
=file1.txt "file with spaces.txt" file3.txt=
|
||||
|
||||
*** differences between $*
|
||||
- *$@* treats each argument as a sepearte item
|
||||
- *$** treats all arguments as a single, combined string
|
||||
|
||||
|
||||
** logical operators
|
||||
- you can use the error return value in conditionals
|
||||
*** example using the OR conditional
|
||||
#+begin_src bash
|
||||
false || echo "Oops fail"
|
||||
"Oops fail"
|
||||
#+end_src
|
||||
|
||||
*** example using the AND conditional
|
||||
#+begin_src bash
|
||||
true && echo "this will print"
|
||||
"this will print"
|
||||
#+end_src
|
||||
|
||||
** concatenate commands
|
||||
- you caoncatinate commands using the semicolon: *;*
|
||||
|
||||
#+begin_src bash
|
||||
false; echo "this prints always"
|
||||
"this prints always"
|
||||
#+end_src
|
||||
|
||||
** common substitution
|
||||
- take the output of a command and put it in a variable
|
||||
#+begin_src bash
|
||||
foo=$(pwd)
|
||||
echo $foo
|
||||
"/Users/ronny/..."
|
||||
#+end_src
|
||||
|
||||
- this can also be done by placing that format in a string and it will expand the string
|
||||
#+begin_src bash
|
||||
echo "the pwd output is: $(pwd)"
|
||||
"the pwd output is: /Users/ronny/..."
|
||||
#+end_src
|
||||
|
||||
** process substitution
|
||||
*** description
|
||||
Bash process substitution is a feature that allows you to use the output of a command or a process as if it were a file. It enables you to redirect input or output between processes in a flexible way without needing intermediate temporary files.
|
||||
|
||||
*** Syntax
|
||||
- creates a temporary file descriptor for the output of the command, which can be used as an input file in another command.
|
||||
#+begin_src bash
|
||||
<(command)
|
||||
#+end_src
|
||||
- creates a temporary file descriptor for writing to the command, which can be used as an output file in another command.
|
||||
#+begin_src bash
|
||||
>(command)
|
||||
#+end_src
|
||||
|
||||
*** examples
|
||||
- this will take b, a, d, c and sort it so the result is 'a, b, c, d'
|
||||
#+begin_src bash
|
||||
sort <(echo -e "b\na") <(echo -e "d\nc")
|
||||
#+end_src
|
||||
|
||||
- this will list the files, send the list to *tee* which will split a portion off to the screen and the rest to grep, followed by text_files.txt
|
||||
|
||||
#+begin_src bash
|
||||
ls | tee >(grep "txt" > text_files.txt)
|
||||
#+end_src
|
||||
|
||||
#+begin_src bash
|
||||
echo "Starting program at $(date)" # Date will be substituted
|
||||
echo "Running program $0 with $# arguments with pid $$"
|
||||
|
||||
for file in "$@"; do
|
||||
grep foobar "$file" > /dev/null 2> /dev/null
|
||||
|
||||
# when pattern is not found, grep has exit status 1
|
||||
# we redirect STDOUT and STDERR to a null register since we do not care about them
|
||||
|
||||
if [[ "$?" - ne 0 ]]; then
|
||||
echo "File $file does not have any foober adding one"
|
||||
echo "# foobar" >> "$file"
|
||||
fi
|
||||
done
|
||||
#+end_src
|
||||
|
||||
- *$$* pid given for program
|
||||
- *$#* number of arguments
|
||||
- *$@* expands to all the arguments
|
||||
- can be used in a for loop
|
||||
- *2>* refers to STDERR
|
||||
- *>* refers to STDOUT
|
||||
|
||||
** test utility
|
||||
|
||||
test is a bash utility that you can use to test the condition of a file. look at the man page for more info
|
||||
|
||||
** curly braces
|
||||
curly braces are used as a form of program command expansion.
|
||||
|
||||
the braces contain a number of arguments seperated by commans that will expand into arguments for the program
|
||||
|
||||
*** example
|
||||
#+begin_src bash
|
||||
touch foo{,1,2,10}
|
||||
touch foo foo1 foo2 foo10
|
||||
|
||||
cp foo{,.old}
|
||||
cp foo foo.old
|
||||
#+end_src
|
||||
|
||||
*** ranges
|
||||
this takes the format {1..20}
|
||||
|
||||
#+begin_src bash
|
||||
touch directory{1..4}/foo{a..z}.txt
|
||||
#+end_src
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue