Basic Bash Inputs

Recently someone asked me about accepting bash inputs. So I decided to take a stab at writing a little about it up. For the initial one we’ll look at accepting text input. Here, we’ll just sandwich a read statement between two echo commands. In the first echo we’ll ask for a name of a variable. Then we’ll read it in with the read command. And in the second echo we’ll write it out. Using the variable involves using the string of the variable (myvariable in this case) with a dollar sign in front of it, as in $myvariable below: echo "Please choose a number: " read myvariable echo "You picked $myvariable" Read also has a number of flags available to it:
  • -a assigns sequential indexes of the array variable
  • -d sets a delimiter to terminate the input
  • -e accepts the line.
  • -n returns after reading a specified number of characters
  • -p prompts without a trailing newline, before attempting to read any input
  • -r doesn’t use a backslash as an escape character
  • -s runs silent, which doesn’t echo text
  • -t: causes read to time out (number of seconds is right after the -t)
  • -u reads input from a file descriptor
Next, we’ll build on that read statement (note the addition of -p) and use a while to force a user to input a y or n and then parse their selection with a basic case statement: while true; do read -p "Do you wish to continue?" yn case $yn in [Yy]* ) echo "Add your action here"; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done Finally, let’s look at positional parameters. Here, you can feed them at the tail end of the script, as words that are separated by spaces after the name of the script. Here, we simply just echo $0, which is the first position (aka – the name of the script you just ran) and $1 and $2 as the next two. #!/bin/bash echo "You Used These" echo '$0 = ' $0 echo '$1 = ' $1 echo '$2 = ' $2 You could also take $3, $4, etc. This is different than writing flags, which requires a bit more scripting. So if you called the script with: /path/to/script/ test1 You would see: You Used These $0 = ./ $1 = test1 What tips/additions do you have?

Using Inputs in Bash Scripts

You can easily accept user provided input in bash by using the read command in bash for Linux and OS X. Here, we’ll echo out a choice to a user in a script, read the output into a variable called yn and then echo out the response: echo "Please enter y or n: " read yn echo "You chose wrong: $yn" Here, we used echo to simply write out what was chosen in the input. But we could also take this a little further and leverage a case statement to then run an action based on the choice selected: read -p "Should the file extension change warning be disabled (y/n)? " yn case ${yn:0:1} in y|Y ) defaults write FXEnableExtensionChangeWarning -bool false echo "The warning has been disabled" ;; * ) defaults write FXEnableExtensionChangeWarning -bool true echo "The warning has been enabled" ;; esac The options when scripting are pretty much infinite and chances are, if you’ve written any scripts, you’ll know of a better way to do this than how I’ve always done it. One of the great things about scripting is the fact that there’s always a better way. So feel free to throw any of your examples into the comments!

Spotlightish Finder

When you search for files the Finder window with the results has what is known as the Path Bar. As you can see in the below image, the Path Bar allows you to click on any directory in the hierarchy up to the one you are on and open that directory in the Finder.
The Path Bar in Mac OS X
The Path Bar in Mac OS X
By default, the Path Bar is only shown in a search result, but if you like it then you can see it in every Finder window. To enable this feature, create a boolean key in the with a value of yes. To do so, you can use the defaults command:
defaults write ShowPathBar -bool YES
To go back to having the Path Bar disabled:
defaults write ShowPathBar -bool NO

Speed Up ScreenSharing

Screen Sharing over a WAN can be a bit slow. But you can send less data and receive a less latent connection. To do so, you are going to augment the controlObserveQuality key of the property list. If you set the key to a 1, which I see commonly suggested then it will be black and white, which in todays world is another way of saying practically illegible. Instead try the number 2, which sets it to grey scale, which is pretty good (that’s what I use). To do so run:
defaults write controlObserveQuality 2
You can also set the controlObserveQuality key to 3, 4 or 5 which is 8-bit color, 16-bit color and full color respectively. Since the default connection is full color, resetting the controlObserveQuality to default would mean running:
defaults write controlObserveQuality 5