371 lines
10 KiB
Plaintext
371 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Interacting with the Command Line Shell"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Basic Linux Commands"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mkdir mynewdir\n",
|
|
"cd mynewdir/\n",
|
|
"/mynewdir$ pwd\n",
|
|
"/mynewdir$ cp ../spider.txt .\n",
|
|
"/mynewdir$ touch myfile.txt\n",
|
|
"/mynewdir$ ls -l \n",
|
|
"#Output:\n",
|
|
"#-rw-rw-r-- 1 user user 0 Mai 22 14:22 myfile.txt\n",
|
|
"#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt\n",
|
|
"/mynewdir$ ls -la\n",
|
|
"#Output:\n",
|
|
"#total 12\n",
|
|
"#drwxr-xr-x 2 user user 4096 Mai 22 14:17 .\n",
|
|
"#drwxr-xr-x 56 user user 12288 Mai 22 14:17 ..\n",
|
|
"#-rw-rw-r-- 1 user user 0 Mai 22 14:22 myfile.txt\n",
|
|
"#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt\n",
|
|
"/mynewdir$ mv myfile.txt emptyfile.txt\n",
|
|
"/mynewdir$ cp spider.txt yetanotherfile.txt\n",
|
|
"/mynewdir$ ls -l\n",
|
|
"#Output:\n",
|
|
"#total 8\n",
|
|
"#-rw-rw-r-- 1 user user 0 Mai 22 14:22 emptyfile.txt\n",
|
|
"#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt\n",
|
|
"#-rw-rw-r-- 1 user user 192 Mai 22 14:23 yetanotherfile.txt\n",
|
|
"/mynewdir$ rm *\n",
|
|
"/mynewdir$ ls -l\n",
|
|
"#total 0\n",
|
|
"/mynewdir$ cd ..\n",
|
|
"rmdir mynewdir/\n",
|
|
"ls mynewdir\n",
|
|
"#ls: cannot access 'mynewdir': No such file or directory\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Redirecting streams"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"cat stdout_example.py \n",
|
|
"#!/usr/bin/env python3\n",
|
|
"print(\"Don't mind me, just a bit of text here...\")\n",
|
|
"./stdout_example.py \n",
|
|
"#Output: Don't mind me, just a bit of text here...\n",
|
|
"./stdout_example.py > new_file.txt\n",
|
|
"cat new_file.txt \n",
|
|
"#Output: Don't mind me, just a bit of text here...\n",
|
|
"./stdout_example.py >> new_file.txt\n",
|
|
"cat new_file.txt \n",
|
|
"#Output: Don't mind me, just a bit of text here...\n",
|
|
" #Don't mind me, just a bit of text here...\n",
|
|
"cat streams_err.py \n",
|
|
"#!/usr/bin/env python3\n",
|
|
"\n",
|
|
"data = input(\"This will come from STDIN: \")\n",
|
|
"print(\"Now we write it to STDOUT: \" + data)\n",
|
|
"raise ValueError(\"Now we generate an error to STDERR\")\n",
|
|
"./streams_err.py < new_file.txt\n",
|
|
"#This will come from STDIN: Now we write it to STDOUT: Don't mind #me, just a bit of text here...\n",
|
|
"#Traceback (most recent call last):\n",
|
|
" #File \"./streams_err.py\", line 5, in <module>\n",
|
|
" #raise ValueError(\"Now we generate an error to STDERR\")\n",
|
|
"#ValueError: Now we generate an error to STDERR\n",
|
|
"./streams_err.py < new_file.txt 2> error_file.txt\n",
|
|
"#This will come from STDIN: Now we write it to STDOUT: Don't mind #me, just a bit of text here...\n",
|
|
"cat error_file.txt \n",
|
|
"#Traceback (most recent call last):\n",
|
|
" #File \"./streams_err.py\", line 5, in <module>\n",
|
|
" #raise ValueError(\"Now we generate an error to STDERR\")\n",
|
|
"#ValueError: Now we generate an error to STDERR\n",
|
|
"echo \"These are the contents of the file\" > myamazingfile.txt\n",
|
|
"cat myamazingfile.txt \n",
|
|
"#These are the contents of the file\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Pipes and pipelines"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"ls -l | less\n",
|
|
"#(... A list of files appears...)\n",
|
|
"# It lists all files and directories in the current directory in long format (including permissions, number of links, owner, group, size, and modification date). The output is then piped into \"less\" command which allows you to page through the results one screen at a time.\n",
|
|
"# This code does the following: \n",
|
|
"# 1. cat spider.txt: Reads the contents of the file \"spider.txt\"\n",
|
|
"# 2. tr ' ' '\\n': Uses the 'tr' command to replace all spaces in the text with newline characters. This transforms each word into a separate line, preparing it for sorting and counting unique occurrences.\n",
|
|
"# 3. sort: Sorts the lines (words) alphabetically\n",
|
|
"# 4. uniq -c: Counts the number of occurrences of each line (word), prefixing each line with the count\n",
|
|
"# 5. sort -nr: Sorts the lines in numerical and reverse order (i.e., descending order) based on the counts from the \"uniq -c\" command. This will put the most frequent words at the top.\n",
|
|
"# 6. head: Displays only the first few lines of the sorted output. By default it shows the first line but here we may change this number to view more results.\n",
|
|
"cat spider.txt | tr ' ' '\\n' | sort | uniq -c | sort -nr | head\n",
|
|
"# This part does the following:\n",
|
|
" # 7 the\n",
|
|
" # 3 up\n",
|
|
" # 3 spider\n",
|
|
" # 3 and\n",
|
|
" # 2 rain\n",
|
|
" # 2 itsy\n",
|
|
" # 2 climbed\n",
|
|
" # 2 came\n",
|
|
" # 2 bitsy\n",
|
|
" # 1 waterspout."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"cat capitalize.py\n",
|
|
"#!/usr/bin/env python3\n",
|
|
"\n",
|
|
"import sys\n",
|
|
"\n",
|
|
"for line in sys.stdin:\n",
|
|
" print(line.strip().capitalize())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"cat haiku.txt \n",
|
|
"#advance your career,\n",
|
|
"#automating with Python,\n",
|
|
"#it's so fun to learn.\n",
|
|
"\n",
|
|
"cat haiku.txt | ./capitalize.py \n",
|
|
"#Advance your career,\n",
|
|
"#Automating with python,\n",
|
|
"#It's so fun to learn.\n",
|
|
"\n",
|
|
"./capitalize.py < haiku.txt\n",
|
|
"#Advance your career,\n",
|
|
"#Automating with python,\n",
|
|
"#It's so fun to learn."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Redirections, Pipes, and Signals"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"**Managing streams**\n",
|
|
"These are the redirectors that we can use to take control of the streams of our programs\n",
|
|
"- command > file: redirects standard output, overwrites file\n",
|
|
"- command >> file: redirects standard output, appends to file\n",
|
|
"- command < file: redirects standard input from file\n",
|
|
"- command 2> file: redirects standard error to file\n",
|
|
"- command1 | command2: connects the output of command1 to the input of command2\n",
|
|
"\n",
|
|
"**Operating with processes**\n",
|
|
"These are some commands that are useful to know in Linux when interacting with processes. Not all of them are explained in videos, so feel free to investigate them on your own.\n",
|
|
"- ps: lists the processes executing in the current terminal for the current user\n",
|
|
"- ps ax: lists all processes currently executing for all users \n",
|
|
"- ps e: shows the environment for the processes listed \n",
|
|
"- kill PID: sends the SIGTERM signal to the process identified by PID\n",
|
|
"- fg: causes a job that was stopped or in the background to return to the foreground\n",
|
|
"- bg: causes a job that was stopped to go to the background\n",
|
|
"- jobs: lists the jobs currently running or stopped\n",
|
|
"- top: shows the processes currently using the most CPU time (press \"q\" to quit) "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Creating Bash scripts"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!/bin/bash\n",
|
|
"echo \"Starting at: $(date)\"\n",
|
|
"echo\n",
|
|
"\n",
|
|
"echo \"UPTIME\"\n",
|
|
"uptime\n",
|
|
"echo\n",
|
|
"\n",
|
|
"echo \"FREE\"\n",
|
|
"free\n",
|
|
"echo\n",
|
|
"\n",
|
|
"echo \"WHO\"\n",
|
|
"who\n",
|
|
"echo\n",
|
|
"\n",
|
|
"echo \"Finishing at: $(date)\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!/bin/bash\n",
|
|
"\n",
|
|
"echo \"Starting at: $(date)\"; echo\n",
|
|
"echo \"UPTIME\"; uptime; echo\n",
|
|
"echo \"FREE\"; free; echo\n",
|
|
"echo \"WHO\"; who; echo\n",
|
|
"echo \"Finishing at: $(date)\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Using variables and globs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"example=hello\n",
|
|
"echo $example"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!/bin/bash\n",
|
|
"\n",
|
|
"line=\"-------------------------------------------------\"\n",
|
|
"echo \"Starting at: $(date)\"; echo $line\n",
|
|
"echo \"UPTIME\"; uptime; echo $line\n",
|
|
"echo \"FREE\"; free; echo $line\n",
|
|
"echo \"WHO\"; who; echo $line\n",
|
|
"echo \"Finishing at: $(date)\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"echo *.py\n",
|
|
"echo c*\n",
|
|
"echo *\n",
|
|
"echo ?????.py"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Conditional execution in Bash"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if test -n \"$PATH\"; then echo \"Your path is not empty\"; fi"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Advanced Bash Concepts"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## While loops in Bash scripts"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"n=1\n",
|
|
"while [ $n -le 5 ]; do\n",
|
|
" echo \"Iteration number $n\"\n",
|
|
" ((n+=1))\n",
|
|
"done"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!/bin/bash\n",
|
|
"n=0\n",
|
|
"command=$1\n",
|
|
"while ! $command && [ $n -le 5 ]; do\n",
|
|
" sleep $n\n",
|
|
" ((n+=1))\n",
|
|
" echo \"Retry #$n\"\n",
|
|
"done;"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|