1
\$\begingroup\$

In this post, I present the bash script for installing the ds4mac:

#!/usr/bin/env bash
script_magic="alias ds='source ~/.ds/ds_script'"
echo "Installing ds..."
grep "$script_magic" ~/.bashrc 
if [ $? != 0 ]; then
 echo "$script_magic" >> ~/.bashrc 
 echo "~/.bashrc updated!"
else
 echo "~/.bashrc is already updated."
fi
# Create files:
echo "Creating files..."
mkdir -p ~/.ds
echo "Created the .ds directory."
make > /dev/null
cp ds_engine ~/.ds/ds_engine
echo "Built the ds_engine."
tag_file=~/.ds/tags
touch $tag_file
add_tag_to_file () {
 grep 1ドル $tag_file > /dev/null
 if [ $? != 0 ]; then
 echo 1ドル 2ドル >> $tag_file
 echo Setting the tag 1ドル to directory 2ドル done.
 fi
}
# Populate the default 
echo "Populating the tag file with default tags..."
add_tag_to_file "docs" "~/Documents"
add_tag_to_file "down" "~/Downloads"
add_tag_to_file "root" "/"
add_tag_to_file "home" "~"
add_tag_to_file "ds" "~/.ds"
echo "Done populating the tag file with default tags."
echo "Copying the script..."
cp ds_script ~/.ds/ds_script
echo "Done! ds will be available for use in your next shell session. :-]"

(The entire project is here.)

See also

  1. The main script
  2. The tag engine

Critique request

Please, tell me anything that comes to mind. ^^

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 1, 2021 at 3:37
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The installer script should not ignore errors. As an example, in

make > /dev/null
cp ds_engine ~/.ds/ds_engine

the make command can fail (if no compiler is installed, if there are syntax errors in the program, etc.). In such a case it makes no sense to copy the binary to the installation directory, or to execute any of the subsequent commands. That will only produce more error messages which hide the actual problem.

A simple solution is to set the "-e" flag in the script:

#!/usr/bin/env bash
set -e

From "man bash" (this applies to other shells as well):

-e Exit immediately if a simple command ... exits with a non-zero status
answered Sep 3, 2021 at 7:19
\$\endgroup\$
2
\$\begingroup\$

Patterns like:

grep 1ドル $tag_file > /dev/null
if [ $? != 0 ]; then 
 ...
fi

Can be rewritten as:

if grep -q 1ドル $tag_file; then
 ...
fi
answered Sep 3, 2021 at 2:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.