print current directory in bash command inside cloud build yaml - google-cloud-build

So i want to print the current working directory after reading the git tag of the commit example :
/demo
/test
/prod
So when a tag contains verxxx-demo the inline script will know that it needs to switch to the specific folder.
My problem is now that i want to print the current path after the folder is switched and usually in linux you do it with pwd but in cloudbuild seems not to work or i am missing something .
if [[ $TAG_NAME == *test* ]]
then
cd org/test
echo switched "($PWD)"
elif [[ $TAG_NAME == *demo* ]]
then
cd org/demo
echo switched folder
else
echo no tag
fi
when i use this code , i get the next error in cloudbuild :
Your build failed to run: generic::invalid_argument: generic::invalid_argument: invalid value for 'build.substitutions': key in the template "PWD" is not a valid built-in substitution
Any suggestions how to tackle this one ? so i can print the current path after the change folder happens ?

I think what you want is
echo "switched `pwd`"
based on this answer.
I know the variable substitution can be tricky with cloudbuild, so please lmk if that doesn't work.

Related

How do I find a file given its basename and retrieve its filetype in a bash script?

I'm writing my first bash script as part of homework and given covid 19 we haven't been able to go over some of this stuff in class (and I'm not doing well googling).
I am writing a script that takes user input and searches the cd to find a file with the name of $userInput. If found the script will determine if the file is a "normal file" (.txt for example) or if the file is a directory. How can I gather this information?
Here is my code currently:
#! /bin/bash
$userInput
echo "Enter a file name. Empty to exit."
while [ 1 ]
do
echo "Please input a file name:"
read userInput
if [ "$userInput" == "" ]; then
exit
else
echo $userInput
fi
done
I have the looping and getting user input down, what am I missing here?
if cd $userInput*; then
cd ..
echo "It is a directory."
else
# the cd will do the rest of informing for you
fi
I also strongly suggest to turn this into a file and get user input in command line, like this:
bash ./yourcode.sh filename
Inside your code:
if cd $1*; then
cd ..
echo "It is a directory."
else
# the cd will do the rest of informing for you
fi
EDITED: EXTRA
Continue here if you really wanna understand what happened. In bash, every command/function by default returns a value that informs the kernel whether it terminated successfully or something went wrong, for sake of simplicity, we will just assume it returns true if successful and false if not, here I used this piece of info with the function cd which changes the current directory to the one specified, if the change happened successfully, it will return true which then I will capture in my if statement to execute the code, which consists of returning to the original directory (cd ..) and saying to the user it was a directory.
Otherwise, cd will return false and will tell you itself what happened, was it a file or was it nonexistent, and the * (a wildcard) exists to find whatever extension it might have or even doesn't have. I hope this helps, cheers!
There are several ways to classify an entry. For instance,
[[ -f $filename ]]
tests for regular files,
[[ -d $filename ]]
tests for directories, and there are other tests too where you can test for symlinks, or whether you have read/write/execute permissions. You find the list of available file test operators here.

Shell script working only after adding an echo statement just before body of the 'if' block

I am trying to run the below script but it is not executing the if block but when I add another statement echo "Test" just below the if statement then my if block gets executed. Can anyone suggest what is causing this? I am not able to figure out how adding an echo statement is making the if block execute.
P.S. Please assume that the source and target directories exist and the file also exists in the source directory with the same name as mentioned in the script.
Script not Working:
if [ -f $/apps/agile/product/xxxxxx.xlsx ]
then
mv /apps/agile/product/xxxxxx.xlsx /apps/agile/product/Archived
echo "Moved the last generated Report to Archived Directory"
fi;
Working Script:
if [ -f $/apps/agile/product/xxxxxx.xlsx ]
echo "Test"
then
mv /apps/agile/product/xxxxxx.xlsx /apps/agile/product/Archived
echo "Moved the last generated Report to Archived Directory"
fi;
The argument to if is a sequence of commands. The exit status of the last one of these commands is what if examines and acts on.
You are effectively changing
if false; then
to
if false; true; then
which succeeds.
The real cause of the error is probably the aberrant $/ at the beginning of the path. We can't guess what the correct path is, but this is almost certainly incorrect, unless you really have a directory named $ in the current directory (which would be hugely impractical).

List files in directory as menu options and then run that selected script

First post here, and quite a newbie to using terminal. Sorry in advance for any fails.
I'm trying to list whatever .sh files are in a directory, so when i choose the file, it runs the file. The files are just .sh files that export environment variables so i can load a specific job im working on.
The thing is, if i copy the script and paste into terminal and run, it works.
But if i try to run the script through terminal. It works, but doesnt set the environment variables. Here's the script.
cd ~/nuke/pipeline/executables/
echo "-- JOB LIST --"
# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'stop' to cancel: "
# allow the user to choose a file
select filename in *.sh
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == stop ]]; then break; fi
# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
echo "'$REPLY' is not a valid number"
continue
fi
# now we can use the selected file, trying to get it to run the shell script
. $filename
echo "Job Environment is $filename"
# it'll ask for another unless we leave the loop
break
done
#And here's whats in one of the .sh files
export DIR=/Users/darren/nuke/pipeline
export JOB=RnD
export SHOT=base
The issue here is the sub shell. As you may be aware, environment variables set in a sub shell don't propagate to the parent.
Let's call the script in your question as menu.sh. It is calling other scripts based on user's choice, through this line:
. $filename
Looks like you are invoking your script as menu.sh. That would run in a sub shell and the environment variables set through the above . $filename call are lost when it finishes.
To make it work properly, you need to invoke menu.sh as:
source /path/to/menu.sh
or
. /path/to/menu.sh

OSX : absolute desktop file path ? (including /Volumes)

When I try to retrieve the path of a file on my desktop, I get :
/Users/MyName/Desktop/file.txt
But I need the full path (to feed a bash script):
/Volumes/MyDrive/Users/MyName/Desktop/file.txt
I spent quite some time searching for a solution, but couldn't find any.
I'm open to anything that can be run through Automator, bash or applescript.
It makes me crazy because if I simply drop my file.txt in Coda, it will output the full absolute path I want, right away, whereas Terminal won't.
Any ideas ?
So you need to find which volume under /Volumes is equivalent to /, yes? That's easy - just look for the symlink to /.
for vol in /Volumes/*; do
if [ "$(readlink "$vol")" = / ]; then
root_vol=$vol
fi
done
Then you can do
echo "$root_vol$HOME/Desktop/file.txt"
or whatever.
For anyone who might need it :
if you're running an OSX Automator workflow/application/service and needs an absolute path to your input file, simply add a "Run shell script" action, set the input "as arguments" and use the following :
#!/bin/bash
## Check if the input is an absolute path including the volume name
if [[ "$1" = /Volumes/* ]]
## If TRUE, the path is already correct
then
echo "$1"
## If FALSE, needs fixing
else
## Resolve the volume name
## by looping through all the volumes to find the one symlinked to "/"
for vol in /Volumes/*
do
if [ "$(readlink "$vol")" = / ]
then root_vol=$vol
fi
done
## Pass the path back to Automator for further use
echo "$root_vol$1"
fi

Trouble Passing Parameter into Simple ShellScript (command not found)

I am trying to write a simple shell-script that prints out the first parameter if there is one and prints "none" if it doesn't. The script is called test.sh
if [$1 = ""]
then
echo "none"
else
echo $1
fi
If I run the script without a parameter everything works. However if I run this command source test.sh -test, I get this error -bash: [test: command not found before the script continues on and correctly echos test. What am I doing wrong?
you need spaces before/after '[',']' chars, i.e.
if [ "$1" = "" ] ; then
#---^---------^ here
echo "none"
else
echo "$1"
fi
And you need to wrap your reference (really all references) to $1 with quotes as edited above.
After you fix that, you may also need to give a relative path to your script, i.e.
source ./test.sh -test
#------^^--- there
When you get a shell error message has you have here, it almost always helps to turn on shell debugging with set -vx before the lines that are causing your trouble, OR very near the top your script. Then you can see each line/block of code that is being executed, AND the value of the variables that the shell is using.
I hope this helps.

Resources