Incrementing a variable within a string in csh - linux

I'm writing a script in csh (it needs to be in csh)
and I've really been struggling on what seems like a trivial problem.
I want to set an environment variable with string containing a numeric variable. However I want to increment the value being stored without affecting the value. Is this possible. I've spent quite a while digging through csh tutorials and I'm at a loss.
Below is the last iteration of what I have tried. Nothing seems to work.
set LOG_HIST = 8
setenv LOG_FILE "/foo/log/foo."${LOG_FILE}".log"
setenv NEXT_LOG_FILE "/foo/log/foo."(${LOG_FILE} + 1)".log"

You can use the backticks to get the output of a command and use expr to evaluate an arithmetical expression:
% set LOG_HIST = 8
% setenv LOG_FILE "/foo/log/foo."${LOG_HIST}".log"
% setenv NEXT_LOG_FILE "/foo/log/foo."`expr ${LOG_HIST} + 1`.log
% echo $NEXT_LOG_FILE
/foo/log/foo.9.log

Related

Indirect expansion returns variable name instead of value

I am trying to set up some variables using indirect expansion. According to the documentation I've read, the set up should be simple:
var1=qa
qa_num=12345
varname="${var1}_ci"
echo ${!varname}
I should be getting "12345". Instead, the output is "varname". If I remove the exclamation point, I end up with "qa_ci", not "12345"
This should be a relatively simple solution, so I'm not sure what I'm missing, if anything.
Your code defines qa_num, but the varname assignment references qa_ci. As a result, your echo was expanding nonexistent qa_ci, giving empty results. Changing the varname assignment fixes the problem on my system.
Example: foo.sh:
#!/bin/bash
var1=qa
qa_num=12345
varname="${var1}_num" # <=== not _ci
echo "${!varname}" # I also added "" here as a general good practice
Output:
$ bash foo.sh
12345

Set environment variable to parent directory if not already set

I'm writing a bash script, and it needs to check if an environment variable exists, and set it to the parent directory of where the script is being run if the variable isn't already set. If it is already set, it should do nothing. What's the best way of doing this?
There are two parts. First, the parent of the current working directory is just $PWD/... Second, you can assign a value to a variable if it isn't already set with
: ${MYVAR:=$PWD/..}
The first : is the do-nothing command, but its arguments are still evaluated. The parameter expansion operator := has the side effect of setting MYVAR to the given value if it isn't already set.
Three things:
An environment variable is a globally available, in a program and it child programs. A shell variable is only available in the current shell. You asked to know whether the environment variable exists. Are you sure that is what you want? In that case you may want to do a
if set | grep ^variable_name= > /dev/null ; then
#set the variable
fi
Note that this just checks if the variable exists. If you do it very early in your script, you are almost sure that the variable is indeed an environment variable. But do you really care if it is a variable specific to your local shell or an environment variable?
Second is, do you care if the variable exists? Or do you just want it to contain the value of the parent directory? Do you need the set | grep in the example above, or is it sufficient to test that [ "$variable" != "" ] ? , as in
if [ "$variable" = "" ] ; then
# set the value to the parent-dir
fi
Third, the parent-dir is generally dirname $PWD. $PWD/.. is also the parent-dir.
So, if you do not care whether it is is an environment variable or not, and if you just want it to contain an actual directory, the code would be something like:
if [ "$variable" = "" ] ; then
variable=$(dirname "$PWD")
fi
(which is perhaps a bit more readable Chepner's answer)

create variables inside unix script

I am trying to create a variable in a script, based on another variable.
I just don't know what needs to be adjusted in my code, if it is possible.
I am simplifying my code for your understanding, so this is not the original code.
The code goes like that:
#!/bin/csh -f
set list_names=(Albert Bela Corine David)
set Albert_house_number=1
set Bela_house_number=2
set Corine_house_number=3
set David_house_number=4
foreach name ($list_names)
#following line does not work....
set house_number=$($name\_house_number)
echo $house_number
end
the desired output should be:
1
2
3
4
Thanks for your help.
Unfortunately, the bashism ${!varname} is not available to us in csh, so we'll have to go the old-fashioned route using backticks and eval. csh's quoting rules are different from those of POSIX-conforming shells, so all of this is csh specific. Here we go:
set house_number = `eval echo \" \$${name}_house_number\"`
echo "$house_number"
${name} is expanded into the backticked command, so this becomes equivalent to, say,
set house_number = `eval echo \" \$Albert_house_number\"`
which then evaluates
echo " $Albert_house_number"
and because of the backticks, the output of that is then assigned to house_number.
The space before \$$ is necessary in case the value of the expanded variable has special meaning to echo (such as -n). We could not simply use echo "-n" (it wouldn't print anything), but echo " -n" is fine.1
The extra space is stripped by csh when the backtick expression is expanded. This leads us to the remaining caveat: Spaces in variable values are going to be stripped; csh's backticks do that. This means that if Albert_house_number were defined as
set Albert_house_number = "3 4"
house_number would end up with the value 3 4 (with only one space). I don't know a way to prevent this.
1 Note that in this case, the echo "$house_number" line would have to be amended as well, or it would run echo "-n" and not print anything even though house_number has the correct value.

Read filename with * shell bash

I'am new in Linux and I want to write a bash script that can read in a file name of a directory that starts with LED + some numbers.(Ex.: LED5.5.002)
In that directory there is only one file that will starts with LED. The problem is that this file will every time be updated, so the next time it will be for example LED6.5.012 and counting.
I searched and tried a little bit and came to this solution:
export fspec=/home/led/LED*
LedV=`basename $fspec`
echo $LedV
If I give in those commands one by one in my terminal it works fine, LedV= LED5.5.002 but if i run it in a bash scripts it gives the result: LedV = LED*
I search after another solution:
a=/home/led/LED*
LedV=$(basename $a)
echo $LedV
but here again the same, if i give it in one by one it's ok but in a script: LedV = LED*.
It's probably something small but because of my lack of knowledge over Linux I cannot find it. So can someone tell what is wrong?
Thanks! Jan
Shell expansions don't happen on scalar assignments, so in
varname=foo*
the expansion of "$varname" will literally be "foo*". It's more confusing when you consider that echo $varname (or in your case basename $varname; either way without the double quotes) will cause the expansion itself to be treated as a glob, so you may well think the variable contains all those filenames.
Array expansions are another story. You might just want
fspec=( /path/LED* )
echo "${fspec[0]##*/}" # A parameter expansion to strip off the dirname
That will work fine for bash. Since POSIX sh doesn't have arrays like this, I like to give an alternative approach:
for fspec in /path/LED*; do
break
done
echo "${fspec##*/}"
pwd
/usr/local/src
ls -1 /usr/local/src/mysql*
/usr/local/src/mysql-cluster-gpl-7.3.4-linux-glibc2.5-x86_64.tar.gz
/usr/local/src/mysql-dump_test_all_dbs.sql
if you only have 1 file, you will only get 1 result
MyFile=`ls -1 /home/led/LED*`

SETENV: Bad : modifier in $ ($)

I am using the tcsh terminal in Linux. In the other terminal I normally used I set the path to some license file as follows:
export PATH="$PATH:$MODEL_TECH"
Tcsh shell does not recognise this command so I tried the following:
setenv PATH "$PATH:$MODEL_TECH"
set PATH "$PATH:$MODEL_TECH"
setenv PATH=("$PATH:$MODEL_TECH")
But then I always get the following error:
Bad : modifier in $ ($).
What be also great if someone could help me here out quickly, tried quite a few combinations but nothing works.
Drop the =
setenv LICENSE_FILE "/usr/local/softwarex/license.dat"
From the man page for tcsh:
setenv [name [value]]
Without arguments, prints the names and values of all environā€
ment variables. Given name, sets the environment variable name
to value or, without value, to the null string.
Put curly braces around the variable names:
setenv PATH ${PATH}:${foo}
or use this form:
set path = ($path $foo)
Try setenv LICENSE_FILE /usr/local/softwarex/license.dat. This should be documented in the man page somewhere on your system, so try reading up in man tcsh; tcsh is a very different beast from bash and friends. If the relevant man page isn't available on your system for some reason, here's the first man tcsh I found.
On a tcsh shell the path or any environment variable can be appended as below:
setenv PATH $PATH":$NEWPATH"
If it's not working use this:
setenv PATH ${PATH}:/.../../../

Resources