bash variable expansion in if statement [duplicate] - bash

This question already has answers here:
Tilde expansion in quotes
(3 answers)
Closed 7 years ago.
Checking for the existence of a directory which should resolve to ~/code/devtools/deploy-mix.
This if statement doesn't pass -- though I can cd ~/code/devtools/deploy-mix. Plz help with bash syntax :)
GIT_DIR="$HOME/code"
if [ -d "${GIT_DIR}/devtools/deploy-mix" ]; then
echo "found $GIT_DIR/devtools/deploy-mix"
fi
output:
sh -x script
'[' -d '$HOME/code/devtools/deploy-mix' ']'
I'd love to give out some internet karma for this one and I'm ok if you decide to call me a noob (sometimes the coding angle doesn't sit on your shoulder).

Replace ~ by $HOME or remove quotes.
Examples:
GIT_DIR="$HOME/code"
GIT_DIR=~/'code'

Related

bash if statement command not found [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
I have the simplest issue, but I can't get this to work, and it's driving me crazy. So I am running a script called execute.sh that has a help function. I swear my syntax is correct below; I have tried just one equal sign and now trying two. I have tried the double brackets and continue to get the error "[-h: command not found" . To execute the script I run the following the command.
sh execute.sh -h
Where -h is the argument I am passing int that forces the bash script to call the help function and then exist. But it keeps erroring on the IF Statement. Not sure what else to do, any suggestions?
arg=$1
if ["$arg" == "-h"]; then
helpFunction
exit1;
fi
In
["$arg" == "-h"]
line, you should separate the [ from " also " from the ] with a space. Because [ is actually a command, so it should have a space after it, and the syntax of [ command arguments require a space before ].
arg=$1
if [ "$arg" == "-h" ]; then
helpFunction
exit 1;
fi

Beginner: how can i use a $variable inside curly braces? [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
I'm having a hard time figuring out how to use a $var inside curly braces {}..
#!/bin/bash
read -p "How many files must there be built?" numb
for n in {1..$numb}
do
echo "Building file $n"
touch file$n.txt
done
This would result in:
How many files must there be built?8
Building file {1..8}
I've tried enclosing the $numb inside backticks or double quotes but to no avail..
You could use this syntax for your loop:
#!/bin/bash
read -p "How many files must there be built?" numb
for ((i=1; i<=$numb; i++)); do
echo "Building file $i"
touch file$i.txt
done
It's less fancy, but it works.

what is the -n operator in bash shell script and stand for? [duplicate]

This question already has answers here:
What does -n mean in Bash?
(2 answers)
Closed 3 years ago.
I found this -n operator in bash shell script. I don't have any clue about this and searched, but didn't find any helpful resource.
This is the script code:
#!/bin/bash
while [ -n "$1" ] ....
I also tried to get the output of the -n like:
#!/bin/bash
echo -n #return nothing
Your help will be appreciated!
-n string returns true if the length of the string is non-zero.
This is documented in Bash Conditional Expressions

using command line arguments in loop shell [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 3 years ago.
I am trying to use command line arguments for arithmetic but cant seem to find any documentation explaining how to do this. As an example if I use:
for i in {$1..$2}
do
echo $i
done
and call
test.sh 1 20
the following output is produced:
{1..20}
instead of
1
2
3
..
20
The following will also work:
declare -a ary='({'$1..$2'})'
for i in "${ary[#]}"; do
echo "$i"
done
Note that declare is as harmful as eval.
You need to check and sanitize the arguments before use.
There's no way to do this properly without the evil eval() with brace expansion in bash.
You can use seq instead :
for i in $(seq $1 $2); do

Bash Script : How to pass a wildcard before my filename in "if" exist file? [duplicate]

This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I wish to be able to check if file exist.
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
but the wilcard / joker don't work
How I can pass it ?
Your wildcard doesn't work because it's quoted. Unquoting it however might break the [ command as it only expects one filename argument, and if two or more files wore globbed it would break.
In bash you can use compgen that will generate a list of files matching the globbing pattern, it will also set proper exit status if no globs are found, it is a hack? I don't know, but it could look like it:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi

Resources