Coder Social home page Coder Social logo

bashify's Introduction

bashify

few helper functions in bash

#repeatString repeats a string

repeatString string ?depth

#depth >> how many times to repeat the string

repeatString "bash" 5 # bashbashbashbashbash

#charAt

get the position of a character in a string

charAt string positionToSearch

charAt "bash" 2 # a

#includes

check if a word is in a string

includes string stringToSearch ?depth

#depth  where to start search from

includes "bash" sh 2

#returns 1 for false or 0 for true

#endsWith check if a word is at the end of a string

endsWith string endToCheck ?depth

# depth >> where to start the search from

endsWith "bash" a 2

#returns 1 for false or 0 for true

#isInteger check if a value is an integer

isInteger number

#return 1 for non integers or 0 for integers

#int get all the integers before the decimal point non integers values will throw an error

int number
int 25.8 ; # 25

#destructure

set the content of an array into different variables

gotchas

do not quote the array argument ( first agument )

it is important you quote the second argument to this function

associative arrays work in alphabetical order

use "," to separate the variables to assign each array element to

destructure array values

array=( bash ksh zsh )

destructure ${array[@]} "shell1,,shell2"

echo $shell1  # bash

echo $shell2  # zsh

destructure ${array[@]} "shell1,shell2,shell3"

echo $shell1 # bash

echo $shell2 # ksh

echo $shell3 # zsh

#... Spread a bunch of string inside an array

... string

str=bash

array=( $(... $str) )

echo ${str[@]} # b a s h

#foreach foreach element of an array execute a function

gotchas:

don't quote the array arugment ( i.e the first argument )

If you pass in a function as a callback using the function command you should wrap it in single quote

s() { echo $(( $1 * $1 )) ;}

array=( 1 2 3 4 5 6 )

foreach ${array[@]} s # 2 4 6 8 10 12

foreach ${array[@]} 'function s() { echo $(( $1 + $1 )) ;}' # always end the function with a ;} 
# 2 4 6 8 10 12

#copyWithin copy an array index into another index quote the first argument use ${array[*]} instead of ${array[@]}

copyWithin arrayArgument indexToCopyfrom indexToCopyTo

array=( "bash" "ksh" "zsh" "csh" )

copyWithin "${array[*]}" 1 3 # bash ksh zsh ksh

bashify's People

Contributors

hexathos avatar zombieleet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.