From: Ian Kelling Date: Fri, 3 Jul 2020 21:45:07 +0000 (-0400) Subject: minor fixes and indenting X-Git-Url: https://iankelling.org/git/?a=commitdiff_plain;h=b3350aac5ce785f146bd12de3366bda3e25d3961;p=small-misc-bash minor fixes and indenting --- diff --git a/ex b/ex index cf04ee6..7859db3 100755 --- a/ex +++ b/ex @@ -14,7 +14,7 @@ # limitations under the License. ex() { - local help="Usage: ex [--help] FILE... + local help="Usage: ex [--help|-q] FILE... Extract many types of files Based on their extensions, @@ -22,56 +22,130 @@ Based on their extensions, See source for exact file extensions. Note: dtrx (package & command) extracts most of these plus some others." - if [[ $1 == --help ]]; then - echo "$help" - fi - local x super - for x in "$@"; do - case "$x" in - # tars first, so they are higher pri than non-tar extensions - *.tar ) tar xf "$x" ;; - *.tar.bz2 | *.tbz2 ) tar xjf "$x" ;; - *.tar.gz | *.tgz ) tar xzf "$x" ;; - *.tar.xz ) tar Jxf "$x" ;; - *.xz) xz -d "$x" ;; - *.7z ) 7za x "$x" ;; - *.bz2 ) bunzip2 "$x" ;; - *.deb ) - ar x "$x" - tmp=(data.tar.*) - case $tmp in - *.xz) tar xJf data.tar.xz ;; - *.gz) tar xzf data.tar.gz ;; - *) echo "error: could not find data.tar.*" >&2; exit 1 ;; - esac - tar xzf control.tar.gz - rm -f control.tar.gz - rm -f data.tar.* + local quiet=false + case $1 in + --help) + echo "$help" + return 0 + ;; + -q) + quiet=true + shift + ;; + esac + local x super restore_nullglob f + for x in "$@"; do + case "$x" in + # tars first, so they are higher pri than non-tar extensions + *.tar ) + $quiet || echo tar xf "$x" + tar xf "$x" + ;; + *.tar.bz2 | *.tbz2 ) + $quiet || echo tar xjf "$x" + tar xjf "$x" + ;; + *.tar.gz | *.tgz ) + $quiet || echo tar xzf "$x" + tar xzf "$x" + ;; + *.tar.xz ) + $quiet || echo tar Jxf "$x" + tar Jxf "$x" + ;; + *.xz) + $quiet || echo xz -d "$x" + xz -d "$x" + ;; + *.7z ) + $quiet || echo 7za x "$x" + 7za x "$x" + ;; + *.bz2 ) + $quiet || echo bunzip2 "$x" + bunzip2 "$x" + ;; + *.deb ) + $quiet || ar x "$x" + ar x "$x" + restore_nullglob=false + if ! shopt -q nullglob; then + restore_nullglob=true + fi + tmp=(data.tar.* control.tar.*) + if $restore_nullglob; then + shopt -u nullglob + fi + if (( ${#tmp[@]} != 2 )); then + echo "error: could not find data.tar.* or control.tar.*" >&2 + return 1 + fi + for f in ${tmp[@]}; do + case $f in + *.xz) + $quiet || echo tar xJf $f + tar xJf $f ;; - *.dsc) dpkg-source -x "$x" ;; - *.gz ) gunzip "$x" ;; - *.iso ) - local super - if [[ $EUID != 0 ]]; then - super=sudo - fi - local temp_dir=$(mktemp -d) - $super losetup -f - $super mount -o loop "$x" "$temp_dir" - local dir="${x%%.iso}" - mkdir "$dir" - cp -a "$temp_dir"/* "$dir" - $super umount "$temp_dir" - ;; - *.jar) jar xf "$x" ;; - *.r[0-9][0-9]|*.rar ) unrar x "$x" ;; - *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories ;; - *.sfs) $super unsquashfs "$x" ;; - *.Z ) uncompress "$x" ;; - *.zip|*.xpi ) unzip "$x" ;; + *.gz) + $quiet || echo tar xzf $f + tar xzf $f + ;; + esac + rm -f $f + done + ;; + *.dsc) + $quiet || echo dpkg-source -x "$x" + dpkg-source -x "$x" + ;; + *.gz ) + $quiet || echo gunzip "$x" + gunzip "$x" + ;; + *.iso ) + local super + if [[ $EUID != 0 ]]; then + super=sudo + fi + local temp_dir + temp_dir=$(mktemp -d) + $super losetup -f + $quiet || echo $super mount -o loop "$x" "$temp_dir" + $super mount -o loop "$x" "$temp_dir" + local dir="${x%%.iso}" + mkdir "$dir" + $quiet || cp -a "$temp_dir"/* "$dir" + cp -a "$temp_dir"/* "$dir" + $quiet || $super umount "$temp_dir" + $super umount "$temp_dir" + ;; + *.jar) + $quiet || echo jar xf "$x" + jar xf "$x" + ;; + *.r[0-9][0-9]|*.rar ) + $quiet || echo unrar x "$x" + unrar x "$x" + ;; + *.rpm ) + $quiet || echo "rpm2cpio $x | cpio --extract --make-directories" + rpm2cpio "$x" | cpio --extract --make-directories + ;; + *.sfs) + $quiet || echo $super unsquashfs "$x" + $super unsquashfs "$x" + ;; + *.Z ) + $quiet || echo uncompress "$x" + uncompress "$x" + ;; + *.zip|*.xpi ) + $quiet || echo unzip "$x" + unzip "$x" + ;; - *) echo "I don't know how to extract $x";; - esac - done + *) echo "I don't know how to extract $x";; + esac + done } ex "$@" diff --git a/ll-function b/ll-function index 6deb916..c48ecea 100644 --- a/ll-function +++ b/ll-function @@ -15,9 +15,9 @@ ll() { - case $1 in - -h|--help) - cat <<'EOF' + case $1 in + -h|--help) + cat <<'EOF' usage: ll [ARGS_TO_LS] ls -lA with enhanced output @@ -30,88 +30,93 @@ ls -lA with enhanced output * remove first line size summary EOF - ;; - esac - local x y perm line binls sizePadding middle tail size \ - max_hl_digits hardlinks initial_space hardlink_spacing - local max_hl_digits=0 - local -a lines hl - binls=$(type -P ls) - local aclchar=false - # there's no way to tell if ls uses the acl specifier unless we loop over the data twice - # the 11th char is either - # . for selinux context - # + for any other kind of acl - # or blank for no other kind of acl - # I don't want to see this generally. - while read -r line; do - # if we did want the first line, it would need to be stripped of non-printing chars: - #line=${line#$'\E[00m'} - # lines like "total 123M", we don't want - if [[ ! $line =~ ^total\ [0-9][^\ ]*$ ]]; then - lines+=("$line") - if ! [[ $line == [-dscbl][-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]* ]]; then - hardlinks= - else - [[ ! ${line:10:1} == " " ]] && aclchar=true + ;; + esac + local x y perm line binls sizePadding middle tail size \ + max_hl_digits hardlinks initial_space hardlink_spacing + local max_hl_digits=0 + local -a lines hl + binls=$(type -P ls) + binlslink=$(readlink -f $binls) + if [[ $binlslink == */busybox ]]; then + $binls -lA "$@" + return + fi + local aclchar=false + # there's no way to tell if ls uses the acl specifier unless we loop over the data twice + # the 11th char is either + # . for selinux context + # + for any other kind of acl + # or blank for no other kind of acl + # I don't want to see this generally. + while read -r line; do + # if we did want the first line, it would need to be stripped of non-printing chars: + #line=${line#$'\E[00m'} + # lines like "total 123M", we don't want + if [[ ! $line =~ ^total\ [0-9][^\ ]*$ ]]; then + lines+=("$line") + if ! [[ $line == [-dscbl][-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]* ]]; then + hardlinks= + else + [[ ! ${line:10:1} == " " ]] && aclchar=true - # we also need to parse the hardlinks on the first pass, because for - # example ls could see the highest count as 11, and thus use 3 - # places for hardlinks, " 10", but then we use 9 or 8 for a more - # useful count, and would then use 2 places. So we have to look - # through them all because we can't rely on the spacing that ls - # decided on. - y="${line:11}" - initial_space="${y%%[![:space:]]*}" - hardlinks="${y#$initial_space}" # remove any initial spaces - hardlinks="${hardlinks%%[[:space:]]*}" # remove everything beyond first word - # ignore the hardlinks that files/dirs always have - hardlinks=$(( hardlinks - 1 )) - [[ $hardlinks == 0 ]] && hardlinks= - if (( ${#hardlinks} > max_hl_digits )); then - max_hl_digits=${#hardlinks} - fi - fi - hl+=("$hardlinks") + # we also need to parse the hardlinks on the first pass, because for + # example ls could see the highest count as 11, and thus use 3 + # places for hardlinks, " 10", but then we use 9 or 8 for a more + # useful count, and would then use 2 places. So we have to look + # through them all because we can't rely on the spacing that ls + # decided on. + y="${line:11}" + initial_space="${y%%[![:space:]]*}" + hardlinks="${y#$initial_space}" # remove any initial spaces + hardlinks="${hardlinks%%[[:space:]]*}" # remove everything beyond first word + # ignore the hardlinks that files/dirs always have + hardlinks=$(( hardlinks - 1 )) + [[ $hardlinks == 0 ]] && hardlinks= + if (( ${#hardlinks} > max_hl_digits )); then + max_hl_digits=${#hardlinks} fi - done< <( "$binls" -lAh --color=always "--time-style=+%m-%d %Y + fi + hl+=("$hardlinks") + fi + done< <( "$binls" -lAh --color=always "--time-style=+%m-%d %Y %m-%d %I:%M %P" "$@" ) - hardlink_spacing=$((max_hl_digits + 1)) + hardlink_spacing=$((max_hl_digits + 1)) - for index in "${!lines[@]}"; do - line=${lines[index]} - hardlinks=${hl[index]} - if [[ $line != [-a-zA-Z][-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]* ]]; then - # line we don't understand - printf "%s\n" "$line" - continue - fi - perm=0 - for (( x=0; x<=8; x++ )); do - y=${line:$(( -1*x + 9 )):1} - [[ $y == [tT] ]] && perm=$(( perm + 512 )) - if [[ $y == [sS] ]]; then - [[ $x == 3 ]] && perm=$(( perm + 1024 )) - [[ $x == 6 ]] && perm=$(( perm + 2048 )) - fi - [[ $y != [-ST] ]] && perm=$(( perm + 2**x )) - done - if $aclchar; then - y="${line:11}" - else - y="${line:10}" - fi - middle=${y#*[^ ]* } - size=${middle#*[^ ]* *[^ ]* } - middle=${middle%"$size"} - tail=${size#*[^ ]* } - size=${size%"$tail"} - declare -i sizePadding="${#size} - 1" - size=( $size ) # remove spaces - size=${size/.?/} - - printf "%s%4o%${hardlink_spacing}s%s%${sizePadding}s%s\n" \ - "${line:0:1}" $perm "$hardlinks" " $middle" "$size" " $tail" + for index in "${!lines[@]}"; do + line=${lines[index]} + hardlinks=${hl[index]} + if [[ $line != [-a-zA-Z][-r][-w][-xsS][-r][-w][-xsS][-r][-w][-xtT]* ]]; then + # line we don't understand + printf "%s\n" "$line" + continue + fi + perm=0 + for (( x=0; x<=8; x++ )); do + y=${line:$(( -1*x + 9 )):1} + [[ $y == [tT] ]] && perm=$(( perm + 512 )) + if [[ $y == [sS] ]]; then + [[ $x == 3 ]] && perm=$(( perm + 1024 )) + [[ $x == 6 ]] && perm=$(( perm + 2048 )) + fi + [[ $y != [-ST] ]] && perm=$(( perm + 2**x )) done + if $aclchar; then + y="${line:11}" + else + y="${line:10}" + fi + middle=${y#*[^ ]* } + size=${middle#*[^ ]* *[^ ]* } + middle=${middle%"$size"} + tail=${size#*[^ ]* } + size=${size%"$tail"} + declare -i sizePadding="${#size} - 1" + size=( $size ) # remove spaces + size=${size/.?/} + + printf "%s%4o%${hardlink_spacing}s%s%${sizePadding}s%s\n" \ + "${line:0:1}" $perm "$hardlinks" " $middle" "$size" " $tail" + done } diff --git a/psg-function b/psg-function deleted file mode 100644 index bdef351..0000000 --- a/psg-function +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash -# Copyright (C) 2016 Ian Kelling - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -psg () { - local x y help - help="Usage: psg [--help] GREP_ARGS -grep ps and output in a nice format" - if [[ $1 == --help ]]; then - echo "$help" - return - fi - x=$(sudo ps -eF) - # final grep is because some commands tend to have a lot of trailing spaces - y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') - if [[ $y ]]; then - echo "$x" | head -n 1 - echo "$y" - fi -}