├── .gitignore ├── BPT1_problem_1 ├── readme.md └── script.sh ├── BPT1_problem_2 ├── debug.md ├── readme.md └── solution.txt ├── BPT1_problem_3 ├── readme.md └── script.sh ├── BPT1_problem_4 ├── readme.md └── script.sh ├── BPT2_problem_1 ├── readme.md └── script.sh ├── BPT2_problem_2 ├── readme.md └── script.sh ├── BPT2_problem_3 ├── readme.md └── script.sh ├── BPT2_problem_4 ├── readme.md └── script.sh ├── BPT3_problem_1 ├── readme.md └── script.sh ├── BPT3_problem_2 ├── readme.md └── script.sh ├── BPT3_problem_3 ├── readme.md └── script.sh ├── BPT3_problem_4 ├── readme.md └── script.sh ├── BPT4_problem_1 ├── function.sh └── readme.md ├── BPT4_problem_2 ├── readme.md └── script.sh ├── BPT4_problem_3 ├── convert_file.sh └── readme.md ├── BPT4_problem_4 ├── readme.md └── replace_second_occurrence.sed ├── LICENSE ├── assignment_0 ├── readme.md └── script.sh ├── assignment_1 ├── readme.md └── script.sh ├── assignment_10 ├── readme.md └── script.sh ├── assignment_2 ├── readme.md └── script.sh ├── assignment_3 ├── fibonacci.bc └── readme.md ├── assignment_4 └── readme.md ├── assignment_5 └── readme.md ├── assignment_6 ├── html_output.html ├── readme.md └── script.sh ├── assignment_7 ├── api_output.json ├── readme.md └── script.sh ├── assignment_8 └── readme.md ├── assignment_9 └── readme.md ├── evaluate.sh ├── myCount.sh ├── oppe-practice-1 ├── readme.md └── script.sh ├── oppe-practice-10 └── readme.md ├── oppe-practice-11 └── readme.md ├── oppe-practice-12 └── readme.md ├── oppe-practice-2 └── readme.md ├── oppe-practice-3 └── readme.md ├── oppe-practice-4 └── readme.md ├── oppe-practice-5 └── readme.md ├── oppe-practice-6 └── readme.md ├── oppe-practice-7 └── readme.md ├── oppe-practice-8 └── readme.md ├── oppe-practice-9 └── readme.md ├── ppa_1.1 ├── movetext.sh └── readme.md ├── ppa_1.2 ├── filetypes.sh └── readme.md ├── ppa_2.1 ├── readme.md └── script.sh ├── ppa_2.2 ├── readme.md └── script.sh ├── ppa_3.1 ├── readme.md └── texts.sh ├── ppa_3.2 ├── calender.sh └── readme.md ├── ppa_3.3 ├── errorlog.sh └── readme.md ├── ppa_4.1 ├── dirs.sh └── readme.md ├── ppa_4.2 ├── Pincode_info.csv ├── pincode.sh └── readme.md ├── ppa_5.1 ├── oddargs.sh └── readme.md ├── ppa_5.2 ├── investment.sh └── readme.md ├── ppa_5.3 ├── long.sh └── readme.md ├── ppa_5.4 ├── arch.sh └── readme.md ├── ppa_5.5 ├── fail.sh └── readme.md ├── ppa_5.6 ├── login.sh └── readme.md ├── ppa_5.7 ├── logintime.sh └── readme.md ├── ppa_5.8 ├── readme.md └── su.sh ├── ppa_6.1 ├── readme.md └── swapcase.sh ├── ppa_6.2 ├── myCount.sh └── readme.md ├── ppa_6.3 ├── backup.sh └── readme.md ├── ppa_7.1 ├── evenodd.awk └── readme.md ├── ppa_7.2 ├── readme.md └── repeat.awk ├── ppa_7.3 ├── big.awk └── readme.md ├── practice_2.1 ├── readme.md └── solution.md ├── practice_2.2 ├── readme.md └── script.sh ├── practice_2.3 ├── readme.md └── script.sh ├── practice_3.1 ├── readme.md └── solution.md ├── practice_3.2 ├── readme.md └── script.sh ├── practice_4.1 ├── readme.md └── script.sh ├── practice_4.10 ├── readme.md └── script.sh ├── practice_4.2 ├── readme.md └── script.sh ├── practice_4.3 ├── readme.md └── script.sh ├── practice_4.4 ├── readme.md └── script.sh ├── practice_4.5 ├── readme.md └── script.sh ├── practice_4.6 ├── readme.md └── script.sh ├── practice_4.7 ├── readme.md └── script.sh ├── practice_4.8 ├── readme.md └── script.sh ├── practice_4.9 ├── readme.md └── script.sh ├── practice_5.1 ├── readme.md └── solution.md ├── practice_5.10 ├── readme.md └── script.sh ├── practice_5.11 ├── readme.md └── script.sh ├── practice_5.12 ├── readme.md └── script.sh ├── practice_5.2 ├── readme.md └── solution.md ├── practice_5.3 ├── readme.md └── solution.md ├── practice_5.4 ├── readme.md └── script.sh ├── practice_5.5 ├── readme.md └── script.sh ├── practice_5.6 ├── readme.md └── script.sh ├── practice_5.7 ├── readme.md └── script.sh ├── practice_5.8 ├── readme.md └── script.sh ├── practice_5.9 ├── readme.md └── script.sh ├── practice_7.1a ├── readme.md └── script.sh ├── practice_7.1b ├── readme.md └── script.sh ├── practice_7.1c ├── readme.md └── script.sh ├── practice_7.2 ├── readme.md └── script.sh ├── practice_7.3 ├── readme.md └── script.sh ├── practice_7.4 ├── readme.md └── script.sh ├── practice_7.5 ├── readme.md └── script.sh ├── practice_7.6 ├── readme.md └── script.sh ├── practice_7.7 ├── readme.md └── script.sh ├── practice_7.8 ├── readme.md └── script.sh ├── practice_7.9 ├── readme.md └── script.sh ├── practice_8.1 ├── readme.md └── script.sh ├── practice_8.2 ├── readme.md └── script.sh ├── practice_8.3 ├── readme.md └── script.sh ├── practice_8.4 ├── readme.md └── script.sed ├── practice_8.5 ├── OPTS ├── readme.md └── script.sed ├── practice_9.1 ├── file.awk └── readme.md ├── practice_9.2 ├── file.awk └── readme.md ├── practice_9.3 ├── readme.md └── union.awk ├── practice_9.4 ├── readme.md └── script.awk ├── practice_9.5 ├── readme.md └── script.awk ├── q-awk-1 ├── readme.md └── script.sh ├── q-awk-2 ├── readme.md └── script.sh ├── q-awk-3 ├── readme.md └── script.sh ├── q-awk-4 ├── readme.md └── script.sh ├── q-awk-5 ├── readme.md └── script.sh ├── q-bash-1 ├── readme.md └── script.sh ├── q-bash-2 ├── readme.md └── script.sh ├── q-bash-3 ├── readme.md └── script.sh ├── q-bash-4 ├── readme.md └── script.sh ├── q-grep-1 ├── readme.md └── script.sh ├── q-grep-2 ├── readme.md └── script.sh ├── q-grep-3 ├── readme.md └── script.sh ├── q-grep-4 ├── readme.md └── script.sh ├── q-grep-5 ├── readme.md └── script.sh ├── q-sed-1 ├── readme.md └── script.sed ├── q-sed-2 ├── readme.md └── script.sed ├── q-sed-3 ├── readme.md └── script.sed ├── q-sed-4 ├── readme.md └── script.sed ├── q-sed-5 ├── readme.md └── script.sed ├── readme.md └── regex.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /BPT1_problem_1/readme.md: -------------------------------------------------------------------------------- 1 | A company has 5 departments, each named `Dept1` to `Dept5`. Within each department, there are 3 teams labeled `TeamA` to `TeamC`. Each team works on 10 projects. 2 | 3 | There is a directory for every team in `~/se2001/BPT1_problem_1/`. 4 | 5 | **Write Bash command(s) to create a file for each project, with the files named from `project1` to `project10`, in every team directory. Each file should be empty to ensure minimal space usage.** 6 | 7 | **Note** Please run synchro init command before you start working on your solution. -------------------------------------------------------------------------------- /BPT1_problem_1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for dept in Dept{1..5}; do 4 | for team in Team{A..C}; do 5 | for project in {1..10}; do 6 | touch "./$dept/$team/project$project" 7 | done 8 | done 9 | done 10 | -------------------------------------------------------------------------------- /BPT1_problem_2/debug.md: -------------------------------------------------------------------------------- 1 | # Debugging Tips 2 | 3 | ## Important Note 4 | If you encounter test failures related to links, please ensure you are using **absolute paths** instead of **relative paths**. This can help avoid issues with link resolution during testing. 5 | -------------------------------------------------------------------------------- /BPT1_problem_2/readme.md: -------------------------------------------------------------------------------- 1 | Please use your knowledge of the commands like `mkdir`, `touch`, `ln` etc. to achieve the following directory structure in the current working directory by running the commands (not a script). 2 | 3 | 1. **Create Directories**: 4 | 5 | - Create a base directory called `project`. 6 | - Within `project`, create the following subdirectories using brace expansion: 7 | - `src/{module1,module2}/{include,lib}` 8 | - `docs/{api,manual}` 9 | - Create a directory `bin` within `project`. 10 | 11 | 12 | 2. **Create Files**: 13 | 14 | - Inside `src/module1/include`, create an empty file named `module1.h`. 15 | - Inside `src/module1/lib`, create an empty file named `module1.so`. 16 | - Inside `src/module2/include`, create an empty file named `module2.h`. 17 | - Inside `src/module2/lib`, create an empty file named `module2.so`. 18 | 19 | 20 | 3. **Create Hard Links**: 21 | 22 | - Create a hard link to `module1.h` in the `bin` directory and name it `module1_hardlink.h`. 23 | - Create a hard link to `module2.so` in the `bin` directory and name it `module2_hardlink.so`. 24 | 25 | 26 | 4. **Create Soft Links**: 27 | 28 | - Create a soft link to the `docs` directory in the `project` directory and name it `documentation`. 29 | - Create a soft link to `module1.so` inside `module2/lib` directory and name it `module1_symlink.so`. 30 | 31 | 32 | 5. **Modify Permissions**: 33 | 34 | - Set the permission of `project` directory to `755`. 35 | - Set the permission of all directories under `project/src` to `700`. 36 | - Set the permission of all `.h` files to `644`. 37 | - Set the permission of all `.so` files to `755`. -------------------------------------------------------------------------------- /BPT1_problem_2/solution.txt: -------------------------------------------------------------------------------- 1 | mkdir -p project/src/{module1,module2}/{include,lib} project/docs/{api,manual} project/bin 2 | 3 | touch project/src/module1/include/module1.h project/src/module1/lib/module1.so 4 | touch project/src/module2/include/module2.h project/src/module2/lib/module2.so 5 | 6 | ln project/src/module1/include/module1.h project/bin/module1_hardlink.h 7 | ln project/src/module2/lib/module2.so project/bin/module2_hardlink.so 8 | 9 | ln -s project/docs project/documentation 10 | ln -s project/src/module1/lib/module1.so project/src/module2/lib/module1_symlink.so 11 | 12 | chmod 755 project 13 | chmod 700 project/src/module1/include project/src/module1/lib project/src/module2/include project/src/module2/lib 14 | chmod 644 project/src/module1/include/module1.h project/src/module2/include/module2.h 15 | chmod 755 project/src/module1/lib/module1.so project/src/module2/lib/module2.so 16 | 17 | -------------------------------------------------------------------------------- /BPT1_problem_3/readme.md: -------------------------------------------------------------------------------- 1 | You are given two text files, `file1.txt` and `file2.txt` in the `/opt/assets/` directory, each containing a list of names of world rivers (one name per line) in current working directory. Write two sequential commands in a file `script.sh` and make it executable such that the script will do the following 2 | 3 | 1. Display the differences between them in a unified format. 4 | 5 | 2. Print the names that are common to both files. 6 | 7 | **Hint** 8 | 9 | - **Use appropriate flags/options present in the following commands. Please use man pages or --help flag to understand what each flag do.** 10 | 11 | - **Use `diff`**: Compare the two files and display the differences between them in a unified format. 12 | 13 | - **Use** `comm`: can be used for the following 14 | - Names that are common to both files. -------------------------------------------------------------------------------- /BPT1_problem_3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | diff -u /opt/assets/file1.txt /opt/assets/file2.txt 4 | comm -12 <(sort /opt/assets/file1.txt) <(sort /opt/assets/file2.txt) -------------------------------------------------------------------------------- /BPT1_problem_4/readme.md: -------------------------------------------------------------------------------- 1 | Please use the nslookup command (on powershell or linux on your local system) to identify the IP addresses associated with the following websites. After obtaining the IP addresses, enter each one into a browser to check if it directs you to the corresponding website. For each website, print the result using the echo command: if the website is successfully reached using the IP address, print "www.website.com Yes", otherwise print "www.website.com No". Perform these steps for each website sequentially: 2 | 3 | www.google.com 4 | www.yahoo.com 5 | www.duckduckgo.com 6 | www.youtube.com 7 | www.aws.amazon.com 8 | 9 | Create a file with name `script.sh` which contains the `echo` commands sequentially. 10 | 11 | Hint: 12 | 13 | You can use the nslookup command for DNS lookup. 14 | 15 | What is DNS and Why Perform a DNS Lookup?: The Domain Name System (DNS) translates human-readable domain names (like www.example.com) into IP addresses (like 93.184.216.34). DNS is crucial because it allows users to access websites using domain names instead of remembering complex IP addresses. A DNS lookup retrieves the IP address associated with a domain name, which is essential for network troubleshooting and ensuring that domain names are correctly mapped to their respective IP addresses. -------------------------------------------------------------------------------- /BPT1_problem_4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "www.google.com Yes" 3 | echo "www.yahoo.com No" 4 | echo "www.duckduckgo.com Yes" 5 | echo "www.youtube.com No" 6 | echo "www.aws.amazon.com No" -------------------------------------------------------------------------------- /BPT2_problem_1/readme.md: -------------------------------------------------------------------------------- 1 | Write a **bash script** `script.sh` to extract the country name and its capital from a HTML file. 2 | Assume that the HTML Code is of the following format and convert it in to the format as shown in the sample output. 3 | 4 | **Note** 5 | 6 | The HTML input to the script will be sent through standard input. 7 | 8 | **Sample Input** 9 | 10 | ```html 11 |
  • Country: France, Capital: Paris
  • 12 |
  • Country: Japan, Capital: Tokyo
  • 13 |
  • Country: Canada, Capital: Ottawa
  • 14 | ``` 15 | 16 | **Sample Output** 17 | 18 | ```bash 19 | France 20 | Paris 21 | Japan 22 | Tokyo 23 | Canada 24 | Ottawa 25 | ``` -------------------------------------------------------------------------------- /BPT2_problem_1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -oP '(?<=)[^<]+|(?<=)[^<]+' -------------------------------------------------------------------------------- /BPT2_problem_2/readme.md: -------------------------------------------------------------------------------- 1 | Write a shell script in `script.sh` that checks if either the `global_config` or `local_config` directories exist, and if either of them do, then create an empty file named `default_config` in current working directory. 2 | 3 | **Hint** 4 | 5 | - Use the concepts taught in Week 3 in Redirections and Combining Commands. 6 | - Make appropriate use of || and && operators. -------------------------------------------------------------------------------- /BPT2_problem_2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | [ -d "global_config" ] || [ -d "local_config" ] && touch default_config -------------------------------------------------------------------------------- /BPT2_problem_3/readme.md: -------------------------------------------------------------------------------- 1 | Create a script in `script.sh` which checks for the string "database_error" in the `app.log` file. 2 | If the string is found add a timestamped entry to `error_report.log`. 3 | The timestamp should be of the current date and time. 4 | Refer to the **sample output** for the format. 5 | 6 | **Hint** 7 | 8 | - To add the timestamp you need to use the `date` command. 9 | 10 | **Sample input (from app.log)** 11 | 12 | ```bash 13 | 2024-06-27 12:00:00 INFO Application started 14 | 2024-06-27 12:01:00 ERROR Failed to connect to database 15 | 2024-06-27 12:02:00 ERROR database_error detected in module X 16 | 2024-06-27 12:03:00 INFO Processing request 17 | 2024-06-27 12:04:00 ERROR database_error: connection lost 18 | 19 | ``` 20 | **Sample output** 21 | 22 | ```bash 23 | Fri Jun 28 12:00:00 UTC 2024: Database error found 24 | ``` -------------------------------------------------------------------------------- /BPT2_problem_3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if grep -q "database_error" app.log; then 4 | time=$(date) 5 | echo "$time: Database error found" >> error_report.log 6 | fi -------------------------------------------------------------------------------- /BPT2_problem_4/readme.md: -------------------------------------------------------------------------------- 1 | A CSV file named `people.csv` contains rows with the name and age of people. 2 | Write a shell script using `grep` in `script.sh` to remove all the rows where the age is not a numeric value. 3 | 4 | **Note** 5 | 6 | - The input is provided through the standard input (`&1`). 7 | - The header would also be filtered out. 8 | 9 | **Sample Input** 10 | ```bash 11 | Name,Age 12 | John Doe,30 13 | Jane Smith,twenty-five 14 | Alice Johnson,25 15 | Bob Brown,45 16 | ``` 17 | **Sample Output** 18 | 19 | ```bash 20 | John Doe,30 21 | Alice Johnson,25 22 | Bob Brown,45 23 | ``` -------------------------------------------------------------------------------- /BPT2_problem_4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -E '^[^,]+,[0-9]+$' <&0 -------------------------------------------------------------------------------- /BPT3_problem_1/readme.md: -------------------------------------------------------------------------------- 1 | Write a `script.sh` that reads a CSV file `student_data.csv` present in the current working directory containing student information with columns StudentID,Name,Age,Grade. 2 | The script should check if the `StudentID` field is **numeric** for each row. 3 | Print a lists of each row with an `invalid StudentID` error message along with the corresponding row number. 4 | Please refer to the following sample input and output for reference. 5 | 6 | Note: 7 | - Assume that the `student_data.csv` is present in the current working directory. It is created dynamically for each test case. 8 | - Using `tail -n +2 student_data.csv` will allow you to access the file without the header of the file. 9 | 10 | **Sample input** 11 | 12 | ``` 13 | StudentID,Name,Age,Grade 14 | 101,John Doe,20,A 15 | abc123,Jane Smith,22,B 16 | 104,Mike Brown,23,C 17 | 105,,19,D 18 | ``` 19 | 20 | **Sample output** 21 | 22 | ``` 23 | Row 2: Invalid StudentID 24 | ``` -------------------------------------------------------------------------------- /BPT3_problem_1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | tail -n +2 student_data.csv | while IFS=, read -r StudentID Name Age Grade; do 4 | line_number=$(grep -n "$StudentID,$Name,$Age,$Grade" student_data.csv | cut -d: -f1) 5 | row_number=$(echo "$line_number - 1" | bc) 6 | if ! [[ "$StudentID" =~ ^[0-9]+$ ]]; then 7 | echo "Row $row_number: Invalid StudentID" 8 | fi 9 | done -------------------------------------------------------------------------------- /BPT3_problem_2/readme.md: -------------------------------------------------------------------------------- 1 | Write a `script.sh` that processes a CSV file containing names and ages, and outputs the names of people whose age is a multiple of 5. 2 | 3 | **Note** 4 | - The file also contains headers. 5 | - The CSV file is given through the standard input (`&0`) 6 | 7 | **Sample Input** 8 | 9 | ``` 10 | Name,Age 11 | John Doe,30 12 | Jane Smith,22 13 | Alice Johnson,25 14 | Bob Brown,45 15 | ``` 16 | 17 | **Sample Output** 18 | 19 | ``` 20 | John Doe 21 | Alice Johnson 22 | Bob Brown 23 | ``` -------------------------------------------------------------------------------- /BPT3_problem_2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | { 4 | read 5 | while IFS=, read -r Name Age; do 6 | if (( Age % 5 == 0 )); then 7 | echo "$Name" 8 | fi 9 | done 10 | } -o -t " 13 | exit 1 ;; 14 | esac 15 | done 16 | 17 | if [ -z "$input_file" ] || [ -z "$output_file" ] || [ -z "$conv_type" ]; then 18 | echo "Error: All options (-i, -o, -t) are required" 19 | exit 1 20 | fi 21 | 22 | if [ ! -f "$input_file" ]; then 23 | echo "Error: Input file '$input_file' not found" 24 | exit 1 25 | fi 26 | 27 | case "$conv_type" in 28 | "csv2tsv") 29 | awk -v FPAT='([^,]*)|("[^"]+")' 'BEGIN { 30 | OFS = "\t" 31 | } 32 | { 33 | if (NF == 0) { 34 | print "" 35 | } else { 36 | for (i = 1; i <= NF; i++) { 37 | gsub(/^"|"$/, "", $i) # Remove surrounding quotes 38 | printf "%s%s", $i, (i == NF ? "\n" : OFS) 39 | } 40 | } 41 | }' "$input_file" > "$output_file" 42 | ;; 43 | "tsv2csv") 44 | awk 'BEGIN { 45 | OFS = "," 46 | } 47 | { 48 | if (NF == 0) { 49 | print "" 50 | } else { 51 | n = split($0, fields, "\t") 52 | for (i = 1; i <= n; i++) { 53 | # Remove existing quotes if present 54 | gsub(/^"|"$/, "", fields[i]) 55 | 56 | # Check if field needs quoting (contains comma, quote, or newline) 57 | if (fields[i] ~ /[,"\n]/) { 58 | gsub(/"/, "\"\"", fields[i]) # Escape existing quotes 59 | fields[i] = "\"" fields[i] "\"" # Add surrounding quotes 60 | } 61 | printf "%s%s", fields[i], (i == n ? "\n" : OFS) 62 | } 63 | } 64 | }' "$input_file" > "$output_file" 65 | ;; 66 | *) 67 | echo "Error: Invalid conversion type. Use 'csv2tsv' or 'tsv2csv'" 68 | exit 1 69 | ;; 70 | esac 71 | -------------------------------------------------------------------------------- /BPT4_problem_3/readme.md: -------------------------------------------------------------------------------- 1 | Sriveni is receiving files from client in various format. 2 | She is recieving numerous files in different formats and she wanted to automate the task of cleaning these files. 3 | 4 | She often needs to convert data files between different formats, such as CSV and TSV, for various data processing tasks. 5 | To simplify this process, she wants to create a Bash script that can convert a file from one format to another. 6 | 7 | Write a Bash script named `convert_file.sh` that uses **getopts** to parse command-line options. 8 | The script should accept the following options: 9 | 10 | -i : Specifies the input filename. 11 | -o : Specifies the output filename. 12 | -t : Specifies the type of conversion, either `csv2tsv` or `tsv2csv`. 13 | 14 | The script should read the input file, perform the specified conversion, and write the output to the specified output file. 15 | The file should be assumed to be present in the _cwd_. 16 | 17 | **Note** 18 | 19 | - Apart from `getopts` you will need to used `sed` commands for the coversion. 20 | 21 | **Sample Usage** 22 | 23 | ``` 24 | bash convert_file.sh -i input.csv -o output.tsv -t csv2tsv 25 | ``` 26 | 27 | **Sample Input** 28 | 29 | ``` 30 | Name,Age,Location 31 | John Doe,30,New York 32 | Jane Smith,25,Los Angeles 33 | ``` 34 | 35 | **Sample Output** 36 | 37 | ``` 38 | Name Age Location 39 | John Doe 30 New York 40 | Jane Smith 25 Los Angeles 41 | ``` 42 | 43 | **Example `getopts` Template** 44 | 45 | ``` 46 | while getopts "flags" o; do 47 | case "${o}" in 48 | f) command1 ;; 49 | l) command2 ;; 50 | a) command3 ;& # fall through 51 | g) command4 ;; 52 | *) echo "Illegal flag"; exit ;; 53 | esac 54 | done 55 | ``` 56 | -------------------------------------------------------------------------------- /BPT4_problem_4/readme.md: -------------------------------------------------------------------------------- 1 | You have stored logs in a text file where each line starts with a hexadecimal code followed by a log message. 2 | Your task is to write a sed script named `replace_second_occurrence.sed` that replaces the second occurrence of the word "error" with the word "issue" in each line, but only if the line starts with a hexadecimal code. 3 | 4 | **Note** 5 | 6 | - Assume that the file is given as standard input (`&0`) to your script. 7 | - make sure that your sed expression is case insensitve. 8 | 9 | **Example Command:** 10 | 11 | ``` 12 | cat input.txt | sed -f replace_second_occurrence.sed > output.txt 13 | ``` 14 | 15 | **Sample input.log:** 16 | 17 | ``` 18 | 1a2b3c4d This is an error message with an error that needs fixing. 19 | 5f6e7d8c No errors here, just a warning. 20 | abcd1234 Another error followed by error. 21 | 1234abcd error error error error 22 | nohexcode error should not be replaced here. or it will be an error. 23 | ``` 24 | 25 | **Sample output.txt:** 26 | 27 | ``` 28 | 1a2b3c4d This is an error message with an issue that needs fixing. 29 | 5f6e7d8c No errors here, just a warning. 30 | abcd1234 Another error followed by issue. 31 | 1234abcd error issue error error 32 | nohexcode error should not be replaced here. or it will be an error. 33 | ``` 34 | -------------------------------------------------------------------------------- /BPT4_problem_4/replace_second_occurrence.sed: -------------------------------------------------------------------------------- 1 | /^[0-9A-Fa-f]\{8\}/{ 2 | s/\([Ee][Rr][Rr][Oo][Rr]\)/issue/2I 3 | } 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Akshit Mehta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /assignment_0/readme.md: -------------------------------------------------------------------------------- 1 | Welcome to the your zeroth VM Task. This assignment is not graded and provided with a solution file. 2 | 3 | **IMPORTANT**: execute `synchro eval` from the assignment directory (for this assignment `~/se2001/assignment_0`) to record your submission for grading. 4 | 5 | # Assignment 0 (Not graded) 6 | 7 | Write a script `~/se2001/assignment_0/script.sh` that takes a filename as argument and print only the hash value. 8 | 9 | # Verification 10 | 11 | Once you have written your script you can verify the same using the following command 12 | 13 | ``` 14 | ~/se2001/assignment_0/script.sh hello.txt 15 | ``` 16 | 17 | The expected output should be 18 | 19 | ``` 20 | 133ee989293f92736301280c6f14c89d521200c17dcdcecca30cd20705332d44 21 | ``` -------------------------------------------------------------------------------- /assignment_0/script.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | sha256sum "$1" | cut -d" " -f1 -------------------------------------------------------------------------------- /assignment_1/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 1 2 | 3 | Write a script `script.sh` in `~/se2001/assignment_1` to download the file `sample_1.txt` from [http://local.server](http://local.server) and store it as `s1.txt` in `~/se2001/assignment_1/` 4 | 5 | Hint: Use `wget` command 6 | 7 | ## Optional 8 | 9 | If you are able to achieve the above task then, 10 | 11 | - Try to download the file `sample_2.txt`, `sample_3.txt` and `sample_4.txt`. 12 | - Use `cURL` to achieve the same. -------------------------------------------------------------------------------- /assignment_1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | wget http://local.server/sample_1.txt -O s1.txt 4 | curl -o s2.txt http://local.server/sample_2.txt 5 | curl -o s3.txt http://local.server/sample_3.txt 6 | curl -o s4.txt http://local.server/sample_4.txt -------------------------------------------------------------------------------- /assignment_10/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 10 2 | 3 | Write a script `~/se2001/assignment_10/script.sh` that takes a filename as argument and print only the MD5 hash value. 4 | 5 | The expected output should be 6 | 7 | ``` 8 | 916f4c31aaa35d6b867dae9a7f54270d 9 | ``` 10 | -------------------------------------------------------------------------------- /assignment_10/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo $(md5sum $1 | awk '{print $1}') 4 | -------------------------------------------------------------------------------- /assignment_2/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 2 2 | 3 | Create a file named `.txt` in `~/se2001/assignment_2` and write a script `script.sh` in `~/se2001/assignment_2` to upload the file `.txt` to [http://local.server/upload/.txt](http://local.server/upload/.txt). 4 | 5 | Hint: 6 | - Use can verify the uploaded the file kept temporarily in the location `/tmp/local_uploads` 7 | - Use PUT request for while sending the file -------------------------------------------------------------------------------- /assignment_2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | curl -T 23f3001694.txt http://local.server/upload/23f3001694.txt -------------------------------------------------------------------------------- /assignment_3/fibonacci.bc: -------------------------------------------------------------------------------- 1 | define fibonacci(n) { 2 | if(n == 0) { 3 | return 0; 4 | } 5 | if(n == 1) { 6 | return 1; 7 | } 8 | 9 | a = 0; 10 | b = 1; 11 | 12 | for(i = 2; i < n; i++) { 13 | result = a + b; 14 | a = b; 15 | b = result; 16 | } 17 | 18 | return result; 19 | } 20 | -------------------------------------------------------------------------------- /assignment_3/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 3 2 | 3 | Define a function ` fibonacci(n) ` in file ` fibonacci.bc `. 4 | 5 | The function takes one positional argument which is an integer, say ` n `, and returns ` n `th Fibonacci number in Fibonacci sequence. 6 | 7 | Hint : Functions in ` bc `. 8 | 9 | ## Example : 10 | 11 | Fibonacci sequence : 0, 1, 1, 2, 3, 5, 8, 13, ..... 12 | 13 | If the function is invoked as ` fibonacci(5) `, then the output should be `3`, 14 | as ` 3 ` is at the ` 5 `th position in the Fibonacci sequence. -------------------------------------------------------------------------------- /assignment_4/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 4 2 | 3 | **This assignment is only available between 7pm and 9pm IST** 4 | 5 | Create an account in GitHub and create a public repository named `se2001-` (in lowercase) and initialize with README.md 6 | 7 | Create a file named `script.sh` that exports two shell variables 8 | 9 | - `GITHUB_USERNAME` that holds the GitHub username Ex: "bscse2001" 10 | - `GITHUB_REPOSITORY` that holds the GitHub repository name Ex: "se2001-21f1000000" 11 | 12 | The repository can be deleted after the evaluation and submission of Assignment 5 is successful. -------------------------------------------------------------------------------- /assignment_5/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 5 2 | 3 | **This assignment is only available between 7pm and 9pm IST** 4 | 5 | Clone the repository that is created in Assignment 4 and keep it in ~/se2001/assignment_5/se2001-/ 6 | 7 | Create a file named `script.sh` that exports two shell variables 8 | 9 | - `GITHUB_USERNAME` that holds the GitHub username Ex: "bscse2001" 10 | - `GITHUB_REPOSITORY` that holds the GitHub repository name Ex: "se2001-21f1000000" 11 | 12 | **Sample file structure** 13 | 14 | ``` 15 | ~/se2001/assignment_5/ 16 | └── se2001-21f1000000 17 | └── README.md 18 | ``` -------------------------------------------------------------------------------- /assignment_6/html_output.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Weather - Pune 6 | 7 | 8 |

    Pune

    9 |

    India Meteorological Department

    10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
    DateTemp (°C)Description
    27 Mar (Mon)15°C | 34°CClear
    28 Mar (Tue)16°C | 34°CClear
    29 Mar (Wed)16°C | 35°CClear
    30 Mar (Thu)17°C | 34°CPartly Cloudy
    31 Mar (Fri)18°C | 34°CPartly Cloudy
    1 Apr (Sat) 18°C | 34°CPartly Cloudy
    47 | 48 | -------------------------------------------------------------------------------- /assignment_6/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 6 2 | 3 | Write a bash script ` script.sh ` to scrape a weather forecast website. 4 | 5 | The script takes 3 command line arguments 6 | 7 | * argument 1 8 | - ` city-name `. 9 | 10 | * argument 2 11 | - One of ` min ` and ` max ` refers to minimum temperature and maximum temperature respectively. 12 | 13 | * argument 3 14 | - One of ` mode ` and ` average `. 15 | - If you get more than one mode, your script should return the smallest mode value. 16 | 17 | The output 18 | * A number 19 | - Round of your answer to two decimal places. 20 | - i.e if the actual answer is `15.666666` , your answer should be `15.67` 21 | 22 | Url of weather forecast website : [http://weather.local](http://weather.local) 23 | 24 | Interpretation : 25 | 26 | ``` 27 | ./script.sh chennai min average 28 | 26 29 | ``` 30 | 31 | This returns the ` average ` of the ` min `imum temperature of city ` chennai ` on all days given in forecast. 32 | -------------------------------------------------------------------------------- /assignment_6/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ "$#" -ne 3 ]; then 4 | exit 1 5 | fi 6 | 7 | CITY=$1 8 | TEMP_TYPE=$2 9 | CALC_TYPE=$3 10 | 11 | WEATHER_DATA=$(curl -s http://weather.local/city/${CITY}.html) 12 | 13 | if [ -z "$WEATHER_DATA" ]; then 14 | exit 1 15 | fi 16 | 17 | if [ "$TEMP_TYPE" == "min" ]; then 18 | TEMPS=$(echo "$WEATHER_DATA" | grep -oP '\d+°C \| \d+°C' | sed 's/°C.*//') 19 | elif [ "$TEMP_TYPE" == "max" ]; then 20 | TEMPS=$(echo "$WEATHER_DATA" | grep -oP '\d+°C \| \d+°C' | sed 's/.*| \([0-9]\+\)°C/\1/') 21 | else 22 | exit 1 23 | fi 24 | 25 | if [ -z "$TEMPS" ]; then 26 | exit 1 27 | fi 28 | 29 | if [ "$(echo "$TEMPS" | wc -l)" -eq 1 ]; then 30 | printf "%.2f\n" "$TEMPS" 31 | exit 0 32 | fi 33 | 34 | average() { 35 | local sum=0 36 | local count=0 37 | for temp in $TEMPS; do 38 | sum=$(echo "$sum + $temp" | bc) 39 | ((count++)) 40 | done 41 | printf "%.2f\n" "$(echo "$sum / $count" | bc -l)" 42 | } 43 | 44 | mode() { 45 | local freq=() 46 | local max_count=0 47 | local mode=0 48 | 49 | for temp in $TEMPS; do 50 | freq[$temp]=$(( ${freq[$temp]:-0} + 1 )) 51 | if [ ${freq[$temp]} -gt $max_count ]; then 52 | max_count=${freq[$temp]} 53 | mode=$temp 54 | elif [ ${freq[$temp]} -eq $max_count ] && [ "$(echo "$temp < $mode" | bc)" -eq 1 ]; then 55 | mode=$temp 56 | fi 57 | done 58 | printf "%.2f\n" "$mode" 59 | } 60 | 61 | if [ "$CALC_TYPE" == "average" ]; then 62 | average 63 | elif [ "$CALC_TYPE" == "mode" ]; then 64 | mode 65 | else 66 | exit 1 67 | fi -------------------------------------------------------------------------------- /assignment_7/api_output.json: -------------------------------------------------------------------------------- 1 | { 2 | "coord": { 3 | "lon": 80.2707, 4 | "lat": 13.0827 5 | }, 6 | "weather": [ 7 | { 8 | "id": 802, 9 | "main": "Clouds", 10 | "description": "scattered clouds", 11 | "icon": "https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F03d.png?1499366020890" 12 | } 13 | ], 14 | "base": "stations", 15 | "main": { 16 | "temp": 31.67, 17 | "feels_like": 38.67, 18 | "temp_min": 31.67, 19 | "temp_max": 33.01, 20 | "pressure": 1015, 21 | "humidity": 77 22 | }, 23 | "visibility": 8000, 24 | "wind": { 25 | "speed": 5.14, 26 | "deg": 230 27 | }, 28 | "clouds": { 29 | "all": 40 30 | }, 31 | "dt": 1680669963, 32 | "sys": { 33 | "type": 2, 34 | "id": 2012809, 35 | "country": "IN", 36 | "sunrise": 1680654762, 37 | "sunset": 1680699031 38 | }, 39 | "timezone": 19800, 40 | "id": 1465730, 41 | "name": "Park Town", 42 | "cod": 200 43 | } 44 | -------------------------------------------------------------------------------- /assignment_7/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 7 2 | 3 | The website has weather data about some cities in **India** as a snapshot at particular instant in time. Please, explore the website to know what data each page consists. 4 | 5 | Write a bash script ` script.sh ` using `jq` to parse and filter json data from weather API website. 6 | 7 | The script provides options below. 8 | 9 | | Option | Arg | Description | Output format | Example format | 10 | | :----:| :------: | :---------| :----- | :-------- | 11 | | | ` city-name ` | city name (required), the output city name should match API data | | | 12 | | ` -C ` | `min` | minimum temperature | json object | `{"name": , "temp_min": }` | 13 | | | ` max ` | maximum temperature | json object | `{"name": , "temp_max": }` | 14 | | | ` current ` | current temperature (`temp`) and it's Fahrenheit representation | json object | `{"name": , "temp": , "F": }` | 15 | | ` -W ` | | wind speed & it's square root | json object | `{"name": , "speed": , "sqrtspeed": }` | 16 | | ` -S ` | | city name, date, sunrise and sunset time in HH:MM:SS | json array | `[, DD/MM/YYYY, HH:MM:SS, HH:MM:SS]` | 17 | 18 | Weather API URL: [http://weather.local/api/v1/city/.json](http://weather.local/api/v1/city/.json) 19 | 20 | Some Rules : 21 | 22 | * The output must be pure ascii 23 | * The output indenation must 4 spaces for json object. 24 | 25 | Interpretation : 26 | 27 | ``` 28 | ~$ ./script.sh chennai -C min 29 | { 30 | "name": "Park Town", 31 | "temp_min": 31.67 32 | } 33 | ``` -------------------------------------------------------------------------------- /assignment_7/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ -z "$1" ]]; then 4 | exit 1 5 | fi 6 | 7 | CITY=$1 8 | API_URL="http://weather.local/api/v1/city/${CITY}.json" 9 | DATA=$(curl -s "$API_URL") 10 | 11 | if [[ -z "$DATA" ]]; then 12 | exit 1 13 | fi 14 | 15 | case "$2" in 16 | -C) 17 | case "$3" in 18 | min) 19 | echo "$DATA" | jq -c '{name: .name, temp_min: .main.temp_min}' | jq --indent 4 '.' 20 | ;; 21 | max) 22 | echo "$DATA" | jq -c '{name: .name, temp_max: .main.temp_max}' | jq --indent 4 '.' 23 | ;; 24 | current) 25 | TEMP=$(echo "$DATA" | jq '.main.temp') 26 | FAHRENHEIT=$(printf "%.2f" "$(echo "$TEMP * 9 / 5 + 32" | bc -l)") 27 | echo "$DATA" | jq -c --argjson fahrenheit "$FAHRENHEIT" '{name: .name, temp: .main.temp, F: $fahrenheit}' | jq --indent 4 '.' 28 | ;; 29 | *) 30 | exit 1 31 | ;; 32 | esac 33 | ;; 34 | -W) 35 | WIND_SPEED=$(echo "$DATA" | jq '.wind.speed') 36 | SQRT_SPEED=$(printf "%.2f" "$(echo "sqrt($WIND_SPEED)" | bc -l)") 37 | echo "$DATA" | jq -c --argjson sqrt_speed "$SQRT_SPEED" '{name: .name, speed: .wind.speed, sqrtspeed: $sqrt_speed}' | jq --indent 4 '.' 38 | ;; 39 | -S) 40 | DATE=$(date "+%d/%m/%Y") 41 | SUNRISE=$(date -d @"$(echo "$DATA" | jq '.sys.sunrise')" "+%H:%M:%S") 42 | SUNSET=$(date -d @"$(echo "$DATA" | jq '.sys.sunset')" "+%H:%M:%S") 43 | jq -n --arg city "$(echo "$DATA" | jq -r '.name')" --arg date "$DATE" --arg sunrise "$SUNRISE" --arg sunset "$SUNSET" '[$city, $date, $sunrise, $sunset]' | jq --indent 4 '.' 44 | ;; 45 | *) 46 | exit 1 47 | ;; 48 | esac 49 | -------------------------------------------------------------------------------- /assignment_8/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 8 2 | 3 | Git offers a feature called cherry pick which is used to pick a commit from different branch and apply into your branch. 4 | 5 | Your tasks are 6 | 7 | - Clone the git repository from `/opt/repository/fibonacci` to `~/se2001/assignment_8/fibonacci` using `git clone` 8 | - Create a branch `` from `master` 9 | - Replace the example fibonacci input by your roll number (final three digits without preceded zeroes) in the file README.md and do a commit with message `update example` 10 | - Cherry pick the commit `693dca7f5dae2b72a960018ba3df5b8819963b37` from the branch `remove-todo` and apply it your branch 11 | - Merge the branch `` to `main` locally. -------------------------------------------------------------------------------- /assignment_9/readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 9 2 | 3 | Consider 4 | 5 | ## Iris flower data set 6 | 7 | * Source 8 | - Creator : R. A. Fischer 9 | - [https://archive.ics.uci.edu/ml/datasets/iris](https://archive.ics.uci.edu/ml/datasets/iris) 10 | 11 | ### Attribute Information 12 | 13 | 1. sepal length in cm 14 | 2. sepal width in cm 15 | 3. petal length in cm 16 | 4. petal width in cm 17 | 5. class: 18 | - Iris Setosa 19 | - Iris Versicolour 20 | - Iris Virginica 21 | 22 | The dataset is stored in a table in sqlite3 database. 23 | * Database name : ` iris-flower.sqlite3 ` 24 | * Table name : ` iris ` 25 | * Table Schema : ` CREATE TABLE iris (id INTEGER PRIMARY KEY AUTOINCREMENT, sepal_length REAL, sepal_width REAL, petal_length REAL, petal_width REAL, class VARCHAR(20)); ` 26 | 27 | * Find the database in this directory : ` /opt/iris ` 28 | 29 | Directory Structure : 30 | 31 | ```tree 32 | /opt/ 33 | |_ iris/ 34 | |_ iris-flower.sqlite3 35 | ``` 36 | 37 | Write a bash script ` script.sh ` to process iris data and return mean of a particular column of the dataset. 38 | 39 | The script will be invoked as follows: 40 | 41 | ```terminal 42 | ~$ ./script.sh 43 | ``` 44 | 45 | Argument 1 : 46 | 47 | * ` ` 48 | 49 | - ` setosa ` : For class Iris Setosa 50 | - ` versicolor ` : For class Iris Versicolour 51 | - ` virginica ` : For class Iris Virginica 52 | 53 | Argument 2 : 54 | 55 | * ` ` 56 | 57 | - ` sepal_length ` 58 | - ` sepal_width ` 59 | - ` petal_length ` 60 | - ` petal_width ` 61 | 62 | The output must be ` jq ` parsable. 63 | 64 | * ` jq ` parsable output : 65 | 66 | ``` 67 | {"class": , "feature": , "mean": } 68 | ``` 69 | 70 | * ` ` : This needs to be consistent with class names in sqlite3 database. 71 | * ` ` : Name of the feature. 72 | - If feature name in sqlite3 database is ` sepal_length ` this value should be ` Sepal length `. 73 | * ` ` : Mean of rows of ` ` over ` ` 74 | 75 | Round off ` ` up to 3 decimal places. 76 | 77 | 78 | 79 | 80 | Sample Example 81 | * Execution 82 | 83 | ``` 84 | ./script.sh setosa sepal_length 85 | ``` 86 | 87 | * Output 88 | 89 | ``` 90 | {"class":"Iris-setosa","feature":"Sepal length","mean":5.006} 91 | ``` 92 | 93 | Hint : 94 | 95 | * Use sql from command line to get the desired data. 96 | * Use ` echo ` to format the output in ` jq ` parsable form. 97 | -------------------------------------------------------------------------------- /evaluate.sh: -------------------------------------------------------------------------------- 1 | # Run evaluate.sh in the assignment directory to evaluate the assignment. 2 | # It will evaluate the assignment and save the output in output.txt 3 | # The excercises that require manual input are not evaluated. 4 | 5 | #WIP -------------------------------------------------------------------------------- /myCount.sh: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /oppe-practice-1/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 1 2 | 3 | ## Problem Statement 4 | 5 | Write a script `script.sh` to execute the bash script `run.sh` and print "SUCCESS" if it exits with status 0, otherwise print "FAIL". Note that the file may not have execute permission. 6 | 7 | Note: `run.sh` may also be absent, handle it accordingly. 8 | 9 | ## Sample Example 10 | 11 | ```bash 12 | > echo "echo hello" > run.sh 13 | > chmod a-x run.sh 14 | > ./script.sh 15 | SUCCESS 16 | > echo "echa hello" > run.sh 17 | > ./script.sh 18 | FAIL 19 | ``` 20 | -------------------------------------------------------------------------------- /oppe-practice-1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | chmod u+x run.sh 3 | if ./run.sh &>/dev/null ; then echo SUCCESS 4 | else echo FAIL 5 | fi -------------------------------------------------------------------------------- /oppe-practice-10/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 10 2 | 3 | ## Problem Statement 4 | 5 | Write a script `script.sh` that will remove the outer rectangle and grow the inner rectangle to the exact size of outer rectangle and the inner rectangle region should be replaced with '0' (zero) including the boundary line. 6 | 7 | Note: 8 | - The outer rectangle is always made up of asterisks '*' 9 | - The inner rectangle is always made up of lowercase X 'x' 10 | - The input should be taken from the standard input 11 | 12 | ## Sample Input 13 | 14 | ``` 15 | ******************************** 16 | * * 17 | * xxxxxxxxxxxxxxxxxxxxxxxxxx * 18 | * x x * 19 | * x x * 20 | * xxxxxxxxxxxxxxxxxxxxxxxxxx * 21 | * * 22 | ******************************** 23 | ``` 24 | 25 | ## Sample Output 26 | 27 | ``` 28 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 29 | x x 30 | x 00000000000000000000000000 x 31 | x 00000000000000000000000000 x 32 | x 00000000000000000000000000 x 33 | x 00000000000000000000000000 x 34 | x x 35 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 36 | ``` 37 | -------------------------------------------------------------------------------- /oppe-practice-11/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 11 2 | 3 | ## Problem Statement 4 | 5 | A text input usually contains puctuations, lowercase letters, and uppercase letters. 6 | 7 | Write a script `script.sh` which will 8 | - remove punctuations 9 | - convert uppercase letters to lowercase and 10 | - convert the space character to newline character in that order. 11 | 12 | Finally use concepts of `sort` and `uniq` commands to print the most frequent words ranked 5 to 10 along with its count. 13 | 14 | Note: 15 | - The input should be obtained from the standard input (`&0`). 16 | 17 | ## Sample Input 18 | 19 | ``` 20 | A B C D A E F G H H H A A B I J K L A X K Y Y Y Y Y Y Y G G G A Y A I I I 21 | L L X X I G 22 | ``` 23 | 24 | ## Sample Output 25 | 26 | ``` 27 | l 28 | h 29 | k 30 | b 31 | j 32 | ``` 33 | 34 | ## Hint 35 | - Refer to `tr`, `sort`, `uniq` commands for help. 36 | 37 | 38 | -------------------------------------------------------------------------------- /oppe-practice-12/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 12 2 | 3 | ## Problem Statement 4 | 5 | Write a script `script.sh` which will extract the values of color and name of color from the **standard input**. 6 | 7 | A color is a string which is in the format of "name of color": [r, g, b, a] where r, g, b, a are integers. 8 | 9 | ## Sample Input 10 | 11 | ```html 12 | "aliceblue": [240, 248, 255, 1], 13 | ``` 14 | 15 | ## Sample Output 16 | 17 | ``` 18 | "aliceblue":[240,248,255,1] 19 | ``` 20 | -------------------------------------------------------------------------------- /oppe-practice-2/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 2 2 | 3 | ## Problem Statement 4 | 5 | Write a script `script.sh` to create a link named `storage` in the current directory to the directory whose path is stored in the variable `DIR_PATH` 6 | -------------------------------------------------------------------------------- /oppe-practice-3/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 3 2 | 3 | ## Problem Statement 4 | 5 | Write a function `parent-of-parent` in the script `script.sh` to return the parent of parent directory of the file/directory that is passed as an argument. 6 | 7 | Note: Parent of `/` is `/`. 8 | -------------------------------------------------------------------------------- /oppe-practice-4/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 4 2 | 3 | ## Problem Statement 4 | 5 | The poem "Sail away" by Rabindranath Tagore is provided through the standard input (`&0`). 6 | 7 | ``` text 8 | Early in the day it was whispered that we should sail in a boat, 9 | only thou and I, and never a soul in the world would know of this our 10 | pilgrimage to no country and to no end. 11 | 12 | In that shoreless ocean, 13 | at thy silently listening smile my songs would swell in melodies, 14 | free as waves, free from all bondage of words. 15 | 16 | Is the time not come yet? 17 | Are there works still to do? 18 | Lo, the evening has come down upon the shore 19 | and in the fading light the seabirds come flying to their nests. 20 | 21 | Who knows when the chains will be off, 22 | and the boat, like the last glimmer of sunset, 23 | vanish into the night? 24 | ``` 25 | 26 | Write a script `script.sh` to print the number of non-empty lines that does not contain an article (a, an, the) in it. 27 | The command should print a number that is the count of lines, and should not print the lines. 28 | -------------------------------------------------------------------------------- /oppe-practice-5/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 5 2 | 3 | ## Problem Statement 4 | 5 | Write a script to verify the exact word "System" or "Commands" (case-sensitive) exist in the standard input. 6 | If any of the words are present in the file, print `0` else `1`. Do not print anything else. 7 | Note that the words may occur on separate lines as well. 8 | -------------------------------------------------------------------------------- /oppe-practice-6/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 6 2 | 3 | ## Problem Statement 4 | 5 | Write a script `script.sh` to extract and print the link of all anchor tag present in the standard input. 6 | 7 | Assume all `a` tags start and end in the same line. 8 | 9 | Example: In the given text 10 | 11 | ```html 12 | IITM BS Degree Programme 13 | ``` 14 | 15 | `https://onlinedegree.iitm.ac.in/` is the link to the site. 16 | 17 | Note: Assume the starting `a` tag and `href` attribute value will be on the same line. 18 | -------------------------------------------------------------------------------- /oppe-practice-7/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 7 2 | 3 | ## Problem Statement 4 | 5 | Error Handling requires one to understand exit status implementation. 6 | 7 | Write a script `script.sh` which will echo "Success" if there are `3` arguments passed to it and print all the three arguments. 8 | If there are more or less arguments, the exit status should be `1`. 9 | 10 | ## Sample Example 11 | 12 | ```bash 13 | > ./script hello how are you? 14 | > echo $? 15 | 1 16 | > ./script comment ca va? 17 | Success comment ca va? 18 | > echo $? 19 | 0 20 | ``` 21 | -------------------------------------------------------------------------------- /oppe-practice-8/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 8 2 | 3 | ## Problem Statement 4 | 5 | Sanjay is a professional photographer who capture photos of items for various clients. The photos database has thousands of photos collected each year. The photographs are stored in jpg format using `YYYYMMDD_HHMMSS.jpg` naming convention. For his backup purpose he wants to organise his data by moving the photos to a new directory created using `monthYear` (Ex: Jan2021) format (thus 12 directory for each year). 6 | 7 | Write a shell script `script.sh` which will create folders based on the image name and creates and moves it to the respective month directory. 8 | 9 | Do not create empty folders. 10 | 11 | **Hint:** 12 | - Refer `date --help` or `man date` to get the format of month and year. 13 | 14 | ## Sample Input 15 | 16 | These files are present in your script's current working directory. 17 | 18 | ``` 19 | 20110313_024459.jpg 20 | 20110207_010404.jpg 21 | 20110812_222616.jpg 22 | 20110514_113934.jpg 23 | ``` 24 | 25 | ## Sample Output 26 | 27 | These folder structure are supposed to be created by your script. Your script should not output anything to the screen. 28 | 29 | ``` 30 | ./Aug2011/20110812_222616.jpg 31 | ./Feb2011/20110207_010404.jpg 32 | ./Mar2011/20110313_024459.jpg 33 | ./May2011/20110514_113934.jpg 34 | ``` 35 | -------------------------------------------------------------------------------- /oppe-practice-9/readme.md: -------------------------------------------------------------------------------- 1 | # OPPE Practice 9 2 | 3 | ## Problem Statement 4 | 5 | Write a bash script `script.sh` which works on output of `ls -li` to produce an output which will have unique list of files which has no softlinks and has only one file for multiple hardlinks present. 6 | 7 | Note: The final output should be sorted by ascending order of inode number. 8 | 9 | Hint: Output of the `ls -li` is given as standard input (`&0`). Do not run `ls -li` yourself. 10 | 11 | ## Sample Input 12 | 13 | ```bash 14 | 12 -rwxr-xr-x 1 root root 1113504 Apr 18 2022 bash 15 | 13 -rwxr-xr-x 1 root root 716464 Mar 13 2018 btrfs 16 | 14 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-debug-tree 17 | 15 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-find-root 18 | 16 -rwxr-xr-x 1 root root 396432 Mar 13 2018 btrfs-image 19 | 17 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-map-logical 20 | 18 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-select-super 21 | 19 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-zero-log 22 | 20 lrwxrwxrwx 1 root root 5 Mar 13 2018 btrfsck -> btrfs 23 | 21 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfstune 24 | 22 -rwxr-xr-x 3 root root 34888 Jul 4 2019 bunzip2 25 | 23 -rwxr-xr-x 1 root root 2062296 Nov 25 2021 busybox 26 | 22 -rwxr-xr-x 3 root root 34888 Jul 4 2019 bzcat 27 | 24 lrwxrwxrwx 1 root root 6 Jul 4 2019 bzcmp -> bzdiff 28 | 25 -rwxr-xr-x 1 root root 2140 Jul 4 2019 bzdiff 29 | 26 lrwxrwxrwx 1 root root 6 Jul 4 2019 bzegrep -> bzgrep 30 | 27 -rwxr-xr-x 1 root root 4877 Jul 4 2019 bzexe 31 | 28 lrwxrwxrwx 1 root root 6 Jul 4 2019 bzfgrep -> bzgrep 32 | 29 -rwxr-xr-x 1 root root 3642 Jul 4 2019 bzgrep 33 | ``` 34 | 35 | ## Sample Output 36 | 37 | ``` 38 | 12 -rwxr-xr-x 1 root root 1113504 Apr 18 2022 bash 39 | 13 -rwxr-xr-x 1 root root 716464 Mar 13 2018 btrfs 40 | 14 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-debug-tree 41 | 15 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-find-root 42 | 16 -rwxr-xr-x 1 root root 396432 Mar 13 2018 btrfs-image 43 | 17 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfs-map-logical 44 | 18 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-select-super 45 | 19 -rwxr-xr-x 1 root root 371856 Mar 13 2018 btrfs-zero-log 46 | 21 -rwxr-xr-x 1 root root 375952 Mar 13 2018 btrfstune 47 | 22 -rwxr-xr-x 3 root root 34888 Jul 4 2019 bunzip2 48 | 23 -rwxr-xr-x 1 root root 2062296 Nov 25 2021 busybox 49 | 25 -rwxr-xr-x 1 root root 2140 Jul 4 2019 bzdiff 50 | 27 -rwxr-xr-x 1 root root 4877 Jul 4 2019 bzexe 51 | 29 -rwxr-xr-x 1 root root 3642 Jul 4 2019 bzgrep 52 | ``` 53 | -------------------------------------------------------------------------------- /ppa_1.1/movetext.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mv *.txt level1/ 4 | -------------------------------------------------------------------------------- /ppa_1.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 1 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | Write a bash command to move all the `.txt` files present in the current directory to the directory named `level1` present inside the current directory. 6 | 7 | ## Instructions 8 | 9 | Do not move any other files other than `.txt` files anywhere from the current directory. 10 | 11 | Write only a single line bash command to perform the above task. 12 | 13 | The script should not produce any output. 14 | 15 | The script will not receive any input from arguments or standard input. 16 | 17 | The command should be saved in the file `movetext.sh` 18 | 19 | ## Sample Input 20 | 21 | Assume the following output of `tree` command in the current directory. 22 | 23 | ``` 24 | ./ 25 | ├── abc.txt 26 | ├── level1/ 27 | ├── test.c 28 | ├── test.cpp 29 | ├── test.java 30 | ├── test.py 31 | └── test.txt 32 | ``` 33 | 34 | ## Sample Output 35 | 36 | After running your `movetext.sh`, the output of `tree` should be as follows. 37 | 38 | ``` 39 | ./ 40 | ├── level1/ 41 | │   ├── abc.txt 42 | │   └── test.txt 43 | ├── test.c 44 | ├── test.cpp 45 | ├── test.java 46 | └── test.py 47 | ``` -------------------------------------------------------------------------------- /ppa_1.2/filetypes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | file * -------------------------------------------------------------------------------- /ppa_1.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 1 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | Write a Bash command to print the file types of all the files stored in the current working directory. 6 | 7 | ## Instructions 8 | 9 | The output should be in the format as described below. 10 | The first column is file/directory name and the second column tells about the file type. 11 | Your script `filetypes.sh` will not be provided any input as arguments or standard input. 12 | 13 | ## Sample Input 14 | 15 | Assume the current working directory contains the following files and directories: 16 | 17 | ``` 18 | file.txt 19 | Notes/ 20 | SysComm -> ../SysComm Dropbox 21 | System Commands Sample Questions.md 22 | System Commands Sample Questions.pdf 23 | ``` 24 | 25 | ## Sample Output 26 | 27 | The output of your script should be as follows: 28 | 29 | ``` 30 | file.txt: empty 31 | Notes: directory 32 | SysComm: symbolic link to ../SysComm Dropbox 33 | System Commands Sample Questions.md: UTF-8 Unicode text, with very long lines 34 | System Commands Sample Questions.pdf: PDF document, version 1.7 35 | ``` 36 | -------------------------------------------------------------------------------- /ppa_2.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 2 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | Create a file `documents.txt` containing all the possible file names in the format `file_XYZ.txt` where X is a lower case alphabet, Y is also a lower case alphabet and Z is a number between 0 and 4. 6 | 7 | Hint: Use echo to solve this with a single command. 8 | 9 | ## Instructions 10 | 11 | The file names in `documents.txt` should be separated by a single space. 12 | Few examples of file names in this format are `file_dh3.txt`, `file_sd1.txt`, `file_ja0.txt`, `file_at2.txt`. 13 | You do not need to create any script file for this. You can directly run the command in the terminal to create the file. 14 | After the file is created you should run `synchro eval` to submit your solution. 15 | -------------------------------------------------------------------------------- /ppa_2.1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | { 3 | for i in {a..z}; do 4 | for j in {a..z}; do 5 | for k in {0..4}; do 6 | echo "file_${i}${j}${k}.txt" 7 | done 8 | done 9 | done 10 | } > documents.txt -------------------------------------------------------------------------------- /ppa_2.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 2 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | `encoding-key` is a file located at the path `encryption/two-level/binary/positive-offset/`. 6 | The file encoding-key is updated often and shared between multiple users. 7 | This file is important to you, and you are worried that the file could be deleted by mistake. 8 | Create a file `ek` in the current working directory, such that it is always in sync with the contents of file `encoding-key` and if `encoding-key` gets deleted by any chance the content in it should be available in file `ek`. 9 | 10 | ## Instructions 11 | 12 | Directory `encryption` is located in current working directory. 13 | Write your code in the file `safety.sh`. It will not receive any input in arguments or from standard input. 14 | Your code should not produce any output. 15 | 16 | **NOTE**: The folder `encryption` will be automatically created and present before your `safety.sh` is run during evaluation. 17 | -------------------------------------------------------------------------------- /ppa_2.2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Create a hard link to the encoding-key file 4 | ln encryption/two-level/binary/positive-offset/encoding-key ek 5 | -------------------------------------------------------------------------------- /ppa_3.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 3 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | List all the `.txt` files in the current working directory and redirect the output to a file named `textFiles.txt` and also print 'found' to the terminal (without quotes, do not print anything else). 6 | 7 | If no `.txt` file exists redirect the error of your command to the file `noFiles.txt` and do not print anything. 8 | 9 | ## Instructions 10 | 11 | Hint: Make use of redirection to file and operators to write solution in one line. 12 | 13 | Write your code in the file `texts.sh`. 14 | -------------------------------------------------------------------------------- /ppa_3.1/texts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ls -l *.txt > textFiles.txt 2>noFiles.txt && echo found 3 | -------------------------------------------------------------------------------- /ppa_3.2/calender.sh: -------------------------------------------------------------------------------- 1 | touch "$month.txt" 2 | touch error.txt 3 | cal "$month" > "$month.txt" 2> error.txt || > "$month.txt" 4 | -------------------------------------------------------------------------------- /ppa_3.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 3 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | Given a shell variable month supposed to contain a string value corresponding to some calendar month of 2023. Use the `cal` command to create a file named as `X.txt` where `X` is the string value in the variable month. Your command should also create a file named `error.txt` that should contain the error message if the string month does not correspond to any calendar month. Create all the files in the current working directory. 6 | 7 | For example: 8 | 9 | If the variable month contains the string "nov", your solution should create a file named `nov.txt` containing the calendar of November month and `error.txt` should be empty. 10 | And if the variable month contains the string "garbage", your solution should create a file named `error.txt` containing the error from `cal` command and `garbage.txt` should be empty. 11 | 12 | ## Instructions 13 | 14 | Write your code in a file called `calendar.sh`. The variable `month` will be present as a variable in the environment. 15 | 16 | ## Sample Input 17 | 18 | ``` 19 | sep 20 | ``` 21 | 22 | ## Sample Output 23 | 24 | `sep.txt`: 25 | 26 | ``` 27 | September 2023 28 | Su Mo Tu We Th Fr Sa 29 | 1 2 30 | 3 4 5 6 7 8 9 31 | 10 11 12 13 14 15 16 32 | 17 18 19 20 21 22 23 33 | 24 25 26 27 28 29 30 34 | ``` 35 | 36 | `error.txt` is empty. 37 | -------------------------------------------------------------------------------- /ppa_3.3/errorlog.sh: -------------------------------------------------------------------------------- 1 | test 2> errorlog 2 | test -e 2>> errorlog 3 | test -n 2>> errorlog -------------------------------------------------------------------------------- /ppa_3.3/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 3 - Question 3 2 | 3 | ## Problem Statement 4 | 5 | Execute the commands given below in the sequence and collect the output/error into a file `errorlog` as described below. 6 | 7 | - Execute the command `test` and redirect the standard error to the file `errorlog`. 8 | - Execute the command `test -e` and append the standard error output to the file `errorlog`. 9 | - Execute the command `test -n` and append the standard error to the file `errorlog`. 10 | 11 | ## Instructions 12 | 13 | Write your code in the file `errorlog.sh`. Your script will not receive any input from the command line or standard input. 14 | Your script should not print anything else other than the output/error as described above. 15 | The `test` command can output to the stdout or stderr. You should redirect only the stderr to the file `errorlog`. 16 | -------------------------------------------------------------------------------- /ppa_4.1/dirs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ls -l | grep -w 'd.*rwx' | awk '{print $9}' -------------------------------------------------------------------------------- /ppa_4.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 4 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | Write a command to print the name of directories(in the current working directory) that have read, write and execute permissions for other users. 6 | Print only the directory name on each line. 7 | 8 | ## Instructions 9 | 10 | Your command should be stored in a file named `dirs.sh`. 11 | The script will not recieve any input from the arguments or standard input. 12 | The script should print the output to the standard output. 13 | 14 | ## Sample Input 15 | 16 | If the current working directory has the following directories: 17 | ```bash 18 | $ ls -l 19 | drwxrwxrwx 2 user user 4096 Sep 1 12:00 dir1 20 | drwxrwxr-x 2 user user 4096 Sep 1 12:00 dir2 21 | drwxr-xrwx 2 user user 4096 Sep 1 12:00 dir3 22 | ``` 23 | 24 | ## Sample Output 25 | 26 | The output of the command should be: 27 | ```bash 28 | dir1 29 | dir3 30 | ``` 31 | 32 | ## Constraints 33 | 34 | Directory names will not have spaces in them. 35 | -------------------------------------------------------------------------------- /ppa_4.2/pincode.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | # NOT WORKING 5 | while IFS=',' read -r circle region division office pincode office_type delivery district state; do 6 | if [[ "$pincode" == "Pincode" ]]; then 7 | continue 8 | fi 9 | if [[ "$pincode" == "$pin" ]]; then 10 | echo "$circle $division" 11 | fi 12 | done < Pincode_info.csv 13 | 14 | 15 | -------------------------------------------------------------------------------- /ppa_4.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 4 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | The file `Pincode_info.csv` has information on the pin codes of some places. 6 | The output of the command `head -5 Pincode_info.csv` is given below. 7 | First line of this file gives the information about the sequence of fields in each line of file following it. 8 | 9 | ```csv 10 | Circle Name,Region Name,Division Name,Office Name,Pincode,OfficeType,Delivery,District,StateName 11 | Andhra Pradesh Circle,Kurnool Region,Anantapur Division,A Narayanapuram B.O,515004,BO,Delivery,ANANTHAPUR,Andhra Pradesh 12 | Andhra Pradesh Circle,Kurnool Region,Anantapur Division,Akuledu B.O,515731,BO,Delivery,ANANTHAPUR,Andhra Pradesh 13 | Andhra Pradesh Circle,Kurnool Region,Anantapur Division,Alamuru B.O,515002,BO,Delivery,ANANTHAPUR,Andhra Pradesh 14 | Andhra Pradesh Circle,Kurnool Region,Anantapur Division,Allapuram B.O,515766,BO,Delivery,ANANTHAPUR,Andhra Pradesh 15 | ``` 16 | 17 | ## Instructions 18 | 19 | Write a command to display the Circle name and Division name separated by space for the given pincode stored in a shell variable 'pin'. 20 | Note: If your solution has more than one line, add a semicolon after each line. 21 | 22 | Write your command in the file `pincode.sh`. 23 | The script will not recieve any input through STDIN or the arguments. 24 | The pincode should be stored in an environment variable named `pin` accessible by the script. 25 | The script should output the Circle name and Division name separated by space to the STDOUT. 26 | 27 | ## Sample Input 28 | 29 | For e.g. if `pin=515002` then your command should display: 30 | 31 | ## Sample Output 32 | 33 | ```bash 34 | Andhra Pradesh Anantapur 35 | ``` 36 | -------------------------------------------------------------------------------- /ppa_5.1/oddargs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in "$@"; do 4 | if (( (i-1) % 2 == 0 )); then 5 | echo "$i" 6 | fi 7 | done 8 | -------------------------------------------------------------------------------- /ppa_5.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | Write a bash script that accepts any number of arguments and print the odd numbered arguments, i.e. first argument(\$1), third argument(\$3), fifth argument(\$5) and so on. 6 | 7 | ## Instructions 8 | 9 | In the output the values of the arguments should be separated by a space and printed on the same line. 10 | 11 | The input will be provided to your script `oddargs.sh` as command line arguments. 12 | The output of your script should be printed to the standard output (stdout). 13 | 14 | ## Sample Input 15 | 16 | ```bash 17 | 1 2 3 4 5 6 7 8 9 10 18 | ``` 19 | 20 | ## Sample Output 21 | 22 | ```bash 23 | 1 3 5 7 9 24 | ``` 25 | 26 | ## Constraints 27 | 28 | Arguments may contain spaces. 29 | -------------------------------------------------------------------------------- /ppa_5.2/investment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sum=0 4 | 5 | while read -r hash; do 6 | filename=$(grep "$hash" map | cut -d' ' -f2) 7 | amount=$(grep "INVESTMENT" "$filename" | cut -d'$' -f2) 8 | sum=$((sum + amount)) 9 | done < result 10 | 11 | echo $sum 12 | -------------------------------------------------------------------------------- /ppa_5.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | A finance company called `Fintech` does not have its own analytics team, rather they outsources their analytics work to external vendors. 6 | The company maintains a file for each investor they are interested in, with the filename in the format `firstname_lastname` in the directory `data` (directory `data` is in the current working directory). 7 | They do not want to make their investor details public so they rename each investor file to the hash value of filename before giving it to the external vendors and store the mapping in the file named `map`. 8 | Assume that no two investors have same combination of `firstname` and `lastname`. 9 | 10 | The analytics team(external vendor) received the files of several investors named as some hash value. 11 | They analysed and identified some potential investors for the company and stored the hashed file names(one on each line) corresponding to the identified investors in a file named `result`. 12 | The `firstname_lastname` file contains the potential investment amount and duration details. 13 | Refer the provided sample data for sample file formats and architecture. 14 | 15 | ### Architecture and Sample Data 16 | 17 | ```bash 18 | $ ls -Rl 19 | .: 20 | total 4 21 | drwxrwxrwx 1 user user 512 Dec 11 19:48 data 22 | -rwxrwxrwx 1 user user 435 Dec 11 20:53 map 23 | -rwxrwxrwx 1 user user 195 Dec 11 20:53 result 24 | 25 | ./data: 26 | total 0 27 | -rwxrwxrwx 1 user user 36 Dec 11 20:53 Billie_Barron 28 | -rwxrwxrwx 1 user user 36 Dec 11 20:53 Jeremiah_Brennan 29 | -rwxrwxrwx 1 user user 36 Dec 11 20:53 Long_Mclaughlin 30 | -rwxrwxrwx 1 user user 35 Dec 11 20:53 Lorna_Trevino 31 | -rwxrwxrwx 1 user user 35 Dec 11 20:53 Mandy_Mueller 32 | 33 | $ cat ./data/Billie_Barron 34 | INVESTMENT $16319 35 | FROM 2026 36 | TO 2026 37 | 38 | $ cat map 39 | bb1cc74d6e8c40efdbbbc0e6a657fca02a533fe7f438d5b09b47b43e31cb9a45 ./data/Mandy_Mueller 40 | fe818c4ac047523ac14dbb341f365bbfcd857268a6bfb70d9abb701a80bfb9c3 ./data/Lorna_Trevino 41 | e8b80161e4110a791a0d7c3e40a04099fc75f0e348c2033efe79a2a930a71e98 ./data/Long_Mclaughlin 42 | b1b1222a15fcd532e511d0f461dbe7ae7bda825b68bc510eec2a22cbddd5dad2 ./data/Billie_Barron 43 | 6051c4f27079a41afc99a97f0fc7bb8ba2789cd282f4cd10a71c6a954089b63e ./data/Jeremiah_Brennan 44 | 45 | $ cat result 46 | bb1cc74d6e8c40efdbbbc0e6a657fca02a533fe7f438d5b09b47b43e31cb9a45 47 | fe818c4ac047523ac14dbb341f365bbfcd857268a6bfb70d9abb701a80bfb9c3 48 | e8b80161e4110a791a0d7c3e40a04099fc75f0e348c2033efe79a2a930a71e98 49 | 50 | $ grep INVESTMENT -r 51 | data/Billie_Barron:INVESTMENT $16319 52 | data/Jeremiah_Brennan:INVESTMENT $29440 53 | data/Long_Mclaughlin:INVESTMENT $25906 54 | data/Lorna_Trevino:INVESTMENT $1360 55 | data/Mandy_Mueller:INVESTMENT $7979 56 | ``` 57 | 58 | ### Expected output for above data 59 | 60 | 35245 61 | 62 | ## Instructions 63 | 64 | Write a bash script to print the sum of investment amounts of all the investors identified by the analytics team whose names(hash values) are present in the file `result`. 65 | 66 | Store your commands in the file `investment.sh`. 67 | Your script will not recieve any input through standard input, or as arguments. 68 | Your script should print output to the standard output. 69 | -------------------------------------------------------------------------------- /ppa_5.3/long.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $(wc -l < data.txt) -gt 16 ]; then 4 | echo "Yes" 5 | else 6 | echo "No" 7 | fi 8 | -------------------------------------------------------------------------------- /ppa_5.3/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 3 2 | 3 | ## Problem Statement 4 | 5 | Consider a file named `data.txt` in the current working directory. 6 | Write a script to determine if this file has more than $16$ lines or not. 7 | Your script should print `Yes` if the lines are more than $16$, else print `No`. 8 | 9 | ## Instructions 10 | 11 | Write your script in `long.sh` file. 12 | Your script will not recieve any input through standard input or arguments. 13 | Your script should output to the standard output. 14 | 15 | ## Sample Input 16 | 17 | ```bash 18 | 1 19 | 2 20 | 3 21 | 4 22 | 5 23 | 6 24 | 7 25 | 8 26 | 9 27 | 10 28 | 11 29 | 12 30 | 13 31 | 14 32 | 15 33 | 16 34 | 17 35 | 18 36 | ``` 37 | 38 | ## Sample Output 39 | 40 | ```bash 41 | Yes 42 | ``` 43 | -------------------------------------------------------------------------------- /ppa_5.4/arch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | uname -a | rev | cut -d' ' -f1 | rev -------------------------------------------------------------------------------- /ppa_5.4/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 4 2 | 3 | ## Problem Statement 4 | 5 | Write a command to extract the system's processor architecture from the output of `uname -a`. 6 | The output should be something like 'arm64', 'x86_64' etc. 7 | 8 | ## Instructions 9 | 10 | Write your command in the file `arch.sh`. 11 | Your script will not receive any input through standard input or command line arguments. 12 | Your script should output to the standard output. 13 | 14 | ## Sample Output 15 | 16 | ```bash 17 | x86_64 18 | ``` 19 | -------------------------------------------------------------------------------- /ppa_5.5/fail.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -c "FAILED LOGIN" myauth.log 4 | -------------------------------------------------------------------------------- /ppa_5.5/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 5 2 | 3 | ## Problem Statement 4 | 5 | Write a Bash command to print the number of failed login attempts which are recorded in the file myauth.log located in the current working directory. 6 | 7 | Contents of myauth.log is given below. 8 | 9 | ```bash 10 | Jan 20 20:11:34 IITMBSC systemd-logind[897]: Session 27 logged out. Waiting for processes to exit. 11 | Jan 20 20:11:34 IITMBSC systemd-logind[897]: Removed session 27. 12 | Jan 20 20:17:01 IITMBSC CRON[70999]: pam_unix(cron:session): session opened for user root by (uid=0) 13 | Jan 20 20:17:01 IITMBSC CRON[70999]: pam_unix(cron:session): session closed for user root 14 | Jan 20 20:21:10 IITMBSC su: (to root) student on pts/4 15 | Jan 20 20:21:10 IITMBSC su: pam_unix(su:session): session opened for user root by student(uid=0) 16 | Jan 20 20:21:21 IITMBSC su: pam_unix(su:session): session closed for user root 17 | Jan 20 20:21:21 IITMBSC sudo: pam_unix(sudo:session): session closed for user root 18 | Jan 20 20:22:02 IITMBSC gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session opened for user gdm by (uid=0) 19 | Jan 20 20:22:02 IITMBSC systemd-logind[897]: New session c2 of user gdm. 20 | Jan 20 20:22:02 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user gdm by (uid=0) 21 | Jan 20 20:22:03 IITMBSC polkitd(authority=local): Registered Authentication Agent for unix-session:c2 (system bus name :1.357 [/usr/bin/gnome-shell], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) 22 | Jan 20 20:22:21 IITMBSC gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session closed for user gdm 23 | Jan 20 20:22:21 IITMBSC systemd-logind[897]: Session c2 logged out. Waiting for processes to exit. 24 | Jan 20 20:22:22 IITMBSC systemd-logind[897]: Removed session c2. 25 | Jan 20 20:22:22 IITMBSC polkitd(authority=local): Unregistered Authentication Agent for unix-session:c2 (system bus name :1.357, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus) 26 | Jan 20 20:22:53 IITMBSC login[70867]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty3 ruser= rhost= user=guest 27 | Jan 20 20:22:56 IITMBSC login[70867]: FAILED LOGIN (1) on '/dev/tty3' FOR 'guest', Authentication failure 28 | Jan 20 20:23:01 IITMBSC login[70550]: pam_unix(login:session): session closed for user guest2 29 | Jan 20 20:23:01 IITMBSC systemd-logind[897]: Session 25 logged out. Waiting for processes to exit. 30 | Jan 20 20:23:01 IITMBSC systemd-logind[897]: Removed session 25. 31 | Jan 20 20:23:18 IITMBSC login[71620]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=guest2 32 | Jan 20 20:23:20 IITMBSC login[71620]: FAILED LOGIN (1) on '/dev/tty4' FOR 'guest2', Authentication failure 33 | Jan 20 20:23:27 IITMBSC login[71620]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=root 34 | Jan 20 20:23:30 IITMBSC login[71620]: FAILED LOGIN (2) on '/dev/tty4' FOR 'root', Authentication failure 35 | Jan 20 20:23:39 IITMBSC login[71620]: pam_unix(login:auth): check pass; user unknown 36 | Jan 20 20:23:39 IITMBSC login[71620]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= 37 | Jan 20 20:23:43 IITMBSC login[71620]: FAILED LOGIN (3) on '/dev/tty4' FOR 'UNKNOWN', Authentication failure 38 | Jan 20 20:23:48 IITMBSC login[71620]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 39 | Jan 20 20:23:48 IITMBSC systemd-logind[897]: New session 30 of user guest. 40 | Jan 20 20:23:48 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest by (uid=0) 41 | Jan 20 20:23:51 IITMBSC login[71620]: pam_unix(login:session): session closed for user guest 42 | Jan 20 20:23:51 IITMBSC systemd-logind[897]: Session 30 logged out. Waiting for processes to exit. 43 | Jan 20 20:23:51 IITMBSC systemd-logind[897]: Removed session 30. 44 | Jan 20 20:24:01 IITMBSC login[71803]: pam_unix(login:session): session opened for user student by LOGIN(uid=0) 45 | Jan 20 20:24:01 IITMBSC systemd-logind[897]: New session 32 of user student. 46 | Jan 20 20:24:02 IITMBSC login[71803]: pam_unix(login:session): session closed for user student 47 | Jan 20 20:24:02 IITMBSC systemd-logind[897]: Session 32 logged out. Waiting for processes to exit. 48 | Jan 20 20:24:02 IITMBSC systemd-logind[897]: Removed session 32. 49 | Jan 20 20:24:06 IITMBSC login[71900]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=root 50 | Jan 20 20:24:09 IITMBSC login[71900]: FAILED LOGIN (1) on '/dev/tty4' FOR 'root', Authentication failure 51 | Jan 20 20:24:16 IITMBSC login[71900]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 52 | Jan 20 20:24:16 IITMBSC systemd-logind[897]: New session 33 of user guest. 53 | Jan 20 20:24:16 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest by (uid=0) 54 | Jan 20 20:24:17 IITMBSC login[71900]: pam_unix(login:session): session closed for user guest 55 | Jan 20 20:24:17 IITMBSC systemd-logind[897]: Session 33 logged out. Waiting for processes to exit. 56 | Jan 20 20:24:17 IITMBSC systemd-logind[897]: Removed session 33. 57 | Jan 20 20:24:22 IITMBSC login[72064]: pam_unix(login:session): session opened for user guest2 by LOGIN(uid=0) 58 | Jan 20 20:24:22 IITMBSC systemd-logind[897]: New session 35 of user guest2. 59 | Jan 20 20:24:22 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest2 by (uid=0) 60 | Jan 20 20:24:24 IITMBSC login[72064]: pam_unix(login:session): session closed for user guest2 61 | Jan 20 20:24:24 IITMBSC systemd-logind[897]: Session 35 logged out. Waiting for processes to exit. 62 | Jan 20 20:24:24 IITMBSC systemd-logind[897]: Removed session 35. 63 | Jan 20 20:24:27 IITMBSC agetty[72229]: tty4: invalid character 0x1b in login name 64 | Jan 20 20:24:43 IITMBSC login[72242]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=guest2 65 | Jan 20 20:24:47 IITMBSC login[72242]: FAILED LOGIN (1) on '/dev/tty4' FOR 'guest2', Authentication failure 66 | Jan 20 20:24:48 IITMBSC login[72242]: pam_nologin(login:auth): unexpected response from failed conversation function 67 | Jan 20 20:24:48 IITMBSC login[72242]: pam_nologin(login:auth): cannot determine username 68 | Jan 20 20:24:51 IITMBSC login[72242]: FAILED LOGIN (2) on '/dev/tty4' FOR 'UNKNOWN', User not known to the underlying authentication module 69 | ``` 70 | 71 | ## Instructions 72 | 73 | Write your command in the `fail.sh` file. 74 | Your script will not receive any input from the standard input or command line arguments. 75 | Your script should output to the standard output. 76 | 77 | ## Sample Output 78 | 79 | ```bash 80 | 7 81 | ``` 82 | -------------------------------------------------------------------------------- /ppa_5.6/login.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -o 'user [A-Za-z1-9]*\b' myauth.log | cut -d' ' -f2 | grep -vE '^(root|unknown)$' | sort | uniq 4 | -------------------------------------------------------------------------------- /ppa_5.6/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 6 2 | 3 | ## Problem Statement 4 | 5 | Write a script to print the users(one on each line) who are logged in successfully. 6 | Extract the information from the file named `myauth.log` located in the current working directory. 7 | The output should contain usernames only and should be unique. 8 | 9 | Hint: Use uniq command to get all distinct lines of the output. 10 | 11 | Contents of myauth.log is given below 12 | 13 | ``` 14 | Jan 20 20:11:34 IITMBSC systemd-logind[897]: Session 27 logged out. Waiting for processes to exit. 15 | Jan 20 20:11:34 IITMBSC systemd-logind[897]: Removed session 27. 16 | Jan 20 20:17:01 IITMBSC CRON[70999]: pam_unix(cron:session): session opened for user root by (uid=0) 17 | Jan 20 20:17:01 IITMBSC CRON[70999]: pam_unix(cron:session): session closed for user root 18 | Jan 20 20:21:10 IITMBSC su: (to root) student on pts/4 19 | Jan 20 20:21:10 IITMBSC su: pam_unix(su:session): session opened for user root by student(uid=0) 20 | Jan 20 20:21:21 IITMBSC su: pam_unix(su:session): session closed for user root 21 | Jan 20 20:21:21 IITMBSC sudo: pam_unix(sudo:session): session closed for user root 22 | Jan 20 20:22:02 IITMBSC gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session opened for user gdm by (uid=0) 23 | Jan 20 20:22:02 IITMBSC systemd-logind[897]: New session c2 of user gdm. 24 | Jan 20 20:22:02 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user gdm by (uid=0) 25 | Jan 20 20:22:03 IITMBSC polkitd(authority=local): Registered Authentication Agent for unix-session:c2 (system bus name :1.357 [/usr/bin/gnome-shell], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) 26 | Jan 20 20:22:21 IITMBSC gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session closed for user gdm 27 | Jan 20 20:22:21 IITMBSC systemd-logind[897]: Session c2 logged out. Waiting for processes to exit. 28 | Jan 20 20:22:22 IITMBSC systemd-logind[897]: Removed session c2. 29 | Jan 20 20:22:22 IITMBSC polkitd(authority=local): Unregistered Authentication Agent for unix-session:c2 (system bus name :1.357, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus) 30 | Jan 20 20:22:53 IITMBSC login[70867]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty3 ruser= rhost= user=guest 31 | Jan 20 20:22:56 IITMBSC login[70867]: FAILED LOGIN (1) on '/dev/tty3' FOR 'guest', Authentication failure 32 | Jan 20 20:23:01 IITMBSC login[70550]: pam_unix(login:session): session closed for user guest2 33 | Jan 20 20:23:01 IITMBSC systemd-logind[897]: Session 25 logged out. Waiting for processes to exit. 34 | Jan 20 20:23:01 IITMBSC systemd-logind[897]: Removed session 25. 35 | Jan 20 20:23:18 IITMBSC login[71620]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=guest2 36 | Jan 20 20:23:20 IITMBSC login[71620]: FAILED LOGIN (1) on '/dev/tty4' FOR 'guest2', Authentication failure 37 | Jan 20 20:23:27 IITMBSC login[71620]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=root 38 | Jan 20 20:23:30 IITMBSC login[71620]: FAILED LOGIN (2) on '/dev/tty4' FOR 'root', Authentication failure 39 | Jan 20 20:23:39 IITMBSC login[71620]: pam_unix(login:auth): check pass; user unknown 40 | Jan 20 20:23:39 IITMBSC login[71620]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= 41 | Jan 20 20:23:43 IITMBSC login[71620]: FAILED LOGIN (3) on '/dev/tty4' FOR 'UNKNOWN', Authentication failure 42 | Jan 20 20:23:48 IITMBSC login[71620]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 43 | Jan 20 20:23:48 IITMBSC systemd-logind[897]: New session 30 of user guest. 44 | Jan 20 20:23:48 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest by (uid=0) 45 | Jan 20 20:23:51 IITMBSC login[71620]: pam_unix(login:session): session closed for user guest 46 | Jan 20 20:23:51 IITMBSC systemd-logind[897]: Session 30 logged out. Waiting for processes to exit. 47 | Jan 20 20:23:51 IITMBSC systemd-logind[897]: Removed session 30. 48 | Jan 20 20:24:01 IITMBSC login[71803]: pam_unix(login:session): session opened for user student by LOGIN(uid=0) 49 | Jan 20 20:24:01 IITMBSC systemd-logind[897]: New session 32 of user student. 50 | Jan 20 20:24:02 IITMBSC login[71803]: pam_unix(login:session): session closed for user student 51 | Jan 20 20:24:02 IITMBSC systemd-logind[897]: Session 32 logged out. Waiting for processes to exit. 52 | Jan 20 20:24:02 IITMBSC systemd-logind[897]: Removed session 32. 53 | Jan 20 20:24:06 IITMBSC login[71900]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=root 54 | Jan 20 20:24:09 IITMBSC login[71900]: FAILED LOGIN (1) on '/dev/tty4' FOR 'root', Authentication failure 55 | Jan 20 20:24:16 IITMBSC login[71900]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 56 | Jan 20 20:24:16 IITMBSC systemd-logind[897]: New session 33 of user guest. 57 | Jan 20 20:24:16 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest by (uid=0) 58 | Jan 20 20:24:17 IITMBSC login[71900]: pam_unix(login:session): session closed for user guest 59 | Jan 20 20:24:17 IITMBSC systemd-logind[897]: Session 33 logged out. Waiting for processes to exit. 60 | Jan 20 20:24:17 IITMBSC systemd-logind[897]: Removed session 33. 61 | Jan 20 20:24:22 IITMBSC login[72064]: pam_unix(login:session): session opened for user guest2 by LOGIN(uid=0) 62 | Jan 20 20:24:22 IITMBSC systemd-logind[897]: New session 35 of user guest2. 63 | Jan 20 20:24:22 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest2 by (uid=0) 64 | Jan 20 20:24:24 IITMBSC login[72064]: pam_unix(login:session): session closed for user guest2 65 | Jan 20 20:24:24 IITMBSC systemd-logind[897]: Session 35 logged out. Waiting for processes to exit. 66 | Jan 20 20:24:24 IITMBSC systemd-logind[897]: Removed session 35. 67 | Jan 20 20:24:27 IITMBSC agetty[72229]: tty4: invalid character 0x1b in login name 68 | Jan 20 20:24:43 IITMBSC login[72242]: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=/dev/tty4 ruser= rhost= user=guest2 69 | Jan 20 20:24:47 IITMBSC login[72242]: FAILED LOGIN (1) on '/dev/tty4' FOR 'guest2', Authentication failure 70 | Jan 20 20:24:48 IITMBSC login[72242]: pam_nologin(login:auth): unexpected response from failed conversation function 71 | Jan 20 20:24:48 IITMBSC login[72242]: pam_nologin(login:auth): cannot determine username 72 | Jan 20 20:24:51 IITMBSC login[72242]: FAILED LOGIN (2) on '/dev/tty4' FOR 'UNKNOWN', User not known to the underlying authentication module 73 | ``` 74 | 75 | ## Instructions 76 | 77 | Write your command in the file `login.sh`. 78 | Your script will not receive any input from the standard input or command line arguments. 79 | Your script should output to the standard output. 80 | 81 | ## Sample Output 82 | 83 | ```bash 84 | gdm 85 | guest 86 | guest2 87 | student 88 | ``` 89 | -------------------------------------------------------------------------------- /ppa_5.7/logintime.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep 'New session .* of user guest.$' myauth.log | sort -k1,1M -k2,2n -k3,3n | tail -n 1 | cut -d' ' -f1,2,3 4 | -------------------------------------------------------------------------------- /ppa_5.7/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 7 2 | 3 | ## Problem Statement 4 | 5 | Print the previous login time of the user `guest` in the format `MMM DD HH:MM:SS`. 6 | Where `MMM`, `DD`, `HH`, `MM` and `SS` corresponds to 7 | **Month** (E.g. Nov), **Date**, **Hours**, **Minutes** and **Seconds** respectively. 8 | Extract the information from the logs available in the file `myauth.log` in the current directory. 9 | 10 | Sample log file below. 11 | 12 | ## Sample Input 13 | 14 | ```text 15 | Jan 21 19:42:14 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 16 | Jan 21 19:42:17 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 17 | Jan 21 19:42:17 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 18 | Jan 21 19:42:20 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 19 | Jan 21 19:42:20 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 20 | Jan 21 19:42:21 IITMBSC CRON[109145]: pam_unix(cron:session): session opened for user root by (uid=0) 21 | Jan 21 19:42:21 IITMBSC CRON[109145]: pam_unix(cron:session): session closed for user root 22 | Jan 21 19:42:23 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 23 | Jan 21 19:42:23 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 24 | Jan 21 19:42:27 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 25 | Jan 21 19:42:27 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 26 | Jan 21 19:42:30 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 27 | Jan 21 19:42:30 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 28 | Jan 21 19:42:34 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 29 | Jan 21 19:42:34 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 30 | Jan 21 19:43:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 31 | Jan 21 19:43:21 IITMBSC pkexec[113702]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 32 | Jan 21 19:47:07 IITMBSC gnome-keyring-daemon[1856]: asked to register item /org/freedesktop/secrets/collection/login/2, but it's already registered 33 | Jan 21 19:48:44 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 34 | Jan 21 19:48:44 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 35 | Jan 21 19:49:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 36 | Jan 21 19:49:21 IITMBSC pkexec[115006]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 37 | Jan 21 19:51:56 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 38 | Jan 21 19:51:56 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 39 | Jan 21 19:51:59 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 40 | Jan 21 19:51:59 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 41 | Jan 21 19:52:02 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 42 | Jan 21 19:52:02 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 43 | Jan 21 19:52:06 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 44 | Jan 21 19:52:06 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 45 | Jan 21 19:52:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 46 | Jan 21 19:52:21 IITMBSC pkexec[118590]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 47 | Jan 21 20:07:21 IITMBSC gdm-password]: gkr-pam: unlocked login keyring 48 | Jan 21 20:09:24 IITMBSC login[29203]: pam_unix(login:session): session closed for user guest 49 | Jan 21 20:09:24 IITMBSC systemd-logind[966]: Session 16 logged out. Waiting for processes to exit. 50 | Jan 21 20:09:24 IITMBSC systemd-logind[966]: Removed session 16. 51 | Jan 21 20:09:28 IITMBSC login[119028]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 52 | Jan 21 20:09:28 IITMBSC systemd-logind[966]: New session 25 of user guest. 53 | Jan 21 20:10:51 IITMBSC login[119028]: pam_unix(login:session): session closed for user guest 54 | Jan 21 20:10:51 IITMBSC systemd-logind[966]: Session 25 logged out. Waiting for processes to exit. 55 | Jan 21 20:10:51 IITMBSC systemd-logind[966]: Removed session 25. 56 | Jan 21 20:10:54 IITMBSC login[119189]: pam_unix(login:session): session opened for user guest2 by LOGIN(uid=0) 57 | Jan 21 20:10:54 IITMBSC systemd-logind[966]: New session 26 of user guest2. 58 | Jan 21 20:10:54 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest2 by (uid=0) 59 | Jan 21 20:11:06 IITMBSC login[119189]: pam_unix(login:session): session closed for user guest2 60 | Jan 21 20:11:06 IITMBSC systemd-logind[966]: Session 26 logged out. Waiting for processes to exit. 61 | Jan 21 20:11:06 IITMBSC systemd-logind[966]: Removed session 26. 62 | Jan 21 20:12:17 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 63 | Jan 21 20:12:17 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 64 | Jan 21 20:12:20 IITMBSC su: pam_unix(su:auth): authentication failure; logname=student uid=1000 euid=0 tty=pts/7 ruser=student rhost= user=root 65 | Jan 21 20:12:22 IITMBSC su: FAILED SU (to root) student on pts/7 66 | Jan 21 20:12:23 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 67 | Jan 21 20:12:23 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 68 | Jan 21 20:12:30 IITMBSC su: (to guest) student on pts/7 69 | Jan 21 20:12:30 IITMBSC su: pam_unix(su:session): session opened for user guest by student(uid=1000) 70 | Jan 21 20:12:36 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 71 | Jan 21 20:12:36 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 72 | Jan 21 20:12:40 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 73 | Jan 21 20:12:40 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 74 | Jan 21 20:13:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 75 | Jan 21 20:13:21 IITMBSC pkexec[123026]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 76 | Jan 21 20:13:54 IITMBSC su: pam_unix(su:session): session closed for user guest 77 | Jan 21 20:14:35 IITMBSC su: (to guest) student on pts/1 78 | Jan 21 20:14:35 IITMBSC su: pam_unix(su:session): session opened for user guest by student(uid=1000) 79 | Jan 21 20:15:50 IITMBSC su: pam_unix(su:auth): authentication failure; logname=student uid=1000 euid=0 tty=pts/4 ruser=student rhost= user=guest2 80 | Jan 21 20:15:53 IITMBSC su: FAILED SU (to guest2) student on pts/4 81 | Jan 21 20:15:56 IITMBSC su: (to guest2) student on pts/4 82 | Jan 21 20:15:56 IITMBSC su: pam_unix(su:session): session opened for user guest2 by student(uid=1000) 83 | Jan 21 20:17:01 IITMBSC CRON[123336]: pam_unix(cron:session): session opened for user root by (uid=0) 84 | Jan 21 20:17:01 IITMBSC CRON[123336]: pam_unix(cron:session): session closed for user root 85 | Jan 21 20:23:58 IITMBSC gnome-keyring-daemon[1856]: asked to register item /org/freedesktop/secrets/collection/login/2, but it's already registered 86 | Jan 21 20:30:01 IITMBSC CRON[124399]: pam_unix(cron:session): session opened for user root by (uid=0) 87 | Jan 21 20:30:01 IITMBSC CRON[124399]: pam_unix(cron:session): session closed for user root 88 | Jan 21 20:31:54 IITMBSC su: pam_unix(su:session): session closed for user guest 89 | Jan 21 20:31:56 IITMBSC su: pam_unix(su:session): session closed for user guest2 90 | Jan 21 20:32:04 IITMBSC gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session opened for user gdm by (uid=0) 91 | Jan 21 20:32:04 IITMBSC systemd-logind[966]: New session c2 of user gdm. 92 | Jan 21 20:32:04 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user gdm by (uid=0) 93 | Jan 21 20:32:05 IITMBSC polkitd(authority=local): Registered Authentication Agent for unix-session:c2 (system bus name :1.525 [/usr/bin/gnome-shell], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) 94 | Jan 21 20:32:17 IITMBSC gdm-launch-environment]: pam_unix(gdm-launch-environment:session): session closed for user gdm 95 | Jan 21 20:32:17 IITMBSC systemd-logind[966]: Session c2 logged out. Waiting for processes to exit. 96 | Jan 21 20:32:17 IITMBSC systemd-logind[966]: Removed session c2. 97 | Jan 21 20:32:17 IITMBSC polkitd(authority=local): Unregistered Authentication Agent for unix-session:c2 (system bus name :1.525, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus) 98 | Jan 21 20:32:32 IITMBSC login[119373]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 99 | Jan 21 20:32:32 IITMBSC systemd-logind[966]: New session 31 of user guest. 100 | Jan 21 20:32:32 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest by (uid=0) 101 | Jan 21 20:32:41 IITMBSC login[119373]: pam_unix(login:session): session closed for user guest 102 | Jan 21 20:32:41 IITMBSC systemd-logind[966]: Session 31 logged out. Waiting for processes to exit. 103 | Jan 21 20:32:41 IITMBSC systemd-logind[966]: Removed session 31. 104 | Jan 21 20:32:52 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 105 | Jan 21 20:32:52 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 106 | Jan 21 20:32:54 IITMBSC gnome-keyring-daemon[1856]: asked to register item /org/freedesktop/secrets/collection/login/1, but it's already registered 107 | Jan 21 20:32:56 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 108 | Jan 21 20:32:56 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 109 | Jan 21 20:33:00 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 110 | Jan 21 20:33:00 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 111 | Jan 21 20:33:03 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 112 | Jan 21 20:33:03 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 113 | Jan 21 20:34:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 114 | Jan 21 20:34:21 IITMBSC pkexec[128737]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 115 | ``` 116 | 117 | ## Instructions 118 | 119 | Write your script in the file `logintime.sh`. 120 | Your script will not receive any input from the standard input or command line arguments. 121 | Your script should output to the standard output. 122 | 123 | ## Sample Output 124 | 125 | ```bash 126 | Jan 21 20:32:32 127 | ``` 128 | -------------------------------------------------------------------------------- /ppa_5.8/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 5 - Question 8 2 | 3 | ## Problem Statement 4 | 5 | Mine the logs given in the file `myauth.log` present in the current working directory to print all the usernames to which user `student` switched to using `su` command. 6 | 7 | Note: switching back to the previous user should not be accounted. 8 | 9 | Hint: Basically you have to grep all the lines where `su` command is run successfully and fetch the **username** to which the user `student` switched to. 10 | 11 | Contents of `myauth.log` is given below. 12 | 13 | ## Sample Input 14 | 15 | ```text 16 | Jan 21 20:32:17 IITMBSC polkitd(authority=local): Unregistered Authentication Agent for unix-session:c2 (system bus name :1.525, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus) 17 | Jan 21 20:32:32 IITMBSC login[119373]: pam_unix(login:session): session opened for user guest by LOGIN(uid=0) 18 | Jan 21 20:32:32 IITMBSC systemd-logind[966]: New session 31 of user guest. 19 | Jan 21 20:32:32 IITMBSC systemd: pam_unix(systemd-user:session): session opened for user guest by (uid=0) 20 | Jan 21 20:32:41 IITMBSC login[119373]: pam_unix(login:session): session closed for user guest 21 | Jan 21 20:32:41 IITMBSC systemd-logind[966]: Session 31 logged out. Waiting for processes to exit. 22 | Jan 21 20:32:41 IITMBSC systemd-logind[966]: Removed session 31. 23 | Jan 21 20:32:52 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 24 | Jan 21 20:32:52 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 25 | Jan 21 20:32:54 IITMBSC gnome-keyring-daemon[1856]: asked to register item /org/freedesktop/secrets/collection/login/1, but it's already registered 26 | Jan 21 20:32:56 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 27 | Jan 21 20:32:56 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 28 | Jan 21 20:33:00 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 29 | Jan 21 20:33:00 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 30 | Jan 21 20:33:03 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 31 | Jan 21 20:33:03 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 32 | Jan 21 20:34:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 33 | Jan 21 20:34:21 IITMBSC pkexec[128737]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 34 | Jan 21 20:43:12 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 35 | Jan 21 20:43:12 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 36 | Jan 21 20:43:16 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 37 | Jan 21 20:43:16 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 38 | Jan 21 20:43:19 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 39 | Jan 21 20:43:19 IITMBSC pkexec[130745]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 40 | Jan 21 20:43:19 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 41 | Jan 21 20:43:19 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 42 | Jan 21 20:46:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 43 | Jan 21 20:46:21 IITMBSC pkexec[131794]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 44 | Jan 21 20:48:44 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 45 | Jan 21 20:48:44 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 46 | Jan 21 20:49:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 47 | Jan 21 20:49:21 IITMBSC pkexec[132770]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 48 | Jan 21 21:03:32 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 49 | Jan 21 21:03:32 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 50 | Jan 21 21:03:36 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 51 | Jan 21 21:03:36 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 52 | Jan 21 21:03:40 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 53 | Jan 21 21:03:40 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 54 | Jan 21 21:03:43 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 55 | Jan 21 21:03:43 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 56 | Jan 21 21:04:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 57 | Jan 21 21:04:21 IITMBSC pkexec[136923]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 58 | Jan 21 21:08:53 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 59 | Jan 21 21:08:53 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 60 | Jan 21 21:08:57 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 61 | Jan 21 21:08:57 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 62 | Jan 21 21:09:00 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 63 | Jan 21 21:09:00 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 64 | Jan 21 21:09:07 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 65 | Jan 21 21:09:07 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 66 | Jan 21 21:10:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 67 | Jan 21 21:10:21 IITMBSC pkexec[140512]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 68 | Jan 21 21:17:01 IITMBSC CRON[140600]: pam_unix(cron:session): session opened for user root by (uid=0) 69 | Jan 21 21:17:01 IITMBSC CRON[140601]: pam_unix(cron:session): session opened for user root by (uid=0) 70 | Jan 21 21:17:01 IITMBSC CRON[140600]: pam_unix(cron:session): session closed for user root 71 | Jan 21 21:17:01 IITMBSC CRON[140601]: pam_unix(cron:session): session closed for user root 72 | Jan 21 21:29:12 IITMBSC gdm-password]: gkr-pam: unlocked login keyring 73 | Jan 21 21:30:01 IITMBSC CRON[140874]: pam_unix(cron:session): session opened for user root by (uid=0) 74 | Jan 21 21:30:01 IITMBSC CRON[140874]: pam_unix(cron:session): session closed for user root 75 | Jan 21 21:46:13 IITMBSC su: (to guest) student on pts/4 76 | Jan 21 21:46:13 IITMBSC su: pam_unix(su:session): session opened for user guest by student(uid=1000) 77 | Jan 21 21:46:19 IITMBSC su: (to guest2) student on pts/4 78 | Jan 21 21:46:19 IITMBSC su: pam_unix(su:session): session opened for user guest2 by student(uid=1001) 79 | Jan 21 21:46:27 IITMBSC su: (to guest) student on pts/4 80 | Jan 21 21:46:27 IITMBSC su: pam_unix(su:session): session opened for user guest by student(uid=1002) 81 | Jan 21 21:46:37 IITMBSC sudo: guest : user NOT in sudoers ; TTY=pts/4 ; PWD=/home/student ; USER=root ; COMMAND=/usr/bin/su 82 | Jan 21 21:46:49 IITMBSC su: (to student) student on pts/4 83 | Jan 21 21:46:49 IITMBSC su: pam_unix(su:session): session opened for user student by student(uid=1001) 84 | Jan 21 21:46:56 IITMBSC sudo: student : TTY=pts/4 ; PWD=/home/student ; USER=root ; COMMAND=/usr/bin/su 85 | Jan 21 21:46:56 IITMBSC sudo: pam_unix(sudo:session): session opened for user root by student(uid=0) 86 | Jan 21 21:46:56 IITMBSC su: (to root) student on pts/4 87 | Jan 21 21:46:56 IITMBSC su: pam_unix(su:session): session opened for user root by student(uid=0) 88 | Jan 21 21:47:05 IITMBSC su: (to guest2) student on pts/4 89 | Jan 21 21:47:05 IITMBSC su: pam_unix(su:session): session opened for user guest2 by student(uid=0) 90 | Jan 21 21:47:31 IITMBSC su: pam_unix(su:session): session closed for user guest2 91 | Jan 21 21:47:31 IITMBSC su: pam_unix(su:session): session closed for user root 92 | Jan 21 21:47:31 IITMBSC sudo: pam_unix(sudo:session): session closed for user root 93 | Jan 21 21:47:31 IITMBSC su: pam_unix(su:session): session closed for user student 94 | Jan 21 21:47:31 IITMBSC su: pam_unix(su:session): session closed for user guest 95 | Jan 21 21:47:32 IITMBSC su: pam_unix(su:session): session closed for user guest2 96 | Jan 21 21:47:32 IITMBSC su: pam_unix(su:session): session closed for user guest 97 | Jan 21 21:47:38 IITMBSC gnome-keyring-daemon[1856]: asked to register item /org/freedesktop/secrets/collection/login/1, but it's already registered 98 | Jan 21 21:47:43 IITMBSC su: (to guest2) student on pts/4 99 | Jan 21 21:47:43 IITMBSC su: pam_unix(su:session): session opened for user guest2 by student(uid=1000) 100 | Jan 21 21:47:48 IITMBSC su: (to guest) student on pts/4 101 | Jan 21 21:47:48 IITMBSC su: pam_unix(su:session): session opened for user guest by student(uid=1002) 102 | Jan 21 21:47:49 IITMBSC su: pam_unix(su:session): session closed for user guest 103 | Jan 21 21:47:49 IITMBSC su: pam_unix(su:session): session closed for user guest2 104 | Jan 21 21:48:44 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 105 | Jan 21 21:48:44 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 106 | Jan 21 21:49:14 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 107 | Jan 21 21:49:14 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 108 | Jan 21 21:49:17 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 109 | Jan 21 21:49:17 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 110 | Jan 21 21:49:20 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 111 | Jan 21 21:49:20 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 112 | Jan 21 21:49:23 IITMBSC PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0) 113 | Jan 21 21:49:23 IITMBSC PackageKit: uid 1000 obtained auth for org.freedesktop.packagekit.system-sources-refresh 114 | Jan 21 21:52:21 IITMBSC pkexec: pam_unix(polkit-1:session): session opened for user root by (uid=1000) 115 | Jan 21 21:52:21 IITMBSC pkexec[146922]: student: Executing command [USER=root] [TTY=unknown] [CWD=/home/student] [COMMAND=/usr/lib/update-notifier/package-system-locked] 116 | ``` 117 | 118 | ## Instructions 119 | 120 | Write your script in the file `su.sh`. 121 | Your script will not receive any input from the standard input or command line arguments. 122 | Your script should output to the standard output. 123 | 124 | 125 | ## Sample Output 126 | 127 | ```bash 128 | guest 129 | guest2 130 | guest 131 | student 132 | root 133 | guest2 134 | guest2 135 | guest 136 | ``` 137 | -------------------------------------------------------------------------------- /ppa_5.8/su.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep "su: (to" myauth.log | grep "student on pts" | cut -d' ' -f7 | cut -d')' -f1 -------------------------------------------------------------------------------- /ppa_6.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 6 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | Given a file `words.txt` containing a string in each line in the format `FIRST_second`. 6 | Every string is a combination of two words joined with an underscore(_), 7 | the first word `FIRST` consists of all uppercase letters 8 | and the second word `second` consists of all lowercase letters. 9 | 10 | Write a bash command/script using `sed` to convert all the string to `SECOND_first`. 11 | 12 | After conversion: 13 | 14 | - The **first** and the **second** words should be swapped. 15 | - The **uppercase** word should be converted to **lowercase** word and vice versa. 16 | 17 | The file `words.txt` is located in the current working directory. 18 | 19 | ## Instructions 20 | 21 | Write your solution in the file `swapcase.sh`. 22 | Your script will not receive any input from the standard input or command line arguments. 23 | Your script should output to the standard output. 24 | 25 | ## Sample Input 26 | 27 | ```text 28 | AD_ux 29 | AD_uy 30 | ``` 31 | 32 | ## Sample Output 33 | 34 | ```text 35 | UX_ad 36 | UY_ad 37 | ``` 38 | -------------------------------------------------------------------------------- /ppa_6.1/swapcase.sh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BriskAM/SC-E/dcd335583d0f51a9283bc1036345fe341e2a0f1f/ppa_6.1/swapcase.sh -------------------------------------------------------------------------------- /ppa_6.2/myCount.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #NOT WORKING 4 | 5 | while getopts "lwns:" opt; do 6 | case $opt in 7 | l) 8 | sed -n '$=' "${@: -1}" 9 | ;; 10 | w) 11 | sed 's/[[:space:]]\+/\n/g' "${@: -1}" | sed '/^$/d' | sed -n '$=' 12 | ;; 13 | n) 14 | sed -n '/^[0-9]\+$/p' "${@: -1}" | sed -n '$=' 15 | ;; 16 | s) 17 | sed -n "/$OPTARG/p" "${@: -1}" | sed -n '$=' 18 | ;; 19 | esac 20 | done 21 | -------------------------------------------------------------------------------- /ppa_6.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 6 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | Without using the `wc` or `awk` commands (instead use sed as Bash command), 6 | write a **bash** script that accepts any number of arguments. 7 | Out of these some would be options (hyphen plus a character like -l or -c) 8 | and the last argument will be a **file path** 9 | (use ${@: -1} to access the last argument, there is a space before -1). 10 | Only four options are accepted by your script `-l`, `-w`, `-n` and `-s`. 11 | 12 | Assume that file path given will always be for a valid file and we will refer it as file in the next lines. 13 | 14 | For options, 15 | 16 | - If no option is supplied to your script do nothing. 17 | - If `-l` option is supplied, print the number of lines in the file. 18 | - If `-w` option is supplied, print the number of words in the file. 19 | Assume that any string between spaces is a word. 20 | i.e. if using awk count the number of fields in each line to get the word count. 21 | - If `-n` option is supplied, print the number of lines having only digits(no alphabets or spaces) in the file. 22 | - option `-s` also accepts an argument say str. 23 | In this case print the number of lines containing the string str. 24 | - The above options can be supplied together or more than once. 25 | Print the required count for each appearance of the option on a new line. For e.g. 26 | - if `-l` and `-w` are both supplied together in the sequence print count of lines and count of words each on separate lines. 27 | - If `-l`, `-n` and `-l` options are supplied in the sequence then print number of lines, number of lines containing only digits and finally again number of lines in the file each on separate line. 28 | 29 | Note: Your bash script should not even contain any variable or comment that contians the string wc or awk. 30 | 31 | **Hints**: 32 | 33 | - Use `while getopts` style code. 34 | - Use `sed` to find the count of lines as specified in the conditions above. 35 | 36 | ## Sample Input 37 | 38 | Suppose your bash script is named as `myCount.sh`. 39 | In the below sample the argument to `-s` option is "say" so this should count all the lines containing the string "say". 40 | For the public test case all the commands given in the below sample are executed one by one on the input file. 41 | 42 | 43 | ```bash 44 | $ cat somefile.txt 45 | This is a sample file 46 | this is not end justsay start 47 | that contains say 48 | some number 49 | say like 10 50 | or 51 | 20 52 | or 53 | 233 54 | 444 55 | or say 3444 56 | and now it ends. 57 | ``` 58 | 59 | ## Sample Output 60 | 61 | ```bash 62 | $ bash myCount.sh -l somefile.txt 63 | 12 64 | $ bash myCount.sh -w somefile.txt 65 | 32 66 | $ bash myCount.sh -n somefile.txt 67 | 3 68 | $ bash myCount.sh -s say somefile.txt 69 | 4 70 | $ bash myCount.sh -l -n somefile.txt 71 | 12 72 | 3 73 | $ bash myCount.sh -l -s say -l -n somefile.txt 74 | 12 75 | 4 76 | 12 77 | 3 78 | $ bash myCount.sh 79 | $ bash myCount.sh somefile.txt 80 | ``` 81 | 82 | ## Instructions 83 | 84 | Write your solution in the file `myCount.sh`. 85 | Your script will not receive any input from the standard input. 86 | Your script should output to the standard output. 87 | -------------------------------------------------------------------------------- /ppa_6.3/backup.sh: -------------------------------------------------------------------------------- 1 | find source -type f -exec cp {} destination \; -------------------------------------------------------------------------------- /ppa_6.3/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 6 - Question 3 2 | 3 | ## Problem Statement 4 | 5 | Write a **bash** command using `find` that copies all the files within the directory `source` to `destination`. 6 | Note that all the files within all hierarchy have distinct names and should be copied to `destination`. 7 | Only files should be copied and not directories. 8 | All hierarchies should be flattened. 9 | 10 | ## Instructions 11 | 12 | Write your solution in the file `backup.sh`. 13 | Your script will not receive any input from the standard input or command line arguments. 14 | Your script should output to the standard output. 15 | 16 | ## Sample input 17 | 18 | Assume the folder structure of `source/` as: 19 | 20 | ```bash 21 | $ tree source 22 | source 23 | ├── 1 24 | │   ├── x 25 | │   ├── y 26 | │   └── z 27 | ├── 10 28 | ├── 2 29 | │   ├── a 30 | │   ├── b 31 | │   ├── c 32 | │   └── d 33 | ├── 3 34 | ├── 4 35 | ├── 5 36 | ├── 6 37 | ├── 7 38 | ├── 8 39 | └── 9 40 | 41 | 3 directories, 15 files 42 | ``` 43 | 44 | ## Sample Output 45 | 46 | The directory structure after running your script should be: 47 | 48 | ```bash 49 | $ tree destination 50 | destination 51 | ├── 10 52 | ├── 3 53 | ├── 4 54 | ├── 5 55 | ├── 6 56 | ├── 7 57 | ├── 8 58 | ├── 9 59 | ├── a 60 | ├── b 61 | ├── c 62 | ├── d 63 | ├── x 64 | ├── y 65 | └── z 66 | 67 | 1 directory, 15 files 68 | ``` 69 | 70 | -------------------------------------------------------------------------------- /ppa_7.1/evenodd.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN { 4 | sum_odd = 0; 5 | sum_even = 0; 6 | } 7 | { 8 | for (i = 1; i <= $0; i++) { 9 | if (i % 2 == 0) { 10 | sum_even += i; 11 | } else { 12 | sum_odd += i; 13 | } 14 | } 15 | } 16 | END { 17 | print sum_odd; 18 | print sum_even; 19 | } -------------------------------------------------------------------------------- /ppa_7.1/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 7 - Question 1 2 | 3 | ## Problem Statement 4 | 5 | Write an awk script that reads a value n from the stdin within awk script, 6 | then prints the sum of odd numbers and sum of even numbers each on a separate line respectively, 7 | from the set of natural numbers from 1 to n (ends inclusive). 8 | 9 | ## Instructions 10 | 11 | Write your code in the file `evenodd.awk`. 12 | The file should be an **awk** script, and not a shell script. 13 | Your script will not receive any input from the command line arguments. 14 | The input will be provided through standard input. 15 | Your script should output to the standard output. 16 | 17 | ## Sample Input 18 | 19 | ``` 20 | 10 21 | ``` 22 | 23 | ## Sample Output 24 | 25 | ``` 26 | 25 27 | 30 28 | ``` 29 | -------------------------------------------------------------------------------- /ppa_7.2/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 7 - Question 2 2 | 3 | ## Problem Statement 4 | 5 | Write an awk script to find unintentionally repeated (duplicate) words in the file 'myfile.txt'. 6 | 7 | For example, sometimes a file can contain sentences like: 8 | "The the building is beautiful". 9 | 10 | Print the repeated words in the order of occurence, one word per line. 11 | 12 | A word should not be printed more than once. 13 | 14 | ## Instructions 15 | 16 | Write your command in a file called `repeat.awk`. 17 | The file should be an **awk** script, and not a shell script. 18 | Your script will not receive any input from the standard input or command line arguments. 19 | Your script should output to the standard output. 20 | 21 | ## Sample Input 22 | 23 | ```text 24 | Long years ago, we made a tryst with destiny; and now the the time comes when we shall redeem our pledge, not not wholly or in full measure, but very very substantially. 25 | At the stroke of the midnight hour, when the world sleeps, India will will awake to life and freedom. 26 | ``` 27 | 28 | ## Sample Output 29 | 30 | ```text 31 | the 32 | not 33 | very 34 | will 35 | ``` 36 | -------------------------------------------------------------------------------- /ppa_7.2/repeat.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN { 4 | FS = "[^a-zA-Z]+" 5 | } 6 | { 7 | for (i = 1; i <= NF; i++) { 8 | word = tolower($i) 9 | seen[word] = 0 10 | if (word == "") { 11 | continue 12 | } 13 | next_word = tolower($(i+1)) 14 | if (word == next_word && seen[word] == 0) { 15 | print word 16 | seen[word] = 1 17 | i++ 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /ppa_7.3/big.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN { 4 | currfile = "" 5 | maxfile = "" 6 | maxlines = 0 7 | currlines = 0 8 | } 9 | { 10 | if (FILENAME != currfile) { 11 | if (currlines > maxlines) { 12 | maxlines = currlines 13 | maxfile = currfile 14 | } 15 | currfile = FILENAME 16 | currlines = 0 17 | } 18 | currlines++ 19 | } 20 | END { 21 | if (currlines > maxlines) { 22 | maxlines = currlines 23 | maxfile = currfile 24 | } 25 | print maxfile 26 | } -------------------------------------------------------------------------------- /ppa_7.3/readme.md: -------------------------------------------------------------------------------- 1 | # Programming Practice Assignment - Week 7 - Question 3 2 | 3 | ## Problem Statement 4 | 5 | Write a script using AWK to print the file with the maximum number of lines. 6 | 7 | ## Instructions 8 | 9 | Write your solution in the file `big.awk`. 10 | The script should be an **awk** script, and not a shell script. 11 | Your script will not receive any input from the standard input. 12 | Your script should output to the standard output. 13 | 14 | ## Sample Input 15 | 16 | ```text 17 | file1.txt 18 | 1 19 | 2 20 | 3 21 | 4 22 | 5 23 | 24 | file2.txt 25 | 1 26 | 2 27 | 3 28 | 29 | file3.txt 30 | 1 31 | 32 | 33 | 34 | ``` 35 | 36 | ## Sample Output 37 | 38 | ```text 39 | file1.txt 40 | ``` 41 | 42 | ## Constraints 43 | 44 | If multiple files have the maximum number of lines, 45 | output the name of the file appearing at first. 46 | -------------------------------------------------------------------------------- /practice_2.1/readme.md: -------------------------------------------------------------------------------- 1 | # Week 2 Problem 1 2 | 3 | There is a file named ` final.txt ` in the current working directory, that contains some text that you do not want it to be edited. 4 | 5 | Write a bash command to remove write permissions of user from this file. Do not edit any other file permissions. 6 | 7 | Note : Run ` synchro init ` to get the file ` final.txt ` to your solutions direcotry. -------------------------------------------------------------------------------- /practice_2.1/solution.md: -------------------------------------------------------------------------------- 1 | chmod u-w final.txt -------------------------------------------------------------------------------- /practice_2.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 2 Problem 2 2 | 3 | Write a bash script ` script.sh ` which prints the mime type of ` somefile ` located in ` ~/se2001/practice_2.2 `. 4 | 5 | Hint: Use ` file `. Refer manual of ` file ` or ` file --help `. 6 | 7 | Remember that: 8 | - Initially you may not find any file. 9 | - The files are automatically created when you run ` synchro eval `. 10 | - The same strategy is followed for most of the practice problems. 11 | - You can create dummy files to check for yourself if your script works. -------------------------------------------------------------------------------- /practice_2.2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | file_path="$PWD/somefile" 3 | file --mime-type "$file_path" | sed "s|$file_path|somefile|" -------------------------------------------------------------------------------- /practice_2.3/readme.md: -------------------------------------------------------------------------------- 1 | # Week 2 Problem 3 2 | 3 | Write a bash script ` script.sh ` in directory ` ~/se2001/practice_2.3 ` to find the day of the week of April 1st of 2024 and print it to the output in full format with first letter capitalised. Ex: ` Wednesday ` 4 | 5 | Hint: Use ` date ` command. -------------------------------------------------------------------------------- /practice_2.3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | date -d "2024-04-01" +%A -------------------------------------------------------------------------------- /practice_3.1/readme.md: -------------------------------------------------------------------------------- 1 | # Week 3 Problem 1 2 | 3 | Write a command to append the contents of ` file1.txt ` to the start of ` file2.txt `. 4 | 5 | That is, the contents of ` file2.txt ` should be the contents of ` file1.txt ` followed by the contents ` file2.txt `. 6 | 7 | Note: 8 | - Use ` synchro init ` to initialize the files. -------------------------------------------------------------------------------- /practice_3.1/solution.md: -------------------------------------------------------------------------------- 1 | cat file1.txt file2.txt > temp && mv temp file2.txt -------------------------------------------------------------------------------- /practice_3.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 3 Problem 2 2 | 3 | Write a bash script ` script.sh ` to redirect the stderr and stdout of the command ` ls file1.txt file2.txt ` in that particular order to ` output.txt `. -------------------------------------------------------------------------------- /practice_3.2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ls file1.txt file2.txt > output.txt 2>&1 -------------------------------------------------------------------------------- /practice_4.1/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 1 2 | 3 | Consider a file named ` lsinfo.txt ` in your current working directory that contains output of the command ` ls -al ` run on some directory. 4 | 5 | 1. Use grep/egrep to print all the files/directories in file ` lsinfo.txt ` such that for those files/directories: 6 | 7 | 1. Owner has read and write permission only. 8 | 2. The group to which user belongs to has read permission only 9 | 3. Other users have read permission only 10 | 11 | Write your answer as a bash script ` script.sh `. Redirect your output to file ` out.txt `. 12 | 13 | Note: 14 | - Run ` synchro init ` before running ` synchro eval `. -------------------------------------------------------------------------------- /practice_4.1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | egrep '^-[rw-]{2}-r--r--' lsinfo.txt > out.txt -------------------------------------------------------------------------------- /practice_4.10/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 10 2 | 3 | Write the script ` script.sh ` to list the names of all the c++ files (i.e. with the extension ` .cpp `) in the current directory which contains a line such that 4 | 5 | - The line starts with the string ` void main() ` and ends with the character ` { `. There should be one or more spaces/tabs between the characters ` { ` and ` ) ` , but nothing except spaces. 6 | - For e.g the lines ` void main(){ `, ` void main() {}} ` are not a match for the above condition, but the lines ` void main() { `, ` void main() { ` are a match. 7 | 8 | And also print the count of these files in the following line. (Note: Try ` tee /dev/tty ` to the purpose). 9 | 10 | Store your output in file ` out.txt `. -------------------------------------------------------------------------------- /practice_4.10/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | { 3 | count=0 4 | for file in *.cpp; do 5 | if [[ -f "$file" ]]; then 6 | if grep -qP '^void main\(\)\s+\{$' "$file"; then 7 | echo "$file" | tee /dev/tty 8 | ((count++)) 9 | fi 10 | fi 11 | done 12 | echo "$count" | tee /dev/tty 13 | } > out.txt -------------------------------------------------------------------------------- /practice_4.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 2 2 | 3 | Write a bash script ` script.sh ` to perform the following task, redirect your output to ` out.txt `. 4 | 5 | Consider a file named ` lsinfo.txt ` in your current working directory that contains output of the command ` ls -al ` run on some directory. 6 | 7 | Use grep/egrep to print the long-listing of all files excluding directories in ` lsinfo.txt ` whose last modified date is in January. 8 | 9 | Note: 10 | - There are many special kind of files in linux for example for socket files the first character in permission string is 's' like it is 'd' for directories. 11 | - Run ` synchro init ` to get ` lsinfo.txt ` in your ` pwd `.` -------------------------------------------------------------------------------- /practice_4.2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -v '^d' lsinfo.txt | grep 'Jan' > out.txt -------------------------------------------------------------------------------- /practice_4.3/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 3 2 | 3 | The command below gives the following output. 4 | 5 | ```bash 6 | $ cat twocities.txt 7 | It was the best of times, it was the worst of times, 8 | it was the age of wisdom, it was the age of foolishness, 9 | it was the epoch of belief, it was the epoch of incredulity, 10 | it was the season of Light, it was the season of Darkness, 11 | it was the spring of hope, it was the winter of despair, 12 | we had everything before us, we had nothing before us, 13 | we were all going direct to Heaven, we were all going direct the other way 14 | ``` 15 | 16 | Write a bash script ` script.sh ` to count and print the number of lines that starts with a capital letter and contains the word ` it ` (case-sensitive) in the above file. 17 | 18 | (a) Try a solution with only grep command. -------------------------------------------------------------------------------- /practice_4.3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -E "^[A-Z].*it" twocities.txt | wc -l 4 | -------------------------------------------------------------------------------- /practice_4.4/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 4 2 | 3 | The command below gives the following output. 4 | 5 | ```bash 6 | $ cat twocities.txt 7 | It was the best of times, it was the worst of times, 8 | it was the age of wisdom, it was the age of foolishness, 9 | it was the epoch of belief, it was the epoch of incredulity, 10 | it was the season of Light, it was the season of Darkness, 11 | it was the spring of hope, it was the winter of despair, 12 | we had everything before us, we had nothing before us, 13 | we were all going direct to Heaven, we were all going direct the other way 14 | ``` 15 | 16 | Write a bash script ` script.sh ` which satisfies the statement below: 17 | 18 | - ` out.txt ` contains all the lines that does not contain the word "we" in it. 19 | 20 | - Run ` synchro init ` to get started. -------------------------------------------------------------------------------- /practice_4.4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -v '\b[wW]e\b' twocities.txt > out.txt -------------------------------------------------------------------------------- /practice_4.5/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 5 2 | 3 | The file ` file.txt ` contains three fields in a line in the order 4 | 5 | - Country 6 | - Capital 7 | - Continent 8 | 9 | The first two fields country and capital are separated by a comma and the last two fields capital and continent are separated by a semicolon. 10 | 11 | The command below prints the file. 12 | 13 | ```bash 14 | $ cat file.txt 15 | India, New Delhi; Asia 16 | Czech Republic, Prague; Europe 17 | South Korea, Seoul; Asia 18 | Uzbekistan, Tashkent; Asia 19 | Mozambique, Maputo; Africa 20 | United Arab Emirates, Abu Dhabi; Asia 21 | Papua New Guinea, Port Moresby; Oceania 22 | Estonia, Tallinn; Europe 23 | Trinidad and Tobago, Port of Spain; South America 24 | Ghana, Accra; Africa 25 | ``` 26 | 27 | Write a bash script ` script.sh ` using ` cut ` to redirect only the countries and its capitals of ` file.txt ` in the format ` Country, Capital ` (Eg. ` India, New Delhi `) to file named ` out.txt `. -------------------------------------------------------------------------------- /practice_4.5/script.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | cut -d ';' -f 1 file.txt > out.txt -------------------------------------------------------------------------------- /practice_4.6/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 6 2 | 3 | The command below gives the following output. 4 | 5 | ```bash 6 | $ cat twocities.txt 7 | It was the best of times, it was the worst of times, 8 | it was the age of wisdom, it was the age of foolishness, 9 | it was the epoch of belief, it was the epoch of incredulity, 10 | it was the season of Light, it was the season of Darkness, 11 | it was the spring of hope, it was the winter of despair, 12 | we had everything before us, we had nothing before us, 13 | we were all going direct to Heaven, we were all going direct the other way 14 | ``` 15 | 16 | Write bash script ` script.sh ` using grep to find all the lines that end with the letter ` s ` (comma and space at end should be excluded), in the above file. Redirect your output to file ` out.txt `. 17 | 18 | Note: 19 | - Run ` synchro init ` to begin. -------------------------------------------------------------------------------- /practice_4.6/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | egrep 's[, ]*$' twocities.txt > out.txt -------------------------------------------------------------------------------- /practice_4.7/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 7 2 | 3 | The command given below gives the following output. 4 | 5 | ```bash 6 | $ cat file.txt 7 | India, New Delhi; Asia 8 | Czech Republic, Prague; Europe 9 | South Korea, Seoul; Asia 10 | Uzbekistan, Tashkent; Asia 11 | Mozambique, Maputo; Africa 12 | United Arab Emirates, Abu Dhabi; Asia 13 | Papua New Guinea, Port Moresby; Oceania 14 | Estonia, Tallinn; Europe 15 | Trinidad and Tobago, Port of Spain; South America 16 | Ghana, Accra; Africa 17 | ``` 18 | 19 | Write a bash script ` script.sh ` to print all the countries in the file ` file.txt ` sorted alphabetically by name in reverse order. 20 | (Hint: Use the command ` sort -r ` to sort in reverse order) -------------------------------------------------------------------------------- /practice_4.7/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cut -d ',' -f1 file.txt | sort -r > out.txt -------------------------------------------------------------------------------- /practice_4.8/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 8 2 | 3 | The command given below gives the following output. 4 | 5 | ```bash 6 | $ cat file.txt 7 | India, New Delhi; Asia 8 | Czech Republic, Prague; Europe 9 | South Korea, Seoul; Asia 10 | Uzbekistan, Tashkent; Asia 11 | Mozambique, Maputo; Africa 12 | United Arab Emirates, Abu Dhabi; Asia 13 | Papua New Guinea, Port Moresby; Oceania 14 | Estonia, Tallinn; Europe 15 | Trinidad and Tobago, Port of Spain; South America 16 | Ghana, Accra; Africa 17 | ``` 18 | 19 | Use the cut command to extract the continents (including the one white space in the beginning) of the first 5 lines of file.txt and store it in another file named continent.txt 20 | 21 | Write your answer as a script with name ` script.sh `. 22 | 23 | Note: 24 | - Run ` synchro init ` to begin. -------------------------------------------------------------------------------- /practice_4.8/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cut -d ';' -f2 file.txt | head -n 5 > continent.txt -------------------------------------------------------------------------------- /practice_4.9/readme.md: -------------------------------------------------------------------------------- 1 | # Week 4 Problem 9 2 | 3 | Write a bash script ` script.sh ` to list the names of all the c++ files (i.e. with the extension ` .cpp `) in the current directory which contains a line such that 4 | 5 | - The line starts with the string ` void main() ` and ends with the character ` { `. There should be one or more spaces/tabs between the characters ` { ` and ` ) ` , but nothing except spaces. 6 | - For e.g the lines ` void main(){ `, ` void main() {}} ` are not a match for the above condition, but the lines ` void main() { `, ` void main() { ` are a match. 7 | 8 | Note: 9 | Run ` synchro init ` to begin. -------------------------------------------------------------------------------- /practice_4.9/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.cpp; do 4 | if [[ -f "$file" ]]; then 5 | if grep -qP '^void main\(\)\s+\{$' "$file"; then 6 | echo "$file" 7 | fi 8 | fi 9 | done -------------------------------------------------------------------------------- /practice_5.1/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 1 2 | 3 | A school teaches from first to twelfth standard, each standard has 5 sections named as ` A ` to ` E `. 4 | 5 | The class room number is represented as standard (as number) followed by the section, for example ` 12E `, ` 3C `, etc.. 6 | 7 | Your task is to create a directory for every classroom in directory ` ~/se2001/practice_5.1 `. -------------------------------------------------------------------------------- /practice_5.1/solution.md: -------------------------------------------------------------------------------- 1 | mkdir {1..12}{A..E} -------------------------------------------------------------------------------- /practice_5.10/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 10 2 | 3 | Write a bash script ` script.sh ` to redirect the **manual** entry (.i.e. output of man page) for the command which is specified as value of the shell variable named ` cmd ` to a text file ` man_commandValue.txt `, where ` commandValue ` is the value in variable ` cmd ` (example: if the variable ` cmd ` has value ` ls `, then redirect the out of `man ls` to the file named ` man_ls.txt ` ). 4 | 5 | If there is an error in getting the man page of command in variable ` cmd `, then redirect the standard error to ` man_commandValue.err ` and execute the help command for the command in variable ` cmd ` and redirect the output to the file ` man_commandValue.txt `, where ` commandValue ` is the value in variable ` cmd `. 6 | 7 | Hint: use operator && or || for executing help command based on exit status of first command. -------------------------------------------------------------------------------- /practice_5.10/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | man "$cmd" > "man_${cmd}.txt" 2> "man_${cmd}.err" 4 | 5 | if [ -s "man_${cmd}.err" ]; then 6 | "$cmd" --help > "man_${cmd}.txt" 2>/dev/null 7 | fi -------------------------------------------------------------------------------- /practice_5.11/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 11 2 | 3 | Write a script ` script.sh ` to redirect the stderr of a command which is specified via variable ` cmd ` to a text file ` error.txt ` and print the stdout only if there is no error. 4 | 5 | Hint: Store the stdout to some file then based on success or failure of the command print that file. -------------------------------------------------------------------------------- /practice_5.11/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | temp_out=$(mktemp) 4 | 5 | $cmd 2>error.txt >$temp_out 6 | 7 | if [ $? -eq 0 ]; then 8 | cat $temp_out 9 | fi 10 | 11 | rm $temp_out -------------------------------------------------------------------------------- /practice_5.12/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 12 2 | 3 | You are writing a Bash Script and in one of the task, you have to count the number of occurrences of some word in several files and finally the count should be in the variable ` ct `. 4 | 5 | Write a Bash script ` script.sh ` that can be sourced in a loop to count the number of occurrences in all files, supposing that word count is stored in the variable named ` words `. 6 | 7 | Your script will be used in a loop that will be run for each file. 8 | 9 | The value of ` ct ` should be initialized to 0, at the beginning, and the number of words stored in the variable ` words ` is to be added for your command. 10 | 11 | Print the value of the variable ` ct ` in the end. 12 | 13 | Hint: Write a Bash command in ` script.sh ` that is equivalent to pseudocode "ct=ct+words", where ` ct ` and ` words ` are variables -------------------------------------------------------------------------------- /practice_5.12/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z "$ct" ]; then 4 | ct=0 5 | fi 6 | 7 | ct=$((ct + words)) 8 | 9 | echo $ct -------------------------------------------------------------------------------- /practice_5.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 2 2 | 3 | A school teaches from first to twelfth standard, each standard has 5 sections named as ` A ` to ` E `. The class room number is represented as standard (as number) followed by the section, for example ` 12E `, ` 3C `, etc.. 4 | Each classroom has 40 students. 5 | 6 | There is a directory for every classroom in ` ~/se2001/practice_5.2 `. 7 | 8 | Write a Bash command to create a file for each student, name the files from 1 to 40 in every directory. 9 | 10 | **Note**: run `synchro init` to get the folders. -------------------------------------------------------------------------------- /practice_5.2/solution.md: -------------------------------------------------------------------------------- 1 | for standard in {1..12}; do 2 | for section in {A..E}; do 3 | for student in {1..40}; do 4 | touch ~/se2001/practice_5.2/"$standard$section"/$student 5 | done 6 | done 7 | done -------------------------------------------------------------------------------- /practice_5.3/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 problem 3 2 | 3 | A school teaches from first to twelfth standard, each standard has 5 sections named as ` A ` to ` E `. The class room number is represented as standard(as number) followed by the section, for example ` 12E `, ` 3C `, etc.. 4 | Each classroom has 40 students. There is a directory for every classroom. All the directories are located inside the directory ` ~/se2001/practice_5.3 `. There is a directory for every student named from 1 to 40 in every classroom directory. 5 | 6 | Suddenly the school experienced 40% drop in first and sixth standard so they reduced the number of students in each class room to 32. 7 | 8 | Your task is to remove the extra files in the respective directories, i.e. named from 33 to 40. 9 | 10 | Note : Run ` synchro init ` to initialize the directory as you did for ` practice_5.2 `. 11 | -------------------------------------------------------------------------------- /practice_5.3/solution.md: -------------------------------------------------------------------------------- 1 | for standard in 1 6; do 2 | for section in {A..E}; do 3 | rmdir ~/se2001/practice_5.3/"$standard$section"/{33..40} 4 | done 5 | done -------------------------------------------------------------------------------- /practice_5.4/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 4 2 | 3 | The shell variable ` logfile ` contains the absolute path to some file. 4 | 5 | Write a script ` script.sh ` which performs the following task. 6 | 7 | The task is to print two lines, where first line is the filename alone and the second line is the path of the directory in which the file ` logfile ` is located (print the path without the trail slash ` / `). 8 | 9 | Note: 10 | - Variables are created when you run ` synchro eval `. 11 | 12 | Example shell variable: ` logfile=/home/student78/daily.log ` 13 | 14 | **Output** 15 | 16 | ``` 17 | daily.log 18 | /home/student78 19 | ``` -------------------------------------------------------------------------------- /practice_5.4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | basename "$logfile" 4 | dirname "$logfile" -------------------------------------------------------------------------------- /practice_5.5/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 5 2 | 3 | A shell variable named ` TOTALCOST ` contains a string which is in the format ` XYZ_ABC_PQR `, where ` XYZ `, ` ABC ` and ` PQR ` are three digit numbers. Underscore ` _ ` is used to separate the three digit numbers. 4 | 5 | Write a bash script ` script.sh ` that replaces all the underscores (` _ `) with commas (` , `) in the variable ` TOTALCOST ` and displays the final string on the screen. 6 | 7 | Example: ` TOTALCOST=198_890_128 ` 8 | 9 | **Output** 10 | 11 | ``` 12 | 198,890,128 13 | ``` -------------------------------------------------------------------------------- /practice_5.5/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "${TOTALCOST//_/,}" -------------------------------------------------------------------------------- /practice_5.6/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 6 2 | 3 | Write a bash script ` script.sh ` to display only the filename without extension, whose absolute path is stored in a shell variable named ` file `. The extension should not include the dot ` . `. 4 | 5 | Example: ` file=/home/student56/tmp/artic.jpg ` 6 | 7 | **Output** 8 | 9 | ``` 10 | artic 11 | ``` -------------------------------------------------------------------------------- /practice_5.6/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | filename=$(basename "$file") 4 | echo "${filename%.*}" -------------------------------------------------------------------------------- /practice_5.7/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 7 2 | 3 | The string ` colors ` contains the name of the colors. 4 | Convert it to array of colors. 5 | 6 | Your task is to write a bash script ` script.sh ` to remove all vowels present in the array and display them. 7 | 8 | Example: ` colors="violet indigo blue green yellow orange red" ` 9 | 10 | Use the snippet below to convert a space separated string to an array. 11 | 12 | ```bash 13 | arr=($str) 14 | ``` 15 | 16 | **Output** 17 | 18 | ``` 19 | vlt ndg bl grn yllw rng rd 20 | ``` -------------------------------------------------------------------------------- /practice_5.7/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | arr=($colors) 3 | 4 | printf "%s" "$(echo "${arr[@]}" | tr -d 'aeiouAEIOU' | sed 's/ \+/ /g')" -------------------------------------------------------------------------------- /practice_5.8/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 8 2 | 3 | The variable ` filename ` has name of file along with the extension. 4 | 5 | Find the file extension and print it. 6 | 7 | Write your answer in bash script ` script.sh `. 8 | 9 | Note: 10 | - The variable will be made available during evaluation. 11 | 12 | Example, 13 | - If the filename is ` abc.txt `, print ` txt `. 14 | - If the filename is ` script.sh ` print ` sh `. -------------------------------------------------------------------------------- /practice_5.8/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "$filename" | cut -d'.' -f2 -------------------------------------------------------------------------------- /practice_5.9/readme.md: -------------------------------------------------------------------------------- 1 | # Week 5 Problem 9 2 | 3 | The employee ID of a certain company is in the following format: ` DDYYPXXX ` where 4 | 5 | - ` DD ` stands for department; ex: FN for Finance, SD for Software Development 6 | - ` YY ` stands for work experience, which is a two digit number. 7 | - ` P ` stands for position, which is a letter; e.g. M for Manager, etc. 8 | - ` XXX ` stands for ID number. Which is a three digit number. 9 | 10 | Given three Employee IDs, extract just the Department and the ID Number of these employees. Redirect all the output to the file named ` output.txt `. 11 | 12 | The names of the variables which stores the employee Ids are shell variables with names ` eid1 `, ` eid2 ` and ` eid3 `. Redirect your output in the same sequence as ` sort -n ` order of the shell variable names. 13 | 14 | Write your code in file ` script.sh `. -------------------------------------------------------------------------------- /practice_5.9/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "${eid1:0:2}${eid1:5:3}" > output.txt 4 | echo "${eid2:0:2}${eid2:5:3}" >> output.txt 5 | echo "${eid3:0:2}${eid3:5:3}" >> output.txt 6 | sort -n output.txt -------------------------------------------------------------------------------- /practice_7.1a/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 1c 2 | 3 | Write a bash script that reads two numbers from the standard input and prints the sum of the numbers. Assume that the input will be numeric only. 4 | 5 | Write your script in the file script.sh. -------------------------------------------------------------------------------- /practice_7.1a/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p num1 4 | read -p num2 5 | 6 | sum=$((num1 + num2)) 7 | echo $sum -------------------------------------------------------------------------------- /practice_7.1b/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 1b 2 | 3 | Write a Bash script that accepts a name (string) as command line argument to your script script.sh and print "hello input-name" as output, where input-name is the input string. 4 | 5 | Example: 6 | 7 | If the command line input string is Raghu the output should be 8 | hello Raghu 9 | 10 | Write your script in the file script.sh. -------------------------------------------------------------------------------- /practice_7.1b/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "hello $1" -------------------------------------------------------------------------------- /practice_7.1c/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 1c 2 | 3 | Write a bash script that reads two numbers from the standard input and prints the sum of the numbers. Assume that the input will be numeric only. 4 | 5 | Write your script in the file script.sh. -------------------------------------------------------------------------------- /practice_7.1c/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read num1 4 | read num2 5 | 6 | sum=$((num1 + num2)) 7 | echo $sum -------------------------------------------------------------------------------- /practice_7.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 2 2 | 3 | Write a bash script named ` script.sh ` that takes two arguments, checks if both the arguments are positive integers then prints their sum; else prints "NOT INTEGERS" to STDERR and exits with exit code 1. 4 | 5 | Note: Use the below if else conditional statement if needed. 6 | 7 | ```bash 8 | if condition; then 9 | ... 10 | ... 11 | else 12 | ... 13 | ... 14 | fi 15 | ``` -------------------------------------------------------------------------------- /practice_7.2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ $1 =~ ^[0-9]+$ ]] && [[ $2 =~ ^[0-9]+$ ]]; then 4 | echo $(($1 + $2)) 5 | else 6 | echo "NOT INTEGERS" >&2 7 | exit 1 8 | fi -------------------------------------------------------------------------------- /practice_7.3/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 3 2 | 3 | Write a bash script named ` script.sh `. The script takes two arguments, checks if both the arguments are positive integers then prints their sum; else concatenate the string values in both the arguments and prints the combined string. -------------------------------------------------------------------------------- /practice_7.3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ $1 =~ ^[0-9]+$ ]] && [[ $2 =~ ^[0-9]+$ ]]; then 4 | echo $(($1 + $2)) 5 | else 6 | echo "$1$2" 7 | fi -------------------------------------------------------------------------------- /practice_7.4/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 4 2 | 3 | Write a bash script named ` script.sh ` that accepts a file path as an argument and checks if that exists and is readable by the current user and prints the output as below. 4 | 5 | - Prints "DOES NOT EXIST" on STDERR and return with error code 1 if the file does not exist at the given path. 6 | - Prints "NOT READABLE" on STDERR and return with error code 2 if the file is not readable by the current user. 7 | - Prints "WOO HOO" if the file exists and is readable too. 8 | 9 | Note: 10 | - Use the below if elif conditional statement if needed 11 | 12 | ```bash 13 | if condition; then 14 | ... 15 | ... 16 | elif condition; then 17 | ... 18 | ... 19 | else 20 | ... 21 | ... 22 | fi 23 | ``` 24 | 25 | - The files will be created during ` synchro eval `. -------------------------------------------------------------------------------- /practice_7.4/script.sh: -------------------------------------------------------------------------------- 1 | if [ -e $1 ]; then 2 | if [ -r $1 ]; then 3 | echo "WOO HOO" 4 | else 5 | echo "NOT READABLE" >&2 6 | exit 2 7 | fi 8 | else 9 | echo "DOES NOT EXIST" >&2 10 | exit 1 11 | fi -------------------------------------------------------------------------------- /practice_7.5/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 5 2 | 3 | Write a bash script in ` script.sh ` file that takes an argument which takes a positive integer as argument and prints it in reverse order (For example, if the input number is 123, the Output will be 321) -------------------------------------------------------------------------------- /practice_7.5/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo $1 | rev -------------------------------------------------------------------------------- /practice_7.6/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 6 2 | 3 | Write a bash script in the file ` script.sh ` that accepts an integer argument say ` n ` and prints the below pattern for ` n ` lines. 4 | 5 | For example, 6 | In the below sample, the value of `n` is 5 7 | ``` 8 | * 9 | ** 10 | *** 11 | **** 12 | ***** 13 | ``` 14 | 15 | In the below sample, the value of `n` is 4. 16 | 17 | ``` 18 | * 19 | ** 20 | *** 21 | **** 22 | ``` -------------------------------------------------------------------------------- /practice_7.6/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in $(seq 1 $1); do 4 | printf '%*s\n' "$i" | tr " " "*" 5 | done -------------------------------------------------------------------------------- /practice_7.7/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 7 2 | 3 | Write a bash script in ` script.sh ` file that takes an integer argument and prints whether the number is prime or not. If the number in the argument is a prime number, the program must print "Prime" and if not, it must print "Not Prime"s -------------------------------------------------------------------------------- /practice_7.7/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | num=$1 4 | is_prime=1 5 | 6 | if [ $num -lt 2 ]; then 7 | is_prime=0 8 | fi 9 | 10 | for (( i=2; i*i<=$num; i++ )); do 11 | if [ $((num % i)) -eq 0 ]; then 12 | is_prime=0 13 | break 14 | fi 15 | done 16 | 17 | if [ $is_prime -eq 1 ]; then 18 | echo "Prime" 19 | else 20 | echo "Not Prime" 21 | fi -------------------------------------------------------------------------------- /practice_7.8/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 8 2 | 3 | ` df -h ` gives the disk/filesystem usage information. Write a bash script to list all the filesystem mount point names based on their percentage usage divided in 5 categories in the format below. 4 | 5 | ```pseudo 6 | 0-50 7 | (names of filesystem one in each line with usage between 0 to 50%) 8 | 50-75 9 | (names of filesystem one in each line with usage between 50 to 75%) 10 | 75-85 11 | (names of filesystem one in each line with usage between 75 to 85%) 12 | 85-95 13 | (names of filesystem one in each line with usage between 85 to 95%) 14 | >95 15 | (names of filesystem one in each line with usage above 95%) 16 | ``` 17 | 18 | In each category, print the range in one line, followed by the filesystem mount point names. Print the range string even if there are no filesystems with usage in that range. Your script should not print anything else, all other errors and output from your script should be redirected to /dev/null. 19 | 20 | Write your script in file ` script.sh `. 21 | 22 | Filesystem mount point name is the last field in the output of df -h. 23 | 24 | The categories are 25 | 26 | - 0% to less than 50% usage. 27 | - 50% to less than 75% usage. 28 | - 75% to less than 85% usage. 29 | - 85% to less than 95% usage. 30 | - Equal and above 95% usage. 31 | 32 | Hint: Can store the df command output in a file. Then work on the file named ` dfOutput.txt ` line by line using the below script. 33 | 34 | ```Bash 35 | while read -r line; 36 | do 37 | echo $line; # To print the line. 38 | # Write your code to process the line. 39 | done < dfOutput.txt 40 | ``` 41 | 42 | Use ${var:0:-1} to remove the last character of string var. 43 | 44 | Note: 45 | - The file ` dfOutput.txt ` will be made available during evaluation. -------------------------------------------------------------------------------- /practice_7.8/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | exec 2>/dev/null 4 | 5 | categories=("0-50" "50-75" "75-85" "85-95" ">95") 6 | ranges=(50 75 85 95) 7 | 8 | declare -A filesystems 9 | for category in "${categories[@]}"; do 10 | filesystems["$category"]="" 11 | done 12 | 13 | while read -r line; do 14 | [[ "$line" =~ ^Filesystem ]] && continue 15 | [[ -z "$line" ]] && continue 16 | 17 | usage=$(echo "$line" | awk '{print $5}' | tr -d '%') 18 | mount_point=$(echo "$line" | awk '{$1=$2=$3=$4=$5=""; print substr($0,6)}' | sed 's/^[[:space:]]*//') 19 | 20 | if ! [[ "$usage" =~ ^[0-9]+$ ]] || [[ -z "$mount_point" ]]; then 21 | continue 22 | fi 23 | 24 | if (( usage < ranges[0] )); then 25 | filesystems["0-50"]+="$mount_point"$'\n' 26 | elif (( usage < ranges[1] )); then 27 | filesystems["50-75"]+="$mount_point"$'\n' 28 | elif (( usage < ranges[2] )); then 29 | filesystems["75-85"]+="$mount_point"$'\n' 30 | elif (( usage < ranges[3] )); then 31 | filesystems["85-95"]+="$mount_point"$'\n' 32 | else 33 | filesystems[">95"]+="$mount_point"$'\n' 34 | fi 35 | done < dfOutput.txt 36 | 37 | for category in "${categories[@]}"; do 38 | echo "$category" 39 | if [[ -n "${filesystems[$category]}" ]]; then 40 | echo -e -n "${filesystems[$category]}" | sed '/^$/d' 41 | fi 42 | done 43 | -------------------------------------------------------------------------------- /practice_7.9/readme.md: -------------------------------------------------------------------------------- 1 | # Week 7 Problem 9 2 | 3 | In Problem 8, modify the output of your script as below. 4 | 5 | - Print the range string only if there is a filesystem in that range. 6 | 7 | For example, if there is no filesystem with usage >95% and also none in the range 75-85, and the rest all range has at least one filesystem with usage in that range than your output should be 8 | 9 | ```pseudo 10 | 0-50 11 | (names of filesystem one in each line with usage between 0 to 50%) 12 | 50-75 13 | (names of filesystem one in each line with usage between 50 to 75%) 14 | 85-95 15 | (names of filesystem one in each line with usage between 85 to 95%) 16 | ``` 17 | 18 | Note: 19 | - ` dfOutput.txt ` will be made available during evaluation. -------------------------------------------------------------------------------- /practice_7.9/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | exec 2>/dev/null 4 | 5 | categories=("0-50" "50-75" "75-85" "85-95" ">95") 6 | ranges=(50 75 85 95) 7 | 8 | declare -A filesystems 9 | for category in "${categories[@]}"; do 10 | filesystems["$category"]="" 11 | done 12 | 13 | while read -r line; do 14 | [[ "$line" =~ ^Filesystem ]] && continue 15 | [[ -z "$line" ]] && continue 16 | 17 | usage=$(echo "$line" | awk '{print $5}' | tr -d '%') 18 | mount_point=$(echo "$line" | awk '{$1=$2=$3=$4=$5=""; print substr($0,6)}' | sed 's/^[[:space:]]*//') 19 | 20 | if ! [[ "$usage" =~ ^[0-9]+$ ]] || [[ -z "$mount_point" ]]; then 21 | continue 22 | fi 23 | 24 | if (( usage < ranges[0] )); then 25 | filesystems["0-50"]+="$mount_point"$'\n' 26 | elif (( usage < ranges[1] )); then 27 | filesystems["50-75"]+="$mount_point"$'\n' 28 | elif (( usage < ranges[2] )); then 29 | filesystems["75-85"]+="$mount_point"$'\n' 30 | elif (( usage < ranges[3] )); then 31 | filesystems["85-95"]+="$mount_point"$'\n' 32 | else 33 | filesystems[">95"]+="$mount_point"$'\n' 34 | fi 35 | done < dfOutput.txt 36 | 37 | for category in "${categories[@]}"; do 38 | if [[ -n "${filesystems[$category]}" ]]; then 39 | echo "$category" 40 | echo -e -n "${filesystems[$category]}" | sed '/^$/d' 41 | fi 42 | done 43 | -------------------------------------------------------------------------------- /practice_8.1/readme.md: -------------------------------------------------------------------------------- 1 | # Week 8 Problem 1 2 | 3 | A software company has published some best practices for writing the code. One of the best practices mentioned is that no line in your code should exceed 50 characters in total including all types of characters or spaces. 4 | 5 | Write a bash script ` script.sh ` using sed that prints the names of all .c files that contain one or more lines with a length of more than 50 characters (as specified above). 6 | 7 | Hint: 8 | 9 | q [exit-code] 10 | Immediately quit the sed script without processing any more 11 | input, except that if auto-print is not disabled the cur‐ 12 | rent pattern space will be printed. The exit code argument 13 | is a GNU extension. 14 | 15 | Q [exit-code] 16 | Immediately quit the sed script without processing any more 17 | input. This is a GNU extension. 18 | 19 | Note: 20 | - The files will be made available during evaluation. 21 | -------------------------------------------------------------------------------- /practice_8.1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | grep -l '.\{51\}' *.c | sort 4 | -------------------------------------------------------------------------------- /practice_8.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 8 Problem 2 2 | 3 | Write a bash script ` script.sh ` using sed to print the contents of file ` $filename ` in the format ` : `. 4 | -------------------------------------------------------------------------------- /practice_8.2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sed = "$filename" | sed 'N;s/\n/:/' -------------------------------------------------------------------------------- /practice_8.3/readme.md: -------------------------------------------------------------------------------- 1 | # Week 8 Problem 3 2 | 3 | The command ` cut -d " " -f 5 lsoutput ` is executed to extract the total size of the files. But the ` lsoutput ` contains multiple spaces between fields and for some reason we cannot change the cut command. Your task is to preprocess the file ` lsoutput ` to work with the cut command. 4 | 5 | Write a script ` script.sh ` to perform the task. 6 | 7 | Hint: Use the -i option in sed to do the modification in the file. 8 | 9 | Note: 10 | - ` lsoutput ` will be created during evaluation. 11 | -------------------------------------------------------------------------------- /practice_8.3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sed -i 's/ \{2,\}/ /g' lsoutput 4 | cut -d " " -f 5 lsoutput 5 | -------------------------------------------------------------------------------- /practice_8.4/readme.md: -------------------------------------------------------------------------------- 1 | # Week 8 Problem 4 2 | 3 | In python, the multiline comments can be bounded by either ` ''' ` or ` """ `. 4 | 5 | Write a sed script ` script.sed ` that converts all the comments inside the triple quotes to single-line comment (preceded by #) and remove the lines inside the triple quotes that do not have any contents. 6 | 7 | **Sample Input:** 8 | 9 | ```python 10 | print(1) 11 | """ 12 | This is a comment 1 13 | """ 14 | print(2) 15 | """This is a comment 2 16 | """ 17 | print(3) 18 | """ 19 | This is a comment 3""" 20 | print(4) 21 | """This is a comment 4""" 22 | print(5) 23 | """This is a comment 5""" 24 | print(6) 25 | ``` 26 | 27 | **Sample Output** 28 | 29 | ```python 30 | print(1) 31 | #This is a comment 1 32 | print(2) 33 | #This is a comment 2 34 | print(3) 35 | #This is a comment 3 36 | print(4) 37 | #This is a comment 4 38 | print(5) 39 | #This is a comment 5 40 | print(6) 41 | ``` 42 | -------------------------------------------------------------------------------- /practice_8.4/script.sed: -------------------------------------------------------------------------------- 1 | :loop 2 | /"""/ { 3 | N 4 | s/"""\s*\([^"""\n]*\)\s*"""/#\1/ 5 | t 6 | b loop 7 | } 8 | 9 | # Handle inline triple-quote comments 10 | s/"""\([^"""]*\)"""/#\1/ 11 | 12 | # Handle multi-line triple single-quotes 13 | :loop2 14 | /'''/ { 15 | N 16 | s/'''\s*\([^'''\n]*\)\s*'''/#\1/ 17 | t 18 | b loop2 19 | } 20 | 21 | # Handle inline triple single-quote comments 22 | s/'''\([^''']*\)'''/#\1/ -------------------------------------------------------------------------------- /practice_8.5/OPTS: -------------------------------------------------------------------------------- 1 | OPTS="-n" -------------------------------------------------------------------------------- /practice_8.5/readme.md: -------------------------------------------------------------------------------- 1 | # Week 8 Problem 5 2 | 3 | Write a sed script ` script.sed ` to extract the content inside every ` ` tags. 4 | 5 | Print all the non-empty extracted contents with leading and trail spaces trimmed in the source order. 6 | 7 | Note: Create environment array with name ` OPTS ` if more options to sed execution are required. 8 | 9 | **Sample Input** 10 | ``` 11 | 12 | 13 | Example script for sed 14 | 15 | 16 | 17 |

    Welcome to sed programming

    18 |

    sed is a steam editor known for manipulation of text.
    19 | sed can manipulate the text in the pipeline and can be used alond with other commands as well

    20 | Frequently used options with sed 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
    -n, --quiet, --silent suppress automatic printing of pattern space
    -e script, --expression=script add the script to the commands to be executed
    -f script-file, --file=script-file add the contents of script-file to the commands to be executed
    -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied)
    -l N, --line-length=N specify the desired line-wrap length for the `l' command
    -E, -r, --regexp-extended use extended regular expressions in the script (for portability use POSIX -E).
    47 | 48 | 49 | ``` 50 | 51 | **Sample Output** 52 | 53 | ``` 54 | -n, --quiet, --silent 55 | suppress automatic printing of pattern space 56 | -e script, --expression=script 57 | add the script to the commands to be executed 58 | -f script-file, --file=script-file 59 | add the contents of script-file to the commands to be executed 60 | -i[SUFFIX], --in-place[=SUFFIX] 61 | edit files in place (makes backup if SUFFIX supplied) 62 | -l N, --line-length=N 63 | specify the desired line-wrap length for the `l' command 64 | -E, -r, --regexp-extended 65 | use extended regular expressions in the script (for portability use POSIX -E). 66 | ``` 67 | -------------------------------------------------------------------------------- /practice_8.5/script.sed: -------------------------------------------------------------------------------- 1 | //!d 2 | s/^.*\s*\(.*\) \s*<\/td>.*$/\1/ 3 | /^$/d 4 | p -------------------------------------------------------------------------------- /practice_9.1/file.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | if (ARGC != 2) { 3 | exit 1 4 | } 5 | 6 | n = ARGV[1] 7 | 8 | fib1 = 1 9 | fib2 = 1 10 | 11 | if (n == 1 || n == 2) { 12 | print 1 13 | exit 0 14 | } 15 | 16 | for (i = 3; i <= n; i++) { 17 | fib_next = fib1 + fib2 18 | fib1 = fib2 19 | fib2 = fib_next 20 | } 21 | 22 | print fib2 23 | } -------------------------------------------------------------------------------- /practice_9.1/readme.md: -------------------------------------------------------------------------------- 1 | # Week 9 Problem 1 2 | 3 | Write an awk script in ` file.awk ` that can list nth Fibonacci number where ` n ` is a command line argument to the script. 4 | 5 | Note: The sequence is 1,1,2,3,5,..., and the first and the second value are 1 and 1. 6 | -------------------------------------------------------------------------------- /practice_9.2/file.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS = "," 3 | math_sum = 0 4 | physics_sum = 0 5 | chemistry_sum = 0 6 | count = 0 7 | } 8 | 9 | NR > 1 { 10 | math_sum += $2 11 | physics_sum += $3 12 | chemistry_sum += $4 13 | count++ 14 | } 15 | 16 | END { 17 | print math_sum / count 18 | print physics_sum / count 19 | print chemistry_sum / count 20 | } -------------------------------------------------------------------------------- /practice_9.2/readme.md: -------------------------------------------------------------------------------- 1 | # Week 9 Problem 2 2 | 3 | Write an awk script ` file.awk ` to calculate class average marks of each subject and print each subject average in a new line. 4 | 5 | - The first line of output represents the class average of Mathematics. 6 | - The second line of output represents the class average of Physics. 7 | - The third line of output represents the class average of Chemistry. 8 | 9 | The marks are given in the comma separated file ` marks.csv `. 10 | 11 | Note: 12 | - ` marks.csv ` will be available during evaluation. 13 | -------------------------------------------------------------------------------- /practice_9.3/readme.md: -------------------------------------------------------------------------------- 1 | # Week 9 Problem 3 2 | 3 | Write an awk script ` union.awk ` to get when combined, the distinct (union) lines among two files ` file1 ` and ` file2 `. 4 | 5 | The printed lines are based on the sequence of occurrence from ` file1 ` to ` file2 `. 6 | 7 | Hint: Use the predefined variables NR and FNR 8 | -------------------------------------------------------------------------------- /practice_9.3/union.awk: -------------------------------------------------------------------------------- 1 | { 2 | if (!seen[$0]++) { 3 | print $0; 4 | } 5 | } -------------------------------------------------------------------------------- /practice_9.4/readme.md: -------------------------------------------------------------------------------- 1 | # Week 9 Problem 4 2 | 3 | Write an AWK script in ` script.awk ` to count the number of lines in the file. 4 | -------------------------------------------------------------------------------- /practice_9.4/script.awk: -------------------------------------------------------------------------------- 1 | { 2 | count++ 3 | } 4 | 5 | END { 6 | print count 7 | } -------------------------------------------------------------------------------- /practice_9.5/readme.md: -------------------------------------------------------------------------------- 1 | # Week 9 Problem 5 2 | 3 | The ` uniq ` command in the Linux will not print the adjacent duplicate lines. Write an AWK script ` script.awk ` to the same. 4 | -------------------------------------------------------------------------------- /practice_9.5/script.awk: -------------------------------------------------------------------------------- 1 | { 2 | if ($0 != prev) { 3 | print 4 | } 5 | prev = $0 6 | } -------------------------------------------------------------------------------- /q-awk-1/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You are the disk jockey at a local radio station. 4 | Your job is to create a playlist of songs to play in the upcoming event 5 | of New Year's Eve. You have a dataset of songs available to be played. 6 | As most people will be at a party, you want to play songs that are upbeat and 7 | danceable. For this you decide to play only musics in a major scale and with 8 | a BPM (beats per minute) of at least 120. But if a music has a danceability 9 | higher than 80% despite not satisfying the above condition, you will still 10 | consider it. As it is a New Year's Eve party, you only want to play songs that 11 | are released in this year itself. The current year is stored as an 12 | environment variable `YEAR` when your script is run. 13 | 14 | Given the dataset `spotify.csv` containing details about songs on Spotify music streaming 15 | platform, write a script using awk to find the songs that you will play in the 16 | upcoming event of New Year's Eve. The output should be in the order of the 17 | original dataset. To test multiple test cases, assume that the dataset is 18 | given to your script in standard input. 19 | Write your solution as a bash script using awk in a `script.sh` file. 20 | Output should be in the format: 21 | 22 | ```text 23 | track_name - artist(s)_name 24 | ``` 25 | 26 | ## Input Format 27 | 28 | ```csv 29 | track_name,artist(s)_name,artist_count,released_year,released_month,released_day,in_spotify_playlists,in_spotify_charts,streams,in_apple_playlists,in_apple_charts,in_deezer_playlists,in_deezer_charts,in_shazam_charts,bpm,key,mode,danceability_%,valence_%,energy_%,acousticness_%,instrumentalness_%,liveness_%,speechiness_% 30 | ``` 31 | 32 | ## Sample Input 33 | 34 | ```csv 35 | Seven (feat. Latto) (Explicit Ver.),"Latto, Jung Kook",2,2023,7,14,553,147,141381703,43,263,45,10,826,125,B,Major,80,89,83,31,0,8,4 36 | LALA,Myke Towers,1,2023,3,23,1474,48,133716286,48,126,58,14,382,92,C#,Major,71,61,74,7,0,10,4 37 | vampire,Olivia Rodrigo,1,2023,6,30,1397,113,140003974,94,207,91,14,949,138,F,Major,51,32,53,17,0,31,6 38 | Cruel Summer,Taylor Swift,1,2019,8,23,7858,100,800840817,116,207,125,12,548,170,A,Major,55,58,72,11,0,11,15 39 | WHERE SHE GOES,Bad Bunny,1,2023,5,18,3133,50,303236322,84,133,87,15,425,144,A,Minor,65,23,80,14,63,11,6 40 | Sprinter,"Dave, Central Cee",2,2023,6,1,2186,91,183706234,67,213,88,17,946,141,C#,Major,92,66,58,19,0,8,24 41 | Ella Baila Sola,"Eslabon Armado, Peso Pluma",2,2023,3,16,3090,50,725980112,34,222,43,13,418,148,F,Minor,67,83,76,48,0,8,3 42 | Columbia,Quevedo,1,2023,7,7,714,43,58149378,25,89,30,13,194,100,F,Major,67,26,71,37,0,11,4 43 | fukumean,Gunna,1,2023,5,15,1096,83,95217315,60,210,48,11,953,130,C#,Minor,85,22,62,12,0,28,9 44 | ``` 45 | 46 | ## Sample Output 47 | 48 | ```txt 49 | Seven (feat. Latto) (Explicit Ver.) - "Latto, Jung Kook" 50 | vampire - Olivia Rodrigo 51 | Sprinter - "Dave, Central Cee" 52 | fukumean - Gunna 53 | ``` 54 | -------------------------------------------------------------------------------- /q-awk-1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | awk -v FPAT='([^,]+)|(\"[^\"]+\")' -v YEAR="$YEAR" -f script.awk 4 | -------------------------------------------------------------------------------- /q-awk-2/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You perform a survey on students to determine the effects of gender, 4 | race, parent's education, lunch type, and test preparation courses on 5 | exam performance of math, reading, and writing. 6 | 7 | Perform the following statistics on the data: 8 | 9 | - Print the mean score for math, reading, and writing scores. 10 | - Print the percentage of students who are female and male. 11 | - Print the percentage of students whose parents have gone 12 | to college. 13 | - Find the probability that a student selected at random 14 | gets a mean score of 80 or more in all three subjects given 15 | that the student's parents have gone to college. 16 | - Find the probability that a student selected at random 17 | gets a mean score of 80 or more in all three subjects given 18 | that the student's parents have not gone to college. 19 | 20 | Statistically, $P(A|B) = \frac{P(A \cap B)}{P(B)}$. 21 | 22 | CSV header will **not** be present in the input. 23 | 24 | ## Input Format 25 | 26 | ```csv 27 | "gender","race_ethnicity","parental_level_of_education","lunch","test_preparation_course","math_score","reading_score","writing_score" 28 | ``` 29 | 30 | ## Sample Input 31 | 32 | ```csv 33 | "female","group B","bachelor's degree","standard","none","72","72","74" 34 | "female","group C","some college","standard","completed","69","90","88" 35 | "female","group B","master's degree","standard","none","90","95","93" 36 | "male","group A","associate's degree","free/reduced","none","47","57","44" 37 | "male","group C","some college","standard","none","76","78","75" 38 | "female","group B","associate's degree","standard","none","71","83","78" 39 | "female","group B","some college","standard","completed","88","95","92" 40 | "male","group B","some college","free/reduced","none","40","43","39" 41 | "male","group D","high school","free/reduced","completed","64","64","67" 42 | ``` 43 | 44 | ## Sample Output 45 | 46 | ```txt 47 | math average 68.56 48 | read average 75.22 49 | write average 72.22 50 | female percent 55.56 51 | male percent 44.44 52 | parent college% 88.89 53 | P(>79|college) 37.50 54 | P(>79|~college) 0 55 | ``` 56 | -------------------------------------------------------------------------------- /q-awk-2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | awk -f script.awk 4 | -------------------------------------------------------------------------------- /q-awk-3/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You are given a csv file with a lot of columns. 4 | You want to write some awk scripts to process the file. 5 | But it is hard for you to remember the column number of each column. 6 | 7 | Write a preprocessing awk script that will 8 | take the csv file and print out the column names 9 | from the header line with the column number. 10 | 11 | Assume input is passed to your `script.sh` as stdin. 12 | 13 | **Sample Input** 14 | 15 | ```csv 16 | name,age,gender,city 17 | Bob,21,M,New York 18 | Alice,22,F,Los Angeles 19 | ``` 20 | 21 | **Sample Output** 22 | 23 | ```txt 24 | 1 name 25 | 2 age 26 | 3 gender 27 | 4 city 28 | ``` 29 | -------------------------------------------------------------------------------- /q-awk-3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | awk -f script.awk 4 | -------------------------------------------------------------------------------- /q-awk-4/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You are working on a data file for a Machine Learning Project, 4 | but the data might be corrupted somehow during transit. 5 | 6 | The file is supposed to be separated by some delimiter, 7 | and all records should have the same number of fields. 8 | 9 | You see that the field delimiters are correct, but 10 | the record delimiters have also become same as the 11 | field delimiter. 12 | 13 | You know the number of columns per record, and 14 | it is stored in the bash variable `N`. The delimiter 15 | for the fields is stored in the bash variable `DELIMITER`. 16 | 17 | You have to write an AWK script that reverts this corruption. 18 | 19 | Assume that the data is given to your `script.sh` as stdin. 20 | 21 | The data does not have any header rows. 22 | 23 | **Sample Input** 24 | 25 | ```text 26 | 1,2,3,4,5,6,7,8,9,10,11,12 27 | ``` 28 | 29 | N = 3 30 | DELIMITER = ',' 31 | 32 | **Sample Output** 33 | 34 | ```text 35 | 1,2,3 36 | 4,5,6 37 | 7,8,9 38 | 10,11,12 39 | ``` 40 | -------------------------------------------------------------------------------- /q-awk-4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | awk -v FS="$DELIMITER" -v N="$N" -f script.awk 4 | -------------------------------------------------------------------------------- /q-awk-5/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You are working on a file which was written in an operating system 4 | called Windows which unlike other operating system uses '\r\n' as a 5 | line ending instead of the usual '\n'. You have to convert the file 6 | into a format which is supported by other operating systems. Write an 7 | AWK script to convert the file. 8 | 9 | Assume input is provided as standard input. 10 | -------------------------------------------------------------------------------- /q-awk-5/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | awk -f script.awk 4 | -------------------------------------------------------------------------------- /q-bash-1/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | Write a `script.sh` which will take input from standard input and write output 4 | to standard output. 5 | 6 | The input is the `/etc/passwd` file. Your script should read the input 7 | line by line using `while` loop with `read` and perform the following: 8 | 9 | - If the line is a comment, then skip it. 10 | - If the line is not a comment, then print the username and home directory 11 | of the user. 12 | - If the user has a description (comment) in the GECOS field, 13 | then print the description instead of the username. 14 | - If the home directory is of the form `/home/username`, 15 | then print '/home/$USER' literally. 16 | - If not printing the username, 17 | then print the home directory as it is. 18 | 19 | Your script should only print regular users, not system users. (Hint: UID >= 1000) 20 | 21 | Your script should delimit records using '---' 22 | -------------------------------------------------------------------------------- /q-bash-1/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Users:" 4 | while read -r line; do 5 | if [[ $line =~ ^# ]]; then 6 | continue 7 | fi 8 | IFS=":" read -r username x uid gid comment home shell <<< "$line" 9 | if [[ $uid -lt 1000 ]]; then 10 | continue 11 | fi 12 | echo "---" 13 | if [[ -n $comment ]]; then 14 | echo "Comment: $comment" 15 | elif [[ "$home" = "/home/$username" ]]; then 16 | home="/home/\$USER" 17 | echo "Username: $username" 18 | else 19 | echo "Username: $username" 20 | fi 21 | echo "Home Directory: $home" 22 | done 23 | -------------------------------------------------------------------------------- /q-bash-2/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | > the test cases have been changed on Apr 04 21:45 4 | 5 | Write a `script.sh` which will take input from standard input and write output 6 | to standard output. 7 | You are given heart attack data. From the given data through standard input, 8 | print only the lines that have outliers in the heart rate field. The data 9 | may or may not have a header row. The heart rate field is the 3rd field in 10 | the data. The heart rate field is an integer. An outlier is a value that is 11 | more than 1.5 times the interquartile range from the third quartile, or less 12 | than 1.5 times the interquartile range from the first quartile. 13 | 14 | **Header format**: 15 | 16 | ```csv 17 | Age,Gender,Heart rate,Systolic blood pressure,Diastolic blood pressure,Blood sugar,CK-MB,Troponin,Result 18 | ``` 19 | -------------------------------------------------------------------------------- /q-bash-2/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat > data 4 | sed -i '/^Age,/d' data 5 | sort -t, -k3 data -o data 6 | mapfile -t heartrates < <(cut -d, -f3 data) 7 | lines=${#heartrates[@]} 8 | if [ $((lines % 2)) -eq 0 ]; then 9 | Q1=$((lines/4)) 10 | Q3=$((3*lines/4)) 11 | else 12 | Q1=$(( (lines+1)/4 )) 13 | Q3=$(( 3*(lines+1)/4 )) 14 | fi 15 | Q1=${heartrates[$Q1]} 16 | Q3=${heartrates[$Q3]} 17 | IQR=$((Q3-Q1)) 18 | lower=$(echo "$Q1 - 1.5 * $IQR" | bc) 19 | upper=$(echo "$Q3 + 1.5 * $IQR" | bc) 20 | awk -F, -v lower="$lower" -v upper="$upper" '$3 < lower || $3 > upper' data 21 | # while read -r line; do 22 | # IFS=, read -r age gender heartrate sys dias bsl ckmb trop res <<< "$line" 23 | # if [[ $(bc <<< "$heartrate < $lower") -eq 1 || $(bc <<< "$heartrate > $upper") -eq 1 ]] ; then 24 | # echo "$line" 25 | # fi 26 | # done < data 27 | 28 | 29 | 30 | rm data 31 | -------------------------------------------------------------------------------- /q-bash-3/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | Write a `script.sh` which will take input from standard input and write output 4 | to standard output. 5 | You are given two files, file1 and file2 in the standard input. 6 | The content of two files is separated by a line 'EOF'. 7 | Print the following information: 8 | 9 | - Lines that are common in both files. 10 | - Lines that are only in file1. 11 | - Lines that are only in file2. 12 | 13 | Order of the output should be unchanged. 14 | -------------------------------------------------------------------------------- /q-bash-3/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | while read -r line ; do 4 | if [[ $line = "EOF" ]]; then 5 | break 6 | fi 7 | echo "$line" >> file1 8 | done 9 | while read -r line ; do 10 | echo "$line" >> file2 11 | done 12 | 13 | # echo "Common:" 14 | # comm -12 <(sort file1) <(sort file2) 15 | # echo "Unique to file1:" 16 | # comm -23 <(sort file1) <(sort file2) 17 | # echo "Unique to file2:" 18 | # comm -13 <(sort file1) <(sort file2) 19 | # ^^ This works if order of output can be sorted 20 | 21 | echo "Common:" 22 | grep -Fxf file1 file2 23 | echo "Unique to file1:" 24 | grep -Fvxf file2 file1 25 | echo "Unique to file2:" 26 | grep -Fvxf file1 file2 27 | 28 | rm file1 file2 29 | -------------------------------------------------------------------------------- /q-bash-4/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | Write a `script.sh` which will take input from standard input and write output 4 | to standard output. 5 | You are given the advertising dataset of ISLR book. 6 | Perform SLR on the dataset to predict sales using TV as a predictor. 7 | Print the coefficients of the model. 8 | 9 | Assume the dataset is sent to the script via standard input. 10 | 11 | **Header Format**: 12 | 13 | ```csv 14 | TV,Radio,Newspaper,Sales 15 | ``` 16 | -------------------------------------------------------------------------------- /q-bash-4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sed '/^TV/d' | col > data 4 | mapfile -t x_values < <(cut -d, -f1 data) 5 | mapfile -t y_values < <(cut -d, -f4 data) 6 | 7 | num_points=${#x_values[@]} 8 | 9 | sum_x=0 10 | sum_y=0 11 | for ((i=0; i temp 4 | for os in Macintosh Windows Linux iPhone; do 5 | echo "$os: $(grep -wc "$os" temp)" 6 | done 7 | rm temp 8 | -------------------------------------------------------------------------------- /q-grep-4/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | Write a `script.sh` which will take input from standard input and write output 4 | to standard output. 5 | You are given an access log file to your standard input similar to previous 6 | question. You are to print the count of the HTTP VERBS in the file. The HTTP 7 | VERBS are GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD. 8 | 9 | The output should be in descending order of the count of the HTTP VERBS. 10 | VERBS with count 0 should not be printed. VERBS with same count should be 11 | printed in the order of their name. 12 | 13 | **Hint**: Be familiar with how loops work, how you can redirect stdout of 14 | a loop to another command, and how to sort the output of a command based 15 | on any arbitrary column. 16 | 17 | **Sample Output** 18 | 19 | ``` 20 | GET 10 21 | POST 5 22 | DELETE 3 23 | PUT 2 24 | OPTIONS 1 25 | ``` 26 | -------------------------------------------------------------------------------- /q-grep-4/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat > temp 4 | for verb in GET POST PUT DELETE PATCH OPTIONS HEAD; do 5 | echo "$verb $(grep -wc "$verb /" temp)" 6 | done | grep -v " 0$" | sort -k2 -nr 7 | 8 | rm temp 9 | -------------------------------------------------------------------------------- /q-grep-5/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | Write a `script.sh` which will take input from standard input and write output 4 | to standard output. 5 | You are given an access log file to your standard input similar to previous 6 | question. You are to print the count of the hours at which requests were sent 7 | along with the hour. 8 | 9 | The output should be in descending order of the count of the requests per hour. 10 | 11 | The output should only show hours having more than 9 requests. 12 | 13 | **Sample Output** 14 | 15 | ``` 16 | 634 04 17 | 29 00 18 | 22 14 19 | 21 06 20 | 17 05 21 | 14 03 22 | 12 10 23 | 12 09 24 | 12 07 25 | 12 01 26 | 10 12 27 | 10 11 28 | ``` 29 | -------------------------------------------------------------------------------- /q-grep-5/script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cut -d':' -f2 | sort | uniq -c | sort -nr | grep -oE '[0-9]{2,} [0-9]+' 4 | 5 | -------------------------------------------------------------------------------- /q-sed-1/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You are given a spy's message which is encrypted using 4 | ROT13 technique. You have to decrypt the message and return the original message. 5 | Write a sed script (script.sed) to accomplish this task. 6 | 7 | The ROT13 technique is a simple letter substitution cipher that replaces a 8 | letter with the 13th letter after it, in the alphabet. 9 | ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. 10 | Any character other than alphabet should be left as it is. 11 | 12 | Assume input comes in from standard in. 13 | 14 | Try to solve this problem using one line sed command. 15 | 16 | **Sample Input**: 17 | 18 | ```text 19 | uryyb 20 | ``` 21 | 22 | **Sample Output**: 23 | 24 | ```text 25 | hello 26 | ``` 27 | -------------------------------------------------------------------------------- /q-sed-1/script.sed: -------------------------------------------------------------------------------- 1 | #!/bin/sed -f 2 | 3 | y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/ 4 | -------------------------------------------------------------------------------- /q-sed-2/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You have a text file which has some lines folded 4 | using a backslash at the end of the line. Write a 5 | program to unfold the text file. Assume standard 6 | input contains the text file. The program should 7 | print the unfolded text to the standard output. 8 | It should not remove the other backslashes from 9 | the text file. 10 | 11 | For example, the text file may look like this: 12 | 13 | ```text 14 | Hello\, this is \ 15 | a text file. 16 | It has some lines \ 17 | folded using a backslash \ 18 | at the end of the line. 19 | ``` 20 | 21 | The program should unfold the text file and print 22 | the following: 23 | 24 | ```text 25 | Hello\, this is a text file. 26 | It has some lines folded using a backslash at the end of the line. 27 | ``` 28 | -------------------------------------------------------------------------------- /q-sed-2/script.sed: -------------------------------------------------------------------------------- 1 | #!/bin/sed -f 2 | 3 | :x 4 | $!N 5 | s/\\\n//g 6 | tx 7 | P 8 | D 9 | -------------------------------------------------------------------------------- /q-sed-3/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | Write sed script that are run from your 4 | `script.sh` bash script 5 | that performs the following operations 6 | on the given standard input: 7 | 8 | - Replace all occurrences of the word "the" with "this" 9 | - Add the text "#---" before each function definition 10 | - Add copyright information "# Copyright 2024" at the beginning of the file 11 | - After every 5 lines, add the line number as a comment 12 | 13 | A function definition is a line that starts with the word "function" followed by a space. 14 | The line should contain the symbols `()` and `{`. 15 | The function definition may contain any number of spaces before or after the symbols. 16 | The `{` should be the last character on the line, excluding any trailing spaces or tabs. 17 | The word function can be preceded by any number of spaces or tabs. 18 | 19 | Your `script.sh` should use sed commands to accomplish this. 20 | -------------------------------------------------------------------------------- /q-sed-3/script.sed: -------------------------------------------------------------------------------- 1 | #!/bin/sed -f 2 | 3 | 1i# Copyright 2024 4 | s/\bthe\b/this/g 5 | /^[[:space:]]*function.*()[[:space:]]*{[[:space:]]*$/i#--- 6 | 5~5{ 7 | i# 8 | = 9 | } 10 | 11 | -------------------------------------------------------------------------------- /q-sed-4/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You are given a CSV file as input in the standard input. 4 | The file contains records of data from a database. 5 | The data contains the following fields: 6 | 7 | - Name 8 | - Age 9 | - Gender 10 | - City 11 | - Address 12 | 13 | The address field can contain multiple lines of text. It is always enclosed in double quotes. 14 | 15 | Write a sed script (script.sed) which will convert each record into a single 16 | line of CSV format. The address field should be stripped of newlines and quoted. 17 | The newlines should be replaced with a comma and a space. 18 | -------------------------------------------------------------------------------- /q-sed-4/script.sed: -------------------------------------------------------------------------------- 1 | 2 | :x 3 | 1!{ 4 | /[^"]$/{ 5 | N 6 | s/\n/, / 7 | b x 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /q-sed-5/readme.md: -------------------------------------------------------------------------------- 1 | # Question 2 | 3 | You have been given a minified JSON string. 4 | You have to write a sed script (script.sed) that 5 | will pretty print the JSON string. 6 | 7 | The important data is stored as an JSON object in the key "data". 8 | Print only the data object. 9 | 10 | **Sample Input**: 11 | 12 | ```json 13 | { "data": { "name": "John", "age": 30, "city": "New York" } } 14 | ``` 15 | 16 | **Sample Output**: 17 | 18 | ```json 19 | { 20 | "name": "John", 21 | "age": 30, 22 | "city": "New York" 23 | } 24 | ``` 25 | 26 | Assume there is no nested JSON object. 27 | 28 | Bonus: Can you write a sed script that works for nested JSON objects? 29 | Also look into the `jq` command. 30 | -------------------------------------------------------------------------------- /q-sed-5/script.sed: -------------------------------------------------------------------------------- 1 | #!/bin/sed -f 2 | 3 | s/{ "data": // 4 | s/}$// 5 | s/{/{\n\t/g 6 | s/}/\n}/g 7 | s/,/,\n\t/g 8 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ![GitHub commit activity](https://img.shields.io/github/commit-activity/w/BriskAM/SC-E) 2 | 3 | # IIT Madras System Commands Solutions 4 | 5 | A comprehensive collection of solutions for IIT Madras System Commands Virtual Machine assignments and exercises. This repository provides tested solutions for various Linux/Unix command-line tasks and system administration problems. 6 | 7 | ## Features 8 | 9 | - Complete solutions for all assignments and exercises 10 | - Well-documented code with explanations 11 | - Tested on IIT Madras VM environment 12 | - Easy-to-follow implementation steps 13 | 14 | ## Table of Contents 15 | 16 | - [Installation](#installation) 17 | - [Usage](#usage) 18 | - [Command Usage](#command-usage) 19 | - [Available Commands](#available-commands) 20 | - [Best Practices](#best-practices) 21 | - [Documentation](#documentation) 22 | - [License](#license) 23 | 24 | ## Installation 25 | 26 | 1. SSH into the provided remote VM 27 | 2. Create necessary directories in the `~/se2001` path 28 | 3. Clone or copy the solution scripts to appropriate locations 29 | 30 | ## Usage 31 | 32 | ### Prerequisites 33 | 34 | - Access to IIT Madras remote VM 35 | - All assignments located in `~/se2001` directory 36 | - Basic understanding of Linux commands 37 | 38 | ### Command Usage 39 | 40 | ```bash 41 | synchro COMMAND 42 | ``` 43 | 44 | ### Available Commands 45 | 46 | - `synchro show` - Display current ASSIGNMENT statement 47 | - `synchro init` - Initialize new ASSIGNMENT 48 | - `synchro eval` - Evaluate ASSIGNMENT solution 49 | - `synchro status` - Check submission status 50 | - `synchro help` - Show command help 51 | 52 | ### Best Practices 53 | 54 | 1. Always run `synchro` commands from the assignment directory 55 | 2. Verify solutions before submission 56 | 3. Follow proper file naming conventions 57 | 4. Keep backups of your work 58 | 59 | ## Documentation 60 | 61 | Detailed documentation for each assignment and exercise is available in their respective directories. Each solution includes: 62 | 63 | - Problem statement 64 | - Solution approach 65 | - Implementation details 66 | - Usage examples 67 | 68 | ## License 69 | 70 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 71 | 72 | ## Contributing 73 | 74 | Contributions are welcome! Please feel free to submit pull requests or open issues for improvements. 75 | -------------------------------------------------------------------------------- /regex.md: -------------------------------------------------------------------------------- 1 | # Regular Expressions 2 | 3 | 4 | ## Basic Syntax 5 | 6 | - `/.../`: Start and end regex delimiters 7 | - `|`: Alternation 8 | - `()`: Grouping 9 | 10 | 11 | ## Position Matching 12 | 13 | - `^`: Start of string or start of line in multi-line mode 14 | - `\A`: Start of string 15 | - `$`: End of string or end of line in multi-line mode 16 | - `\Z`: End of string 17 | - `\b`: Word boundary 18 | - `\B`: Not word boundary 19 | - `\<`: Start of word 20 | - `\>`: End of word 21 | 22 | 23 | ## Character Classes 24 | 25 | - `\s`: Whitespace 26 | - `\S`: Not whitespace 27 | - `\w`: Word 28 | - `\W`: Not word 29 | - `\d`: Digit 30 | - `\D`: Not digit 31 | - `\x`: Hexade­cimal digit 32 | - `\O`: Octal digit 33 | 34 | 35 | ## Special Characters 36 | 37 | - `\n`: Newline 38 | - `\r`: Carriage return 39 | - `\t`: Tab 40 | - `\v`: Vertical tab 41 | - `\f`: Form feed 42 | - `\xxx`: Octal character xxx 43 | - `\xhh`: Hex character hh 44 | 45 | 46 | ## Groups and Ranges 47 | 48 | - `.`: Any character except newline (\n) 49 | - `(a|b)`: a or b 50 | - `(…)`: Group 51 | - `(?:…)`: Passive (non-c­apt­uring) group 52 | - `[abc]`: a, b or c 53 | - `[^abc]`: Not a, b or c 54 | - `[a-z]`: Letters from a to z 55 | - `[A-Z]`: Uppercase letters from A to Z 56 | - `[0-9]`: Digits from 0 to 9 57 | 58 | > Note: Ranges are inclusive. 59 | 60 | 61 | ## Quantifiers 62 | 63 | - `*`: 0 or more 64 | - `+`: 1 or more 65 | - `?`: 0 or 1 66 | - `{3}`: Exactly 3 67 | - `{3,}`: 3 or more 68 | - `{3,5}`: 3, 4 or 5 69 | 70 | > Note: Quantifiers are greedy - they match as many times as possible. Add a ? after the quantifier to make it ungreedy. 71 | 72 | 73 | ## Escape Sequences 74 | 75 | - `\`:Escape following character. Used to escape any of the following metacharacters: {}[]()^$.|*+?\. 76 | - `\Q`: Begin literal sequence 77 | - `\E`: End literal sequence 78 | 79 | 80 | ## String Replacement 81 | 82 | - `$1`: 1st group 83 | - `$2`: 2nd group 84 | - `$n`: nth group 85 | - `$``: Before matched string 86 | - `$'`: After matched string 87 | - `$+`: Last matched string 88 | - `$&`: Entire matched string 89 | 90 | > Note: Some regex implem­ent­ations use \ instead of $. 91 | 92 | 93 | ## Assertions 94 | 95 | - `?=`: Lookahead assertion 96 | - `?!`: Negative lookahead 97 | - `?<=`: Lookbehind assertion 98 | - ``?!=, ?`: Once-only subexp­ression 100 | - `?()`: Condition if-then 101 | - `?()|`: Condition if-then-else 102 | - `?#`: Comment 103 | 104 | 105 | ## POSIX 106 | 107 | - `[:upper:]`: Uppercase letters 108 | - `[:lower:]`: Lowercase letters 109 | - `[:alpha:]`: All letters 110 | - `[:alnum:]`: Digits and letters 111 | - `[:digit:]`: Digits 112 | - `[:xdigit:]`: Hexade­cimal digits 113 | - `[:punct:]`: Punctu­ation 114 | - `[:blank:]`: Space and tab 115 | - `[:space:]`: Blank characters 116 | - `[:cntrl:]`: Control characters 117 | - `[:graph:]`: Printed characters 118 | - `[:print:]`: Printed characters and spaces 119 | - `[:word:]`: Digits, letters and underscore 120 | 121 | 122 | ## Pattern Modifiers 123 | 124 | - `g`: Global match 125 | - `i`: Case-i­nse­nsitive 126 | - `m`: Multi-line mode. Causes ^ and $ to also match the start/end of lines. 127 | - `s`: Single-line mode. Causes . to match all, including line breaks. 128 | - `x`: Allow comments and whitespace in pattern 129 | - `e`: Evaluate replac­ement 130 | - `U`: Ungreedy mode --------------------------------------------------------------------------------