Building Program-Specific Shortcuts in UNIX [closed] - macos

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a program, called carmel, which I can run from the command line via:
carmel -h
or whichever suffix I chose. When loading a file, I can say:
carmel fsa1.fst where fsa1.fst is located in my heme folder, /Users/adam/.
I would prefer to have the default file location be, e.g., /Users/adam/carmel/files, and would prefer to not type that in every time. Is there a way to let UNIX know, when I type carmel to then look in that location?

There is no standard Unix shortcut for this behaviour. Some applications will check an environment variable to see where their files are. but looking at carmel/src/carmel.cc on GitHub, I'd say you'd have to write a wrapper script. Like this:
#!/usr/bin/env bash
# Save as ${HOME}/bin/carmel and ensure ${HOME}/bin is before
# ${carmel_bin_dir} in your ${PATH}. Also ensure this script
# has the executable bit set.
carmel_bin_dir=/usr/local/bin # TODO change this?
working_directory=${CARMEL_HOME-${HOME}/carmel/files}
if [[ ! -d "${working_directory}" ]]; then
echo "${working_directory} does not exist. Creating."
mkdir -p "${working_directory}" || echo "Failed to create ${working_directory}"
fi
pushd "${working_directory}"
echo "Launching ${carmel_bin_dir}/carmel ${#} from $(pwd)..."
${carmel_bin_dir}/carmel ${#}
popd
Alternatively, since the source is freely available, you could add some code to read ${CARMEL_HOME} (or similar) and submit this as a pull request.
Good luck!

Related

Getting started on a MAC OSX script/program that needs to get a result from a URL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I come from a Windows programming background and I need to replicate a simple Windows console application in the MAC OSX world. It's actually called from a daily script, but it needs to do the following:
1) Check a network file share for a specific file.
2) If that file exists, make a call to a web site URL with one query string parameter. That web site URL simply returns a YES or NO as a string back (no HTML markup tags are returned).
3) If Yes is returned, delete the file.
This is it, I am just starting to look and I see references to cocoa, x-code, swift. Which way to go and what would support specifically the getting a result back from a URL?
I need to be pointed in the right direction and anything else you wish to add to get me going would be appreciated. Thanks! I appreciate any help that will be forthcoming! Venturing into a whole new world.
Try saving the following on your Desktop with filename MyScript:
#!/bin/bash
# Store the name of the file in a variable
file="/Volumes/SomeServer/SomeFile"
# Store website URL
website="http://SomeServer.com"
# Check if file exists
if [ -f "$file" ]; then
echo File $file exists
curl -L --silent "$website" | grep -q "YES"
if [ $? -eq 0 ]; then
echo Would delete file $file
# rm "$file"
fi
else
echo File $file does not exist
fi
You will need to change the lines that start:
file="..."
website="..."
Then start Terminal using ⌘ + space and start typing "Terminal" till it guesses you want the Terminal application. Then press Enter. That was called a "Spotlight search" and it is the way to find stuff on a Mac.
Now make the above script executable with the following, which you only need to do one time:
chmod +x $HOME/Desktop/MyScript
Now you can run it by typing:
$HOME/Desktop/MyScript
or double-clicking on the MyScript file on your Desktop.
At the moment, the script does NOT actually remove the file - I don't like removing other people's files. If it works how you expect, you can remove the # sign from the line like this and then it will actually remove the file:
# rm "$file"

What is the rationale for Bash ignoring errors when executing a script? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Bash -- and probably other shells too -- ignore any errors by default and just continue with the next command. I wonder why the shell was designed that way. After all, normally, one would want to abort a script in which every command needs the state generated by the previous.
I don't know the exact reasons, something like:
Every check takes extra time. For better performance no additional check everytime and no popup "Are you sure [Yes] [No] [Ignore]".
You are afraid for code like
cd /
ls
cd $HOME/temp;
rm -rf *
Terrible when you do not have a temp dir (script made by a normal user and executed by root)!
Anybody who has root access must be aware of the responsibility and dangers (s)he has. That is why you shouldn't execute scripts you don't trust (don't have the current dir in your PATH). And the person who wrote that script is wrong as well. Without checks on $? the script should be changed into something like
cd / && ls
cd "${HOME}"/temp && rm -rf *
or
cd / && ls
if [ ${#HOME} -gt 1 ]; then
rm -rf /"${HOME}"/temp/*
fi
Are these examples not a proof that exit-on-error would be better? I do not thinlk so. When Unix would fail exit on errors and you don't check everything, things can go terrible wrong with
cd /
ls
rm -rf /$HOME/temp/*
When HOME is set to / or a string with a space (ending with a space..) the last command might work. Always triple check your scripts, you are working with power.

Folder shortcut in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have a git repo in a folder that is inside multiple other folders. When I want to use the command line for git, I have to do cd /Desktop/.../.../.../.../repo_folder to do git commands. Is their any way that I can set a shortcut to a folder or get to a folder faster because having to type in a 70 character long path is not ideal
Thanks in advanced!
You can use the cdable_vars option of bash that allows you to call cd with a variable name. If the argument passed to cd is not a directory, then it is assumed to be a variable name and the value of the variable is used as the destination directory.
Example of use: if you put this in your ~/.bashrc:
alias show='cat ~/.dirs'
save () {
here=`pwd`
if (( $# == 0 )); then
name=`basename $here`
elif (( $# > 1 )); then
echo "usage: save [<name>]"
return -1
else
name=$1
fi
sed -i -e "/^$name=/d" ~/.dirs
echo "$name=\"$here\"" >> ~/.dirs
source ~/.dirs
}
source ~/.dirs
shopt -s cdable_vars
Then, when your current directory is one that you want to remember, just type:
save my_dir
and the next time you want to go there, just type:
cd my_dir
As long there is no my_dir directory where you type it, it will bring you where you want. The save argument is optional. If you do not provide it the defined short hand will be the base name of the current directory:
cd /Desktop/../../../../repo_folder
save
will define repo_folder as the short hand for this directory.
The ~/.dirs file contains your variable definitions for your favourite directories. You can edit it by hand, if you wish. These definitions are evaluated every time you launch a new bash shell. Beware they may overwrite others that you also need. If it is a problem, I advice you to chose unique short hands (my_dir_repo_folder instead of repo_folder). And remember the second pitfall, when you type:
cd foo
you can go either to the local sub-directory foo if there is one or to the directory for which you defined the foo short hand. And there is a third one: if you redefine a short hand, the previous one is overwritten. So, this trick is handy but somehow dangerous because when you cd you do not know any more if you are really where you want. Customizing your prompt to show the current path may be a good idea.
The show alias is just a way to list all currently defined short hands.
Just set "start in" param in your bash shortcut properties and every time you run bash it will open your repo folder :)

Bash - wait for a process (here gcc) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
On my Ubuntu machine I want to create a custom command for compiling a c file.
At the moment I have something liks this which does not work like I want to:
#compile the file
gcc $1 -o ~/.compile-c-output
#run the program
./~/.compile-c-output
#delete the output file
rm ~/.compile-c-output
The problem is that the run command is executed before gcc is ready and so the file does not exist. How can I wait until gcc is ready and I can run the file normaly?
Btw how can I add a random number to the output file so this script also works if I run it on two different terminals?
./~/.compile-c-output
Get rid of the leading ./. That's why the file doesn't exist.
~/.compile-c-output
To get a random file name, use mktemp. mktemp guarantees not to overwrite existing files.
file=$(mktemp) # unspecified file name in /tmp
gcc "$1" "$file" && "$file"
rm "$file"

Shell: How to create the directory with slash and parentheses? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'd like to create the directory "Dir (A/B)" in "test" folder in one go with the following command:
$ mkdir -vp "test/dir (A/B)"
test
test/dir (A
test/dir (A/B)
Unfortunately it's creating 'dir (A' in 'test'.
I've tried to escape it, but without success e.g. mkdir -vp "test/dir (A\/B)".
When creating manually in Finder, it works.
How should I escape the arguments? Thanks.
I'm using bash shell.
Do:
$ mkdir -vp "test/dir (A:B)"
The directory will appear as dir (A/B) in Finder and file open dialogs, but dir (A:B) in shell and other Unix applications.
Note that this is very Mac-specific, it won't work on other flavors of Unix.
Although i would not recommend this, you can create a filename like this:
mkdir 'test:dir (A:B)'
# when creating missing folders
mkdir -pv 'test/dir (A:B)'
In the finder it will show as: "test/dir (A/B)"
but if you look in the bash shell (ls -al), you will see "test:dir (A:B)"

Resources