LINUX - sudo-arshia/tips_and_tricks GitHub Wiki

  1. Bash Basics Example:
#!/bin/bash

# This is a simple Bash script
# It prints "Hello, world!" to the terminal

echo "Hello, world!"
  1. 12 Bash Principles Example:
#!/bin/bash

# Principle 1: Everything is a file
file="/path/to/file.txt"

# Principle 2: Commands and programs
ls -l

# Principle 3: Input and output redirection
command > output.txt
command < input.txt

# Principle 4: Pipes
command1 | command2

# Principle 5: Variables and environment
name="John"
echo "Hello, $name"

# Principle 6: Command substitution
result=$(command)

# Principle 7: Exit status
if [ $? -eq 0 ]; then
    echo "Success"
else
    echo "Failure"
fi

# Principle 8: Conditional statements
if [ condition ]; then
    # Code to run if condition is true
else
    # Code to run if condition is false
fi

# Principle 9: Looping constructs
for i in {1..5}; do
    echo $i
done

# Principle 10: Functions
my_function() {
    echo "Hello from function"
}
my_function

# Principle 11: Scripting and automation
#!/bin/bash
# Script content...

# Principle 12: Text processing
grep "pattern" file.txt
  1. Additional Basics Examples:

Example 1: find command

find /path/to/directory -name "*.txt"

Example 2: awk command with delimiter

awk -F"," '{ print $1 }' file.csv

Example 3: Checking if an application is running on localhost

if pgrep -x "application_name" > /dev/null; then
    echo "Application is running"
else
    echo "Application is not running"
fi

Example 4: Generating an endpoint in Linux

sudo ifconfig eth0 192.168.0.10 netmask 255.255.255.0

Example 5: curl command with GET request and query parameters

curl -X GET "http://example.com/query?a=value1&b=value2"

Example 6: Nginx "Address already in use" issue This error occurs when another process is already using the same port that Nginx is trying to bind to. To resolve it, you can either stop the conflicting process or configure Nginx to use a different port.

Example 7: Finding process using a specific port

sudo lsof -i :8080

Example 8: Including the date in crontab output

* * * * * echo "$(date) My command"

Example 9: Including the date and executing a curl command in crontab

* * * * * echo "$(date) $(curl -s http://example.com)"

These examples demonstrate various concepts and commands in Bash scripting and Linux system administration.