#!/bin/bash# ^ Above line indicate is a bash scriptclear # Clear the terminal# Below line is a comment for my first script, this line is not executedecho"------- My first script begins execution..."echo"--------------------------------------------"# Variable declaration, just stringname, variable value ${stringname}, "" respect blank spaces inside stringintvariable=0inputpath="./input"# Relative path, you can change itoutputpath="./output"filename=$(basename "myfile.dat")# Strip directory and suffix from filenames, check man basenamefilenameraw="${filename%.*}"# Extract string before . from variable filenameextension="${filename##*.}"# Extract string after . from variable filename# Add value to variablelet"intvariable += 5"# echo allow you to print messages, variables values, and special characters using \ before themecho" 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.htmlif[ -z "$unassigned"]# Check if exist, z option true if size is zerothenecho" 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 existecho" Check if exist input path..."if[ ! -d "${inputpath}"];then# d true if FILE exists and is a directory, ! = notecho" Creating input folder..."# If no exist directory, create mkdir input
elseecho" Input folder already exists."fi# same as inputecho" Check if exist output path..."if[ ! -d "${outputpath}"]# d for directorythenecho" Creating output folder..." mkdir output
elseecho" Output folder already exists."fi# if output exist...if[ -d "${outputpath}"]# d for directorythen# Check if exist a file, make a copy, if not create itecho" Check if exist file: ${filenameraw}, with extension ${extension}, full name is: ${filename}"if[ -f "${inputpath}/${filename}"]# f for filethenecho" File: ${filename} exists, so I do a copy in output path"; cp ${inputpath}/${filename}${outputpath}/${filenameraw}.${extension}.bak
elseecho" 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 whileN=5counterwhile=0while[${counterwhile} -lt ${N}]# -lt option means lesser thando touch ${outputpath}/${filenameraw}_${counterwhile}.${extension}letcounterwhile=counterwhile+1 # Forwarddone# for usage,echo" List with files in ${outputpath}: "counter=0for file in "${outputpath}"/* # * = All items, in output pathdoecho" Filename ${counter}: ${file##*/} - full: ${file}"let"counter += 1"# Forwarddone# ls worksecho" ls show a list of all of them too: " ls ${outputpath}elseecho" Output path not exist! :-( this cannot be happening"fi;echo"------- My first script end now!"echo"---------------------------------"
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.