Question : bash make script
Answered by : jos-wigchert
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\n" > $fileName; chmod +x $fileName; else printf "#!/bin/$fileType\n" > $fileName; chmod +x $fileName; fi
fi
Source : | Last Update : Wed, 18 May 22
Question : sample bash script
Answered by : amir-ammar
#!/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
Source : | Last Update : Tue, 28 Jun 22
Question : example bash script
Answered by : salman-ahmad
ipconfig /all
ping google.com
tracert google.com
PAUSE
Source : https://www.howtogeek.com/263177/how-to-write-a-batch-script-on-windows/ | Last Update : Sat, 18 Jun 22