Create Mac terminal script with variable - macos

I'd like to create a script that executes several lines of code, but also ask the user a question to use as a variable.
For example, this is what I execute in terminal:
git add -A && git commit -m "Release 0.0.1."
git tag '0.0.1'
git push --tags
pod trunk push NAME.podspec
I'd like to make 0.0.1 and NAME as variables that I ask the user with questions to start the script off:
What is the name of this pod?
What version?
Then I'd like to merge these variables into the script above. I'm confused about what "dialect" to use (sh, bash, csh, JavaScript?, etc) and that extension I should save it as so I just have to double-click it.
How can I do this?

This should do:
#!/bin/bash
read -e -p "What is the name of this pod?" name
read -e -p "What version?" ver
git add -A && git commit -m "Release $ver."
git tag "$ver"
git push --tags
pod trunk push "$name".podspec
Give this script a suitable name (script or script.sh etc..), then assign proper permission:
chmod +x path/to/the/script
then run it from terminal:
path/to/the/script
You can make your script take the name and version as arguments too. A method to do that combining the above is:
#!/bin/bash
name="$1";ver="$2"
[[ $name == "" ]] && read -e -p "What is the name of this pod?" name
[[ $ver == "" ]] && read -e -p "What version?" ver
...
This has the advantage of taking arguments while working like the first one. You can now call the script with arguments:
path/to/the/script podname ver
and it won't ask for name and ver, instead it will take podname as name and ver as version from the arguments passed.
If the second argument is not passed, it will ask for ver.
If none of the arguments is passed, it will ask for both of them, just like the first code sample.

Related

pipe the package version number and create a git tag from the version number

I am trying to create a git tag from the version number of a package.json file
PACKAGE_VERSION=$(node -p -e "require('./package.json').version") | git tag -a ${PACKAGE_VERSION} -m “Version ${PACKAGE_VERSION}”
git push --tags
I am getting the version from the package.json file but the right hand side of the pipe is wrong
The pipe will start a sub-shell, that is, there, the PACKAGE_VERSION is not defined.
If you want to build the script as a 1-liner, you may replace the | with &&
See these examples:
kent$ x='foo'|echo $x
<--- here the echo cmd prints nothing
kent$ x='foo' && echo $x
foo
Why are you using a pipe? Just set your variable, and then use it in a separate statement. (I haven't checked your command for setting the variable, but you say that it is working.)
PACKAGE_VERSION=..........
git tag -a ${PACKAGE_VERSION} -m "Version ${PACKAGE_VERSION}"

How does eval stop the pathspec errors in this script?

I want to automate the many version control steps of Git. I was successful until I used git commit -S -m ${var} in my Bash script. This line gives me (pathspec errors x # of word) - 1... unless I use eval. How does eval make my script work?
I thought this article had the answer, but my issue involves a string, not an array.
Gif video of the broken vs. working Bash script
Broken code
brokenCommitCode () {
# Give it a multi-word, space-separated message
read -p 'Commit message (use quotes): ' commitMsg
commitMsg="'${commitMsg}'"
echo ${commitMsg}
git add -A &&
git commit -S -m ${commitMsg}
}
Working code
workingCommitCode () {
read -p 'Commit message (use quotes): ' commitMsg
commitMsg="'${commitMsg}'"
echo ${commitMsg}
git add -A &&
eval git commit -S -m ${commitMsg}
}
I expected the brokenCommitCode to commit properly with the message I enter on the prompt. The actual result is a pathspec error when it reaches git commit -S -m ${commitMsg}. How does eval make this work?
I'm using GNU bash, version 4.4.19(1)-release (x86_64-pc-msys) with git version 2.16.2.windows.1 on a Windows 8.1 PC.
Correct fix is
funname() {
read -p 'Commit message (use quotes): ' commitMsg
echo "${commitMsg}"
git add -A &&
git commit -S -m "${commitMsg}"
}
Why eval seems to fix:
single quotes where added to commitMsg variable (seems intent was to prevent message argument to be split on a whitespace)
looking what happens with the following message:
commitMsg="this is a message"
git commit -S -m ${commitMsg}
git commit -S -m this is a message
[error because "is" "a" "message" are taken as different additional arguments]
however it doesn't prevent because single quote is not re-interpreted but is like any other character in variable content
following with the example
git commit -S -m ${commitMsg}
git commit -S -m \'this is a message\'
[error "is" "a" "message'" are taken as different additional arguments]
with eval the single quotes are re-interpreted but also any other character which has a particular meaning in bash (;, &, ${..}, ..)
Suppose for example the following commit message which can inject arbitrary command.
commitMsg="message'; ls -l; echo 'done"
git commit -S -m 'message'; ls -l; echo 'done'

how to run a shell script as an alias?

I wanna alias a script to my zsh. Aliasing a script in zshrc does not work, the output of the script in nothing
There are no syntax errors in my script. i have tried running
"sh ./script.sh" in the script containing folder which does fetches the desired result but alias something="sh ~/script.sh" does not work
even alias something="source ~/script.sh" does not work
the script creates a local project and a github repo
contents of the script:
#!/bin/bash
function create () {
read -p 'Repository Name: ' uservar
projects_directory = ~/Downloads/Projects/ #change this path to the directory where you want to store you files
mkdir $projects_directory/$uservar
cd $projects_directory/$uservar
git init
touch README.md
echo -e "#$uservar" >> $projects_directory/$uservar/README.md
# this is where we make a github repo from cli
repo_name=$uservar
test -z $repo_name && echo "Repo name required." 1>&2 && exit 1
curl -u 'thisisshub' https://api.github.com/user/repos -d "{\"name\":\"$repo_name\"}" #change thisisshub to your <username>
#making a git repo from cli ends
git add .
git commit -m "Initial Commit"
git push -u origin master
code .
}
expected result: successful aliasing of a script
actual result: no output

Right string expansion in Bash (for shell script)

I'm experimenting a little bit with a shell script, which should run git commands for multiple repositories on the same level. This project structure might be a bad idea, but this is another story.
Everything works fine until I've run into this problem:
DETAIL="test test" && command="commit -m '${DETAIL}'" && echo $(git ${command})
# -> error: pathspec 'test'' did not match any file(s) known to git.
I've also tried other opportunities like
DETAIL="test test" && command="commit -m ${DETAIL}" && echo $(git ${command})
DETAIL="test test" && command="commit -m $DETAIL" && echo $(git ${command})
All give the same result (see above). I've also scanned these docs about string expansion, but I don't have the problem, that the variables/strings might be null or undefined. The last echo is not the problem, you can also store the result of $(git status) in a variable and echo this one (my way in the script).
I know, there are similar questions, but I did not found a similar scenario yet, since I'm just dealing with simple and non-null strings, but with (too?) many quotes.
Interesting variant:
DETAIL="test test" && command="commit -m '${DETAIL}'" && echo $("git ${command}")
# -> git commit -m 'test test': command not found # WHAT?
Also interesting, just:
command="commit -m 'test'" && echo $(git ${command})
works fine.
Use bash arrays with proper quoting...
DETAIL="test test" && command=(commit -m "$DETAIL") && git "${command[#]}"
To your code:
echo "$(command)" is the same as command (ok, trailing empty newlines are removed)
"command blabla" does not execute file command with the first argument blabla. It will execute a filename named exactly with space command blabla.
Inside $("git ${command}") you want to execute a filename named git commit -m 'test test' (exactly, this is the whole filename name, with spaces, after ${command} is expanded). As on you system there is no file named git commit -m 'test test' bash returns command not found.

retrieve the path of a repo from the "git bash" from windows with right format

I am trying to retrieve the path of a repository from the "git bash" shell in windows, as follows
user#CND7293ZVV MINGW64 /c/Work/git/repository/subfolder (master)
$ git rev-parse --show-toplevel
C:/Work/git/repository
The problem is that I want to use the ouptut path to use it from a bash script, but it is not in bash format. I want to get /c/Work/git/repository. Is there any way to get the path in a way that can be used directly in the git bash shell?
extra information: The target is to store that path in a variable to be used inside a bash script, independently of whether I am running bash from a linux terminal, or when I am running from the git bash.
Update:
To be able to use the command inside the git-bash environment, and also inside native linux bash, we can use the following:
REPODIR=$(git rev-parse --show-toplevel)
# If we are inside mingw* environment, then we update the path to proper format
if [[ $(uname) == MINGW* ]] ; then REPODIR=$(cygpath -u "${REPODIR}"); fi; echo ${REPODIR}
When running on the Git bash command line, you have an buildin utility with the name cygpath that does this:
$ cygpath -u "C:/Work/git/repository"
/c/Work/git/repository
This can be combined with your existing command to do everything on 1 line:
$ cygpath -u "$(git rev-parse --show-toplevel)"
/c/Work/git/repository

Resources