themercee
5/29/2019 - 1:34 PM

bash basic script

Basic template script bash, dev, linux, script

#!/bin/bash

echo "Going to blabla"
# Arg from cmd
echo Variable one $1

# -z check if $1 exist
if [ -z $1 ]
then
  echo Do zzz
else
  read something
  echo Do $something
fi

# Compare strings
str1="Hello Bash"
str2="Hello Bash"
 
if [ "$str1" == "$str2" ]; then
    echo "Strings are equal"
else
    echo "Strings are not equal"
fi

# Check if string in another
string='My long string'
if [[ $string == *"My long"* ]]; then
  echo "It's there!"
fi

# For one liner
for i in {1..5}; do echo $i; done
# Validate not equal
if [ $# != 3]; then
  echo Need 3 arguments!
fi

# Test file exist (use -d to test directory)
if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

# God damn one liner
if [ -f foo.txt ]; then echo hello; fi