Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash make script

use below script to easilly create scripts with different types that are automaticaly set to executable

#!/bin/bash

version="1.0"

function help-text (){
	echo "-v : Show version"
	echo "-t : Type of script you want to create (bash/python3/...) defaults to bash"
	echo "-h : Show help text"
	exit 0;
}

args=("$@") 
ELEMENTS=${#args[@]}

if [[ $1 = "-h" ]]; then
	help-tekst
fi

for (( i = 0; i < $ELEMENTS; i++ )); do
	case ${args[${i}]} in
		"-"* )
			argument=${args[${i}]}
			for (( j=1; j<${#argument}; j++ )); do
			  	case "${argument:$j:1}" in
			  		"v"  )
						echo "Version: $version"
						;;
					"t"  )
						fileType=${args[${i}+1]}
						i=$i+1
						;;
					"h"  )
						help-text;
						exit 0;
						;;
					*	 )
						echo "-${argument:$j:1}" "invalid command, use -h for more info"
						;;
				esac
			done
			;;
		*	 )
			fileName=${args[${i}]};
			;;
	esac
done

if [[ -z "$fileName" ]]; then
	echo "No fileName was provided"
else
	if [[ -z "$fileType" ]]; then
		printf "#!/bin/bash
" > $fileName;
		chmod +x $fileName;
	else
		printf "#!/bin/$fileType
" > $fileName;
		chmod +x $fileName;
	fi
fi
Comment

sample bash script

#!/bin/bash
# Editor Amir Ammar 

function SOS {
	echo "Enter: number / files / names / stats <chapter-name> / search <word>/ quit"
}

#########################################A
FILE=$PWD'/'$1 						    #M
if test -f "$FILE" ; then 				#I
	FILE=''		  						#R
else									#A
	echo "File $1 not found"            #M
	exit                                #M
fi                                      #A
for x in  $(cat $(echo $PWD'/'$1)) ; do #R
	FILE=$PWD'/'$x                      #COPY    The Earth With Out Art is Hust 'EH'
	if test -f "$FILE"; then            #RIGHT
    	continue						#E
    else				                #D
    	echo File $x not found			#I
    	exit                            #T
fi                                      #O
done                                    #R
##########################################
while SOS && read -s input ; do
	sweet=$(echo $input | egrep "(number)|(files)|(names)|(stats)[s]*(chapter-[0-9]*[0-9]*)*|(search)[s]*[A-Za-z]*|(quit)")
# Options
case $sweet in 
	"number") ## Find the number of chapters in a given directory  ###############################
	 echo $(cat $(echo $PWD'/'$1) | wc -l)  "chapters"  
	;;
	"files") ## find the name of the files #######################################################
	wd=$PWD
	for chapter in $(cat $(echo $PWD'/'$1)) ; do 
		 echo $( cat $wd/$chapter | head -n1 ): $chapter >> temp1.txt
	done
	sort -V temp1.txt > temp2.txt
	cat temp2.txt
	rm temp2.txt
	rm temp1.txt
	;;
	"names") ## find the title of each chapter ###################################################

	wd=$PWD
	for chapter in $(cat $(echo $PWD'/'$1)) ; do 
		 h="$( cat $wd/$chapter | head -n1 ): "
		 name="$( cat $wd/$chapter | head -n3 | tail -n1)"
		 full="$h$name"
		 echo $full >> temp1.txt
	done
	sort -V temp1.txt > temp2.txt
	sed 's/$/ /g' temp2.txt
	rm temp2.txt
	rm temp1.txt
	;;

	stats*) ## find the number of the worlds and the lines within a chapter #######################
	wd=$PWD
	for chapter in $(cat $(echo $PWD'/'$1)) ; do 
		 ch="$( cat $wd/$chapter | head -n1 ): "
		 line_words="$( cat $wd/$chapter |  tail -n +3 |wc -l) lines, $( cat $wd/$chapter| tail -n +3 | wc -w) words" 
		 full=$ch$line_words
		 echo "$full" >> temp1.txt
	done
	sort -V temp1.txt > temp2.txt
	if [[ $sweet = "stats" ]] ; then
		cat temp2.txt
	else
		if [[ $sweet =~ (stats)[s]*(chapter-[0-9]*[0-9]*)* ]] ; then 
			chap=($sweet)
			grep -i "${chap[1]}" temp2.txt
		fi
	fi
 	rm temp2.txt
	rm temp1.txt
	;;
	
	search*) ## search for a specific word within a chapter and print that amount #################
		arr=($sweet)
		wd=$PWD 
		touch unsorted.txt
		touch sorted.txt
		for chapter in $(cat $(echo $PWD'/'$1)); do 
			h="$(head -n1 $wd/$chapter )"
	 		get=$(cat $wd/$chapter | egrep -w -i ${arr[1]} | wc -l)
	 		full="$h: $get"
	 		if [[ $get -gt 0 ]] ; then
				echo $full >> unsorted.txt
	 		fi
		done
		if [[ $(cat unsorted.txt | wc -l) -gt 0 ]] ; then 
			sort -V unsorted.txt > sorted.txt
			cat sorted.txt
		fi
		rm unsorted.txt
		rm sorted.txt
		;;
	"quit") ## exit ################################################################################
	exit
	;;
esac 
done 
Comment

example bash script

ipconfig /all
ping google.com
tracert google.com
PAUSE
Comment

bash scripting tutorial

#!/bin/bash
 
echo -e "Hi, please type the word: c "
read  word
echo "The word you entered is: $word"
echo -e "Can you please enter two words? "
read word1 word2
echo "Here is your input: "$word1" "$word2""
echo -e "How do you feel about bash scripting? "
# read command now stores a reply into the default build-in variable $REPLY
read
echo "You said $REPLY, I'm glad to hear that! "
echo -e "What are your favorite colours ? "
# -a makes read command to read into an array
read -a colours
echo "My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]}:-)"
Comment

PREVIOUS NEXT
Code Example
Shell :: get files with name linux 
Shell :: centos 8 gui 
Shell :: winget github 
Shell :: how to push repository to github 
Shell :: toasty android github 
Shell :: Bootable flash from ubuntu terminal 
Shell :: make kernel module 
Shell :: github create repository command line 
Shell :: sms_autofill flutter 
Shell :: mac ssh download folder 
Shell :: dracula theme gnome terminal 
Shell :: linux nano editor 
Shell :: how to change your bash setup 
Shell :: connect to aws ubuntu gui from windows 
Shell :: how to edit a file in terminal windows 
Shell :: spring boot docker hub image 
Shell :: take a screenshot linux terminal 
Shell :: react native youtube install 
Shell :: deleting a remote branch 
Shell :: flutter doctor --android-licenses error 
Shell :: std error bash 
Shell :: npm -g yarn 
Shell :: linux find file type under a specific folder 
Shell :: change or set password interactively in linux 
Shell :: touch file bash insert text 
Shell :: nautilus open random file 
Shell :: linux lastpass export data 
Shell :: how to install mx package manager 
Shell :: docker build non-zero code 100 
Shell :: December festivities 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =