Snippets

Víctor Goñi Sanz Bash tutorial 1 - Conceptos básicos

Created by Víctor Goñi Sanz last modified
#!/bin/bash
# ^ Above line indicate is a bash script

clear # Clear the terminal

# Below line is a comment for my first script, this line is not executed
echo "------- My first script begins execution..."
echo "--------------------------------------------"

# Variable declaration, just stringname, variable value ${stringname}, "" respect blank spaces inside string
intvariable=0
inputpath="./input"                 # Relative path, you can change it
outputpath="./output"
filename=$(basename "myfile.dat")   # Strip directory and suffix from filenames, check man basename
filenameraw="${filename%.*}"        # Extract string before . from variable filename
extension="${filename##*.}"         # Extract string after . from variable filename

# Add value to variable
let "intvariable += 5"

# echo allow you to print messages, variables values, and special characters using \ before them
echo "  If you want to print a special character use \\ before, # is not, \$ is a special characters"
echo "  Variable with type int intvariable, \${intvariable} contains ${intvariable}"
echo "  Variable with type string inputpath, \${inputpath} contains ${inputpath}"
echo "  Variable with type string outputpath, \${outputpath} contains ${outputpath}"

# Variables unassigned, more: http://www.tldp.org/LDP/abs/html/varassignment.html
if [ -z "$unassigned" ] # Check if exist, z option true if size is zero
then
  echo "  An unassigned variable no exist, so \${unassigned} = NULL = ${unassigned}"
fi

# if usage manual here: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
# This if create input and output only if no exist
echo "  Check if exist input path..."
if [ ! -d "${inputpath}" ]; then  # d true if FILE exists and is a directory, ! = not
  echo "  Creating input folder..."    # If no exist directory, create
  mkdir input
else
  echo "  Input folder already exists."
fi

# same as input
echo "  Check if exist output path..."
if [ ! -d "${outputpath}" ] # d for directory
then
  echo "  Creating output folder..."
  mkdir output
else
  echo "  Output folder already exists."
fi

# if output exist...
if [ -d "${outputpath}" ] # d for directory
then
  # Check if exist a file, make a copy, if not create it
  echo "  Check if exist file: ${filenameraw}, with extension ${extension}, full name is: ${filename}"
  if [ -f "${inputpath}/${filename}" ]  #  f for file
  then
    echo "  File: ${filename} exists, so I do a copy in output path";
    cp ${inputpath}/${filename} ${outputpath}/${filenameraw}.${extension}.bak
  else
    echo "  File: ${filename} not found, creating...";
    touch ${inputpath}/${filename}
  fi;

  # while usage, http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_02.html
  # comparation conditions: http://tldp.org/LDP/abs/html/comparison-ops.html
  # lets create N files automatically using while
  N=5
  counterwhile=0
  while [  ${counterwhile} -lt ${N} ]  # -lt option means lesser than
  do
     touch ${outputpath}/${filenameraw}_${counterwhile}.${extension}
     let counterwhile=counterwhile+1  # Forward
  done

  # for usage,
  echo "  List with files in ${outputpath}: "
  counter=0
  for file in "${outputpath}"/* # * = All items, in output path
  do
      echo "  Filename ${counter}: ${file##*/}   -    full: ${file}"
      let "counter += 1"  # Forward
  done

  # ls works
  echo "  ls show a list of all of them too: "
  ls ${outputpath}
else
  echo "  Output path not exist! :-( this cannot be happening"
fi;

echo "------- My first script end now!"
echo "---------------------------------"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.