Assign variables value to a variable - add

Using the command line, I've defined two variables
set a = 5
set b = 5
In addition I've set another variable, c, in which I am trying to assign a's and b's value.
I tried -
set c = $($a+$b)
But I've got Illegal variable name.
I tried -
set c
c = $($a+$b)
But I've got Illegal variable name., again.

Set variable and assign value:
# a = 5
This is same as:
set a = 5
See value of the variable:
echo $a
You may try following way:
# a = 4
# b = 5
# c = $a + $b
echo $c
9
Don't forget #, it's used instead of 'set'
You can have some basic ideas about working in tcsh from this site
PS. Never worked in tcshso please take my answer/suggestion with a pinch of salt.

Change your script to:
a=5
b=4
c=$(($a + $b))
# test
echo $c
# prints 9
Note that double parenthesis and the removal of the set keyword.

set a = 5
set b = 5
set c = `expr $a + $b`

Related

How to convert result as Integer in bash

when I do
$ ls | wc -l
703
It gave me the result 703, I want to print 702 (703-1)
How can I do it in bash?
You can use arithmetic expansion:
result=$(( $(ls | wc - l) - 1))
or just ignore one of the files
result=$(ls | tail -n+2 | wc -l)
Note that it doesn't work if filenames contain the newline character; use ls -q to get one filename per line in such a case. This applies to the first solution, too, if you're interested in the number of files and not the number of lines in their names.
(Cheeky answer) Remove one line from the output before counting :D
ls | sed '1d' | wc -l
How to convert result as Integer in bash
#choroba has already answered this question and it should have solved OP's problem. However, I want to add more to his answer.
The OP's wants to convert the result into Integer but Bash doesn't have any data type like Integer.
Unlike many other programming languages, Bash does not segregate its variables by "type." Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.
See this for arithmetic operation in Bash.
See this for a best example to learn the untyped nature of Bash. I have posted the example below:
#!/bin/bash
# int-or-string.sh
a=2334 # Integer.
let "a += 1"
echo "a = $a " # a = 2335
echo # Integer, still.
b=${a/23/BB} # Substitute "BB" for "23".
# This transforms $b into a string.
echo "b = $b" # b = BB35
declare -i b # Declaring it an integer doesn't help.
echo "b = $b" # b = BB35
let "b += 1" # BB35 + 1
echo "b = $b" # b = 1
echo # Bash sets the "integer value" of a string to 0.
c=BB34
echo "c = $c" # c = BB34
d=${c/BB/23} # Substitute "23" for "BB".
# This makes $d an integer.
echo "d = $d" # d = 2334
let "d += 1" # 2334 + 1
echo "d = $d" # d = 2335
echo
# What about null variables?
e='' # ... Or e="" ... Or e=
echo "e = $e" # e =
let "e += 1" # Arithmetic operations allowed on a null variable?
echo "e = $e" # e = 1
echo # Null variable transformed into an integer.
# What about undeclared variables?
echo "f = $f" # f =
let "f += 1" # Arithmetic operations allowed?
echo "f = $f" # f = 1
echo # Undeclared variable transformed into an integer.
#
# However ...
let "f /= $undecl_var" # Divide by zero?
# let: f /= : syntax error: operand expected (error token is " ")
# Syntax error! Variable $undecl_var is not set to zero here!
#
# But still ...
let "f /= 0"
# let: f /= 0: division by 0 (error token is "0")
# Expected behavior.
# Bash (usually) sets the "integer value" of null to zero
#+ when performing an arithmetic operation.
# But, don't try this at home, folks!
# It's undocumented and probably non-portable behavior.
# Conclusion: Variables in Bash are untyped,
#+ with all attendant consequences.
exit $?

Passing a variable twice in Bash

I want the command: echo "$string4" to return me the values of the SETFIELDS_X_XX variables defined in the beginning of my file, during the loop. However, it only returns me the name SETFIELDS_X_XX. What am I doing wrong?
SETFIELDS_2_25=225
SETFIELDS_2_200=2200
SETFIELDS_2_500=2500
SETFIELDS_10_25=1025
SETFIELDS_10_200=10200
SETFIELDS_10_500=10500
SETFIELDS_10_1000=101000
SETFIELDS_50_25=5025
SETFIELDS_50_200=50200
SETFIELDS_50_500=50500
SETFIELDS_50_1000=501000
for LBUB in 2 10 50
do
for generations in 25 200 500 1000
do
string1="SETFIELDS_"
string2="$LBUB"
string3="_$generations"
string4=$string1$string2$string3
#echo "LBUB = $LBUB"
#echo "generations = $generations"
echo "$string4"
done
done
Use this instead:
echo ${!string4}
It will print the value stored inside the variable SETFIELDS_X_XX

perl6 Any string port similar to Racket Scheme for reading data?

In Racket Scheme, there is a data structure called "string port" and you can read data from it. Anything similar in perl6? For examples, I want to achieve these outcomes:
my $a = "(1,2,3,4,5)"; # if you read from $a, you get a list that you can use;
my $aList=readStringPort($a);
say $aList.WHAT; # (List)
say $aList.elems; # 5 elements
my $b = "[1,2,3]"; # you get an array to use if you read from it;
my $c = "sub ($one) {say $one;}";
$c("Big Bang"); # says Big Bang
The function EVAL is not quite doing the full spectrum of tasks:
> EVAL "1,2,3"
(1 2 3)
> my $a = EVAL "1,2,3"
(1 2 3)
> $a.WHAT
(List)
> my $b = EVAL "sub ($one) {say $one;}";
===SORRY!=== Error while compiling:
Variable '$one' is not declared. Did you mean '&one'?
------> my $b = EVAL "sub (⏏$one) {say $one;}";
Thanks a lot!
lisprog
EVAL does this.
The problem in your last example, is that double-quoted strings interpolate $ variables and { blocks and similar. In order to represent such things in a string literal, either escape them with backslashes...
my $b = EVAL "sub (\$one) \{say \$one;}";
...or use a non-interpolating string literal:
my $b = EVAL 'sub ($one) {say $one;}';
my $b = EVAL Q[sub ($one) {say $one;}];

Bash shell: How to assign the value of $A to $B given $B='$A' (string, dollar A)?

In Bash shell, given variable
$B = "$A"
which is the string literal and I need to check if the value of B starts with a dollar sign:
if [[ $B == *"$"* ]]; then
How do I assign the value of variable, whose name is the string value of $B, to $B itself?
Thanks!
Update:
To make my question clear, I'll use another example, say I have a variable
$SCORE = 100
and another variable
$B = '$SCORE'
a string variable whose value is the dollar sign name of SCORE, now I want to assign the actual integer score (100) to variable $B.
If you're willing to change your requirements a bit, you can use variable "indirection"
score=100
b="score"
echo "${!b}" # note the bang
If the $ has to be there:
b='$score'
tmp=${b/#$} # remove the leading $
echo "${!tmp}"

Bash variables definition order

I have a setting.sh file with this line:
HOME="/var/lib/${USER}"
Then I have a shell.sh with these lines (in order):
. ./settings.sh
USER="test"
Can I set a $home without first setting $user?
If I use a default user in setting.sh and then set it as a new value in shell.sh, will it update the home variable?
No to both. Statements are executed in order. If you change $USER that will not retroactively update $HOME. Makefiles have lazy variable expansions, but not shell scripts.
You could reorder the statements:
USER="test"
. ./settings.sh
Answers to your questions:
1. No
2. No
You can ( so $user = "")
It will not update your $HOME variable
typical computer programming concepts
int a = 5; // a = 5
int c = a; // c = 5
a = 7; // c is still 5
Not sure if this was worth a question. You can easily try this and answer your own question:
$ A="B IS ${B}"
$ echo $A
B IS
$ B=FOO
$ echo $B
FOO
$ echo $A
B IS
In other words, the right-hand side expression is evaluated on assignment. Which means that answers to both of your questions are no.
What you can do, however, is to perform lazy evaluation. For example:
$ A="B IS \${B}"
$ echo $A
B IS ${B}
$ eval echo $A
B IS
$ B=FOO
$ echo $A
B IS ${B}
$ eval echo $A
B IS FOO
$
Good Luck!

Resources