Last active
August 29, 2015 14:06
-
-
Save dnordstrom/457eb1926bdf00a5e354 to your computer and use it in GitHub Desktop.
Go · · · Combining `cd` and `ls` commands into one
This file contains hidden or 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
## | |
# Simple utility to change directory and list files in one step. If no | |
# arguments are provided, it just lists the files in the current | |
# directory. This allows the script to be used as both `cd` and `ls`. | |
# | |
# Go to directory and list files: | |
# `go /home/philosoraptor/thoughts -Ahl` | |
# | |
# List files in current directory: | |
# `go -Ahl` | |
# | |
# @author L. Daniel Nordstrom <[email protected]> | |
# @version 0.1.0 | |
# @license MPL 2.0 | |
# | |
# This Source Code Form is subject to the terms of the Mozilla Public | |
# License, v. 2.0. If a copy of the MPL was not distributed with this | |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
## | |
# Prints a header with a dashed line (or any other character). | |
# | |
# Parameters | |
# $1 - Header text | |
# $2 - Character used to generate line (default: "-") | |
printHeader() { | |
if [ -n "$1" ]; then | |
local line="" | |
local text="$1" | |
local character="$2" | |
[ -z $character ] && character="-" | |
for (( i=0; i<${#text}; ++i )); do line="$line$character"; done | |
printf "%s\n%s" "$text" "$line" | |
fi | |
} | |
# Prints a banner with dashed lines (or any other character). | |
# | |
# Parameters | |
# $1 - Banner text | |
# $2 - Character used to generate lines (default: "-") | |
printBanner() { | |
if [ -n "$1" ]; then | |
local line="" | |
local text="$1" | |
local character="$2" | |
[ -z $character ] && character="-" | |
text="$character $text $character" | |
for (( i=0; i<${#text}; ++i )); do line="$line$character"; done | |
printf "%s\n%s\n%s" "$line" "$text" "$line" | |
fi | |
} | |
ls="ls" | |
directory="" | |
# Get any optional directory and `ls` flags. | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
# Argument is a flag. | |
-*) ls="$ls $1" ;; | |
# Argument is (hopefully) a directory. | |
*) directory="$1" ;; | |
esac | |
shift | |
done | |
# Run commands. | |
if [ -n "$directory" ]; then | |
# Directory provided: change to it and list files, unless directory | |
# is invalid (change "&&" to ";" to list files even if `cd` fails). | |
# Quotation marks are needed for `cd` to allow spaces in paths. | |
eval "cd \"$directory\"" && \ | |
printf "\n" && \ | |
printHeader "$PWD" && \ | |
printf "\n" && \ | |
eval "$ls" && \ | |
printf "\n" | |
else | |
# Directory not provided: list files with any optional flags. | |
printf "\n" && \ | |
printHeader "$PWD" && \ | |
printf "\n" && \ | |
eval "$ls" && \ | |
printf "\n" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment