Last active
June 30, 2022 09:07
-
-
Save nordinrahman/d3ff3220cd582926ed47732342eaebd8 to your computer and use it in GitHub Desktop.
Nord's bash script cook book
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2022 Nordin Rahman | |
# [@nordinrahman](https://twitter.com/nordinrahman) | |
# [@nordinrahman](https://www.facebook.com/nordinrahman) | |
# [@nordinrahman](https://www.linkedin.com/in/nordinrahman)) | |
# find files with pattern "*.cs" on current directory and sub directories recursively, then emit relative path to each found file, one file per line | |
# Use `find` command | |
# `.` refers to current directory | |
# `-name` is options flag to denote file name pattern | |
# `"*.cs"` wild card patterns to match files we are looking for | |
find . -name "*.cs" | |
# find which lines from given input which is NOT matching seek text | |
# Use `grep` command | |
# `-v` is options to denote negative matching, ie, to list lines not matching seek text | |
# `"text"` is text to match | |
echo "$MULTILINE_VARIABLE" | grep -v "text" | |
# Loop through all line from given multi line input | |
echo "$MULTILINE_VARIABLE" | while read LINE; do echo $LINE; done | |
# Convert given file in FILEPATH variable to be converted to UTF-8 (with BOM) to target path | |
iconv -t UTF-8 "$FILEPATH" > $TARGET_PATH | |
# Create empty temporary file and assign path to temp file to variable | |
PATHTOTEMPFILE=$(mktemp) | |
# Move file and override target file if exist | |
mv -f "$PATH_FROM" "$PATH_TO" | |
# This is full example of how to look for all file with extension `cs` and conver all the files to be encoded to UTF-8 (with BOM), except files under `bin` and `obj` directories | |
find . -name "*.cs" | grep -v "/obj/" | grep -v "/bin/" | while read CSFILE; do CSTEMP="$(mktemp)"; iconv -t UTF-8 "$CSFILE" > "$CSTEMP"; mv -f "$CSTEMP" "$CSFILE"; done | |
# Trim starting/trailing whitespaces in a variable | |
VAR_WITH_STARTING_AND_TRAILING_SPACES=" ABC " | |
echo "$VAR_WITH_STARTING_AND_TRAILING_SPACES" | sed -e s/^[[:space:]]*//g | sed -e s/[[:space:]]*$//g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment