I've got the following line in my script:
xrandr --newmode "$xx$y" $m
where $x and $y are integers, to produce an output like 1024x768.
Unfortunately my script is interpreting the $x as $xx - how can I stop this & get the desired behavior?
You could try to do it this way
"${x}x$y"
Have a look at the docs for more info.
The general logic is to isolate the variable so that it doesn't take xx as the variable name. There are several ways to do that:
Using quotes:
x="xvar";y="yvar"
echo ""$x"x$y"
echo ""$x"x"$y""
echo "$x""x""$y"
echo "$x"x"$y"
Using brace (mentioned by Alberto Zaccagni):
echo "${x}x$y"
Related
The code below tests if the character from a string is matching regex or not.
str=")Y"
c="${str:0:1}"
if [[ $c =~ [A-Za-z0-9_] ]]; then
echo "YES"
output=$c
else
echo "NO"
output="-"
fi
echo $output
I am running it with
source script-name.sh
However, instead of expected printout
NO
-
I am getting an empty line without dash
NO
I understand the issue is somehow around the way i (re-)assign output variable which being me to questions
How to do it properly?
Why source utility has such implication?
UPD_1: it is for Mac's zsh, not bash
UPD_2: the issue occurs only when running script via 'source' utility like "source script-name.sh"
Running with "./script-name.sh" yield correct result as well.
Your problem can be simplified to do on the zsh command line a
echo -
which also doesn't output anything. Similarily, a
echo - x
would output simply x and not - x.
This does not depend on whether or not you are on the Mac. If you would do a
echo - -
or a
=echo -
(the latter using the external program echo) would print a dash.
Therefore, you can change in your script-name.sh a
=echo $output
or a
echo - $output
and you should be fine.
The zshbuiltins man-page explains it, when describing the echo command:
the first dash, possibly following options, is not printed, but everything following it is printed as an argument.
Therefore, in zsh, at least when printing a variable, it is better to also use a lone dash for the safe side.
Your code gives the expected output for bash 4.2.46 on RHEL7.
Are you maybe using zsh?
See echo 'the character - (dash) in the unix command line
EDIT: Ok, if it's zsh, you probably have to use a hack:
if [[ ${output} == '-' ]]; then
echo - ${output}
else
echo ${output}
fi
or use printf:
printf $output"\n"
I want to echo $ANMIAL0 and then $ANIMAL1 using a script below.
But I get line 7: ${ANIMAL$i}: bad substitution error message. What's wrong?
#!/bin/sh
ANIMAL0="tiger"
ANIMAL1="lion"
i=0
while test $i -lt 2; do
echo "Hey $i !"
echo ${ANIMAL$i}
i=`expr $i + 1`
done
You're probably better off using an array instead of ANIMAL0 and ANIMAL1. Something like this maybe?
#!/bin/bash
animals=("Tiger" "Lion")
for animal in ${animals[*]}
do
printf "Hey, ${animal} \n"
done
Using eval will get you into trouble down the road and is not best practice for what you're trying to do.
The problem is that the shell normally performs a single substitution pass. You can force a second pass with eval, but this obviously comes with the usual security caveats (don't evaluate unchecked user input, etc).
eval echo \$ANIMAL$i
Bash has various constructs to help avoid eval.
You can use eval
eval echo \$ANIMAL$i
I am echoing a floating point variable. The results looks as follows:
.4983385178
I am using echo -e "$var". How can I tell echo also to print the 0 before the dot:
0.4983385178
Thanks in advance!
Jonas
printf may help:
$ printf '%.10f\n' .4983385178
0.4983385178
I did
a=1234
let "a=a+1"
on command line and it's fine. But when I do the same in a shell script. It prints out an error that "let: not found".
Here is the script file.
#!/bin/sh
a=1234;
let "a=a+1";
echo "$a";
Thanks,
Do not use let. Use POSIX arithmetic expansion: a=$(($a+1)). This is guaranteed to work in any POSIX-compliant shell.
The problem is likely that /bin/sh is not the same as, or does not behave the same as, your normal shell. For example, when bash is invoked as /bin/sh, it provides a subset of its normal features.
So, you may need to change your shebang line to use a different shell:
#!/bin/bash
or
#!/bin/ksh
You don't need the semi-colons at the ends of the lines.
See at: http://www.hlevkin.com/Shell_progr/hellobash.htm
The correct is:
a=1234;
b=1;
a=`expr $a + $b`;
You should use let a=a+1 without quotes
It's the '$a' of '$a=1234' that is killing you.
The shell does all $ substitutions and THEN evaluates the expression. As a result it saw "=1234" because there was no value to $a.
Use -x to see this.
bash -x your-script
Check your actual shell with the following command in the command line:
echo $SHELL
It will provide a shell name, use that instead of /bin/sh at the first line of your script.
Check the shell name using
echo $SHELL
Change the first line of the script accordingly to
#!/bin/bash
or
#!/bin/ksh
instead of #!/bin/sh.
c=1
d=1
for i in `ls`
do
if [ -f $i ]
then
echo "$c -> $i"
c=`expr $c + 1`
fi
done
c=`expr $c - 1`
echo no. of files $c
for i in `ls`
do
if [ -d $i ]
then
echo "$d -> $i"
d=`expr $d + 1`
fi
done
d=`expr $d - 1`
echo no. of direcrories $d
I have the following script:
#!/bin/sh
r=3
r=$((r+5))
echo r
However, I get this error:
Syntax error at line 3: $ unexpected.
I don't understand what I'm doing wrong. I'm following this online guide to the letter http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/
This sounds fine if you're using bash, but $((r+5)) might not be supported if you're using another shell. What does /bin/sh point to? Have you considered replacing it with /bin/bash if it's available?
The shebang line is your problem. bash is not sh. Change it to #!/bin/bash and it will work. You'll also want echo $r instead of echo r.
It works for me (printing 8), if you change echo r to echo $r. What version of sh do you have installed? What unix distribution?
You might want to try the following:
#!/bin/sh
r=3
r=$((r + 5))
echo $r
For doing maths (including decimals/floats), you can use awkor bc/dc.
awk -vr="$r" 'BEGIN{r=r+5;print r}'
or
echo "$r+5" | bc