Concatenate string literals - linux

I've seen a couple posts for this, like this one, but none are helping me in my particular situation.
scriptsPath="/var/db/gbi/scripts/"
echo "$scriptsPathawesome.csv";
I would expect this to echo /var/db/gbi/scripts/awesome.csv
Instead I get .csv
Seems like it thinks I'm trying to reference a variable named $scriptsPathawesome. How can I concatenate the $scriptsPath variable to the "awesome.csv" string literal?

You need to surround your variable with curly braces like so:
scriptsPath="/var/db/gbi/scripts/"
echo "${scriptsPath}awesome.csv";

Related

Bash - Creating a string with a string variable

I have a variable called CURRENTDATE=20151105.
I want to create a string as below:
abc_20151105_20151105
I tried the following variations:
echo "abc_$CURRENTDATE_$CURRENTDATE"
This gave abc_20151105
echo "abc_'$CURRENTDATE'_'$CURRENTDATE'"
This gave abc_'20151105'_'20151105'
What am I missing here? Thanks in advance!
The problem is that the underscore is a valid character for a variable name. Try one of these:
echo "abc_"$CURRENT_DATE"_"$CURRENT_DATE
echo "abc_${CURRENT_DATE}_$CURRENT_DATE"
Bash doesn't have a concatenation operator, so you concatenate strings by smashing them together in the command; this is what the first example is doing. The second uses braces to explicitly point out the variable name.
You must surround the variable name with ${} in order to isolate it from other valid characters. Like this:
echo "abc_${CURRENT_DATE}_${CURRENT_DATE}"

BASH variable values as new defined variables

I don't know exactly how to ask this in English, but I want to have the value of a variable as a new variable...
The script also has a loop with increasing numbers, and in the end I want to have the variables VAR1, VAR2 etc.
I'm trying this:
COUNT=$(echo 1)
DEFINE=$(echo VAR$COUNT)
$DEFINE=$(echo gotcha!)
When I try this way, I have this error message:
~/script.sh: line n: VAR1=gotcha!: command not found
I played a bit around with brackets and quotation marks, but it didn't work... any solutions?
The problem is that bash expects a command as a result of expansions, not an assignment. VAR1=gotcha! is not a command, hence the error.
It would be better to use an array:
COUNT=$(echo 1)
VAR[COUNT]='gotcha!'
echo ${VAR[COUNT]}
I guess $(echo 1) stands for a more complex command, otherwise you can just use COUNT=1.
You can use declare to create such a "dynamic" variable, but using an array is probably a better choice.
COUNT=1
DEFINE="VAR$COUNT"
declare "$DEFINE=gotcha"

Unterminated string in AutoIT

I like to make a script in AutoIT to work in Excel. when i trying to make a variable with the following argument is giving me error.
$myformula = "=IF(OR(ISNUMBER(search(" "&{"quality","st","rainbow","carryout","auto","surgery","public","fertiliser","Fertilisers","ifertilisersl","rendezvous","remember","that","precast","barefoot","mail","scaffolding","safe","access","salvation","army","funeral","directors","director","seahorse","simply","affordable","stich","time","dressmaking","alterations","hospital","by","jewellers","post","sweet","sensation","trophies","engraving","protection","produce","public","relations","made","fun","auctions","auction","kiltmakers","kiltmaker","linen","chippy","timber","tidy","tangles","past","dealer","trading","crafts","craft","naval","activities","US","Uncle","Diner","Removals","Volunteer","Zoo","Wavelength","Wavelengths","Friends","hairdressing","theatreclub","daynursery","MUSLC","superstore","colleges","AALCO","AARDVARK","ABACUS","ABB","ABBOTSWELL","ABBOTTPUBLIC","ABC","ABCON","ABDN","ABERCARE","ABERDEE","ABERDEEN","ABERDEENSHIRE","ABERFLORA","ABERGELDIE","ABERGLEN","ABERLOUR","ABERMED","ABERNE","ABERPEST","ABERSOL","ABERT","ABSCOT","ABSOFT","ABSPEK","ABSURE","ABTEX","ABTRUST","ACADEMY","ACCESS","ACCESSORIES","ACCOMMODATION","ACCORD","ACCOUNTANCY","ACCURAY","ACE","ACTION","ADAPT","ADEPT","ADMIN","ADMIRAL","ADS","ADULT","ADVANCE","ADVANCED","ADVANT","ADVANTAGE","ADVICE","AEA","AFFAIR","AFOS","AGA","AGE","AGENCY","AGIP","AGRI","AGRICULTURAL","AGRICULTURE","AHT","AI","AIDS","AIR","AIRBORNE","AIRFAIR","AIRLINES","AIRNAUTIC","AIRPAC","AIRPORT","AIRWAYS","AIRYHALL","AIRYLEA","AKRON","ALAD","ALASDAIR","ALB","ALBATECH","ALFA","ALL","ALLIANCE","ALLIED","ALLOMAX","ALPINE","ALSTEV","ALTERNATIVES","ALTRA","ALWAYS","ALZHEIMER","AM","AMALGAMATED","AMAT","AMATOLA","AMBER","AMEC","AMERICAN","AMETEK","AMIN","AMITEC","AMOCO","AMTECH","AMTRAK","AMUSEMENT","AND","ANDERGAUGE","ANIXTER","ANKA","ANTIQUES","ANTRIM","AOC","AP","APARDION","APEX","APOLLO","APOSTOLIC","APPEALS","APPLIED","APPOLLO","AQUA","AQUARISTS","AQUATEC","AQUATIC","AQUIDATA","ARCHITECTURAL","ARCO","ARGONAUT","ARGOSY","ARISTACUT","ARJO","ARJON","ARKAIG","ARTISTES","ARTISTS","ARTWORKS","ARUP","AS","ASA","ASCOT","ASDASUPERSTORES","ASHVALE","ASPECT","ASSEMBLIES","ASSESSORS","ASSOCIATED","ASSOCIATES","ASSOCIATION","ASSURANCE","ASTER","ASTRA","ATEL","ATHENA","ATHENAEUM","ATHOLL","ATLANTIC","AUCHMILL","AUCHTERLESS","AUGHTON","AUGUST","AUP","AUQUHARNEY","AURORA","AUTO","AUTOFIX","AUTOGLASS","AUTOMAGIC","AUTOQUICK","AUTOSAVE","AUTOSPRAY","AUTOTRUCK","AVCO","AVENUE","AWAKENING","AWAY","AXIOM","AZTEC","BAADER","BALNAGASK","BANKHEAD","BAR","BARICO","BARRIER","BARTERING","BASELINE","BATCHBRIGHT","BATH","BATHROOM","BAVARIA","BAYLISS","BAYLOR","BEARING","BEATTIES","BEAUTY","BEAVERBROOKS","BEECHWOOD","BEEFEATER","BEELINE","BEFRIEND","BENBOW","BESTWAY","BEWS","BIKES","BLIND","BLINDCRAFT","BLINDS","BLOOD","BLOOMING","BLOSSOMS","BLOW","BLUEBELLS","BMT","BNA","BOARD","BOARS","BOAT","BODY","BOHEMIA","BOILERMAKERS","BON","BONADDIO","BONAVENTURE","BONIVIEW","BONNYMUIR","BOOKBINDING","BOOTS","BOROWSKI","BORSALINO","BOSANQUET","BOSCH","BOTTLED","BOULEVARD","BOURTREE","BOWEN","BOWLING","BOWTECH","BOX","BP","BPCC","BRA","BRACKENRIDGE","BRAEHEAD","BRAESIDE","BRAKE","BRIEF","BRITANNIA","BRITANNIC","BROAD","BROADCAST","BROADSTRAIK","BROS","BROTHERHOOD","BRUNSWICK","BUCKSBURN","BUDGET","BUILDING","BULAWEYO","BUREAU","BURNTHILLS","BUS","BUSINESS","CABER","CABINS","CABLE","CABS","CAFE","CAIRD","CAIRNCRY","CAIRNGORM","CAKE","CALEDON","CALEDONIA","CALEDONIAN","CALLANDERS","CALOR","CALTEC","CALVARYTABERNACLEUNITED","CAMARGUE","CAMEO","CAMPAIGN","CAMPBELLS","CAMPHILL","CAMWATER","CAN","CANALE","CANCER","CANDO","CANSCO","CAPE","CARADON","CARD","CAREERS","CARGO","CARPET","CARPETS"}&" "," "&b1&" "))),"YES","NO")"
I tried with double quotes as well. What shall /i do?
Strings are enclosed in double-quotes like "this". If you want a string to actually contain a double-quote use it twice like:
"here is a ""double-quote"" - ok?"
You can also use single-quotes like 'this' and 'here is a ' 'single-quote' ' - ok?'
You can mix quote types to make for easier working and to avoid having to double-up your quotes to get what you want. For example if you want to use a lot of double-quotes in your strings then you should use single-quotes for declaring them:
'This "sentence" contains "lots" of "double-quotes" does it not?'
is much simpler than:
"This ""sentence"" contains ""lots"" of ""double-quotes"" does it not?"
Just use ' instead of ".
Like this:
$myformula = '=IF(OR(ISNUMBER(search(" "&{"quality","st","rainbow","carryout","auto","surgery","public","fertiliser","Fertilisers","ifertilisersl","rendezvous","remember","that","precast","barefoot","mail","scaffolding","safe","access","salvation","army","funeral","directors","director","seahorse","simply","affordable","stich","time","dressmaking","alterations","hospital","by","jewellers","post","sweet","sensation","trophies","engraving","protection","produce","public","relations","made","fun","auctions","auction","kiltmakers","kiltmaker","linen","chippy","timber","tidy","tangles","past","dealer","trading","crafts","craft","naval","activities","US","Uncle","Diner","Removals","Volunteer","Zoo","Wavelength","Wavelengths","Friends","hairdressing","theatreclub","daynursery","MUSLC","superstore","colleges","AALCO","AARDVARK","ABACUS","ABB","ABBOTSWELL","ABBOTTPUBLIC","ABC","ABCON","ABDN","ABERCARE","ABERDEE","ABERDEEN","ABERDEENSHIRE","ABERFLORA","ABERGELDIE","ABERGLEN","ABERLOUR","ABERMED","ABERNE","ABERPEST","ABERSOL","ABERT","ABSCOT","ABSOFT","ABSPEK","ABSURE","ABTEX","ABTRUST","ACADEMY","ACCESS","ACCESSORIES","ACCOMMODATION","ACCORD","ACCOUNTANCY","ACCURAY","ACE","ACTION","ADAPT","ADEPT","ADMIN","ADMIRAL","ADS","ADULT","ADVANCE","ADVANCED","ADVANT","ADVANTAGE","ADVICE","AEA","AFFAIR","AFOS","AGA","AGE","AGENCY","AGIP","AGRI","AGRICULTURAL","AGRICULTURE","AHT","AI","AIDS","AIR","AIRBORNE","AIRFAIR","AIRLINES","AIRNAUTIC","AIRPAC","AIRPORT","AIRWAYS","AIRYHALL","AIRYLEA","AKRON","ALAD","ALASDAIR","ALB","ALBATECH","ALFA","ALL","ALLIANCE","ALLIED","ALLOMAX","ALPINE","ALSTEV","ALTERNATIVES","ALTRA","ALWAYS","ALZHEIMER","AM","AMALGAMATED","AMAT","AMATOLA","AMBER","AMEC","AMERICAN","AMETEK","AMIN","AMITEC","AMOCO","AMTECH","AMTRAK","AMUSEMENT","AND","ANDERGAUGE","ANIXTER","ANKA","ANTIQUES","ANTRIM","AOC","AP","APARDION","APEX","APOLLO","APOSTOLIC","APPEALS","APPLIED","APPOLLO","AQUA","AQUARISTS","AQUATEC","AQUATIC","AQUIDATA","ARCHITECTURAL","ARCO","ARGONAUT","ARGOSY","ARISTACUT","ARJO","ARJON","ARKAIG","ARTISTES","ARTISTS","ARTWORKS","ARUP","AS","ASA","ASCOT","ASDASUPERSTORES","ASHVALE","ASPECT","ASSEMBLIES","ASSESSORS","ASSOCIATED","ASSOCIATES","ASSOCIATION","ASSURANCE","ASTER","ASTRA","ATEL","ATHENA","ATHENAEUM","ATHOLL","ATLANTIC","AUCHMILL","AUCHTERLESS","AUGHTON","AUGUST","AUP","AUQUHARNEY","AURORA","AUTO","AUTOFIX","AUTOGLASS","AUTOMAGIC","AUTOQUICK","AUTOSAVE","AUTOSPRAY","AUTOTRUCK","AVCO","AVENUE","AWAKENING","AWAY","AXIOM","AZTEC","BAADER","BALNAGASK","BANKHEAD","BAR","BARICO","BARRIER","BARTERING","BASELINE","BATCHBRIGHT","BATH","BATHROOM","BAVARIA","BAYLISS","BAYLOR","BEARING","BEATTIES","BEAUTY","BEAVERBROOKS","BEECHWOOD","BEEFEATER","BEELINE","BEFRIEND","BENBOW","BESTWAY","BEWS","BIKES","BLIND","BLINDCRAFT","BLINDS","BLOOD","BLOOMING","BLOSSOMS","BLOW","BLUEBELLS","BMT","BNA","BOARD","BOARS","BOAT","BODY","BOHEMIA","BOILERMAKERS","BON","BONADDIO","BONAVENTURE","BONIVIEW","BONNYMUIR","BOOKBINDING","BOOTS","BOROWSKI","BORSALINO","BOSANQUET","BOSCH","BOTTLED","BOULEVARD","BOURTREE","BOWEN","BOWLING","BOWTECH","BOX","BP","BPCC","BRA","BRACKENRIDGE","BRAEHEAD","BRAESIDE","BRAKE","BRIEF","BRITANNIA","BRITANNIC","BROAD","BROADCAST","BROADSTRAIK","BROS","BROTHERHOOD","BRUNSWICK","BUCKSBURN","BUDGET","BUILDING","BULAWEYO","BUREAU","BURNTHILLS","BUS","BUSINESS","CABER","CABINS","CABLE","CABS","CAFE","CAIRD","CAIRNCRY","CAIRNGORM","CAKE","CALEDON","CALEDONIA","CALEDONIAN","CALLANDERS","CALOR","CALTEC","CALVARYTABERNACLEUNITED","CAMARGUE","CAMEO","CAMPAIGN","CAMPBELLS","CAMPHILL","CAMWATER","CAN","CANALE","CANCER","CANDO","CANSCO","CAPE","CARADON","CARD","CAREERS","CARGO","CARPET","CARPETS"}&" "," "&b1&" "))),"YES","NO")'
You have a lot of quotes in there. Try rewriting it adding just one string at a time...once you get it working, then you can start adding more than one string at a time.

A variable of which a part is variable

I have variables that look something like this:
$INFOMonday
$INFOTuesday
In my Bash script, I would like to use the value of todays variable, so to say. I extracted the day with:
dayofweek=$(date --date=${dateinfile#?_} "+%A")
What I need help with, is doing the following:
echo $INFO$dayofweek
Is there any way of adding a variable as part of a variables name? For example, if it's monday, the echo would return the value of $INFOMonday.
The old-style way of doing this is with the indirection operator ${!variable}:
dayofweek=$(date...)
var=INFO$dayofweek
echo ${!var}
Note that in ${!var}, var must be a variable-name, not a string expression or other type of substitution. It's a little clunky.
The newer way of doing it would be with an associative array.

Difference between Groovy String variable replacement

When looking at examples of variable substitution in GStrings, I have noticed two difference syntaxes. This can be seen here: Groovy Templates
This has the example:
def text = 'Dear "$firstname $lastname",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}'
It looks like ${variable} is used more commonly when you have an expression, but $variable is used when you just have a single variable, but even here they mix it with $firstname and ${month}. Is there a reason to do it one way or another when you have a single variable and not an expression, or does it not matter?
It doesn't matter...
As you say, if you have an expression like "${name.toUpperCase()}", "${number}th" or "${list[0]}", then it has to be inside braces, but both "${name}" and "$name" are the same.
Indeed, so long as it's simple property access you can omit the braces, ie: "Hello $person.username"
It could be said that adding the braces can make your string templates easier to read, but that's a personal preference thing.

Resources