I have a problem with a simple subtraction but I don't understand what's wrong.
My code :
start= date +%s%N | cut -b1-13
#Treatment...
end= date +%s%N | cut -b1-13
delta=`expr $end - $start`
echo "delta $delta"
My console display :
1374652348283
...
1374652349207
delta -
My question is : Why do I got a - symbol returned ?
The command:
a= b
(note the space) will set a to an empty string while it runs the command b. It's a way to temporarily set environment variables for a single command, things like:
PATH=/path/to/somwhere gcc whatever # Here, PATH has the modified value.
echo $PATH # Here, PATH has its original value.
So the command line:
start= date +%s%N | cut -b1-13
sets start temporarily to nothing and runs the date command. Hence both start and end are still empty when you use them, which is why you only get the -, since expr - just gives you -.
If you want to get the results of the date command into a variable, use:
start=$(date +%s%N | cut -b1-13)
You didn't assign to the variables. You must not have spaces around the equals sign.
Also, you're doing it wrong.
start=$(date +%s%N | cut -b1-13)
Related
I am newbie in shell script , may be stupid query to experts, I am using following code to remove leading and trailing spaces from value, how do I assign output of echo variable to StringVar variable again or to other Variable. I am using ksh shell.
StringVar= ' abc '
echo StringVar | awk '{$1=$1};1'
x=$(command)
where x is the variable to which you want to assign the output of the command.
In your case, do not give space before or after the assignment operator =
StringVar=' abc '
x=$(echo "$StringVar" | awk '{$1=$1};1')
I need to store in a unix variable a string that can contain special characters.
In my case I am decrypting a text that returns to me Her$p7 that I need to store. Obviously that result can be any string (example i*Fi+K'7).
Would they know how I can save that result that I throw in a variable and use it like this
var=Her$p7
echo var
by example?
I use this for retrieve the string
echo "`${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'`"|perl -e print
The syntax is correct, if not robust, and the issue is in understanding string interpolation. In the standard string context, the dollar sign signifies to the shell that it's about to interpret a variable. Generally, this means to replace the variable with the value of the variable. Consider:
$ t1=Her$p7
$ t2="Her$p7"
$ t3='Her$p7'
$ t4="$(echo 'Her$p7')"
$ echo "t1: $t1; t2: $t2; t3: $t3, t4: $t4"
t1: Her; t2: Her; t3: Her$p7; t4: Her$p7
Note that while setting t1 (the first line) and t2, $p7 was interpreted as a variable (which you had not set), and thus was consequently replaced with it's value (empty/nothing). So, t1 and t2 were set to the value Her<nothing> -> Her.
In the third case, we used single quotes to tell the shell "no interpolation please; I mean strictly what I say". So, t3 is set to exactly the string you typed.
In the last case, we use the subshell operator ($( ... )) to set the variable t4 to the output of subshell command. In this case, we use double quotes to make sure we capture the entire output, but because we aren't typing the variable $p7, the shell won't interpolate the output of the command.
So, you should be good to go with something like:
$ yourVar=$(echo "`${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'`" | perl -e print)
Moving into the last decade, we could clean that up slightly by not using backticks for subshell operations:
$ yourVar=$(echo "$(${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}')" | perl -e print)
I'm trying to parse a lattice with grep and save the output in a variable in a cshell script. But somehow I always get the error message "Illegal variable name" when adding the FILE_IN_B variable. I tried different spacings after the variables name and also tried $() ,or %.* ,instead of sed to remove the last four letters but neither works in cshell. Also tried setting the declaration in "", to no avail. I'm really desperate here...
#!/bin/csh
set FILE_IN = file.ext
set SOURCE = home/Developer
set FILE_IN_B=`sed '/.\{4\}$//' >>> "$FILE_IN"`.lat
set REC = `grep -C 1 'I=11' "$SOURCE/Lattice/$FILE_IN_B" | cut -d ' ' -f 3 | cut -d= -f 2| sed 's/sp//'`
csh has some built-in variable manipulation, including one meant for extension removal:
% set FILE_IN=file.ext
% set FILE_IN_B="${FILE_IN:r}.lat"
% echo "$FILE_IN_B"
file.lat
If sed is required then printf can be used as the standard input, similar to portable sh:
% set FILE_IN=file.ext
% set FILE_IN_B=`printf %s "$FILE_IN" | sed 's/.\{4\}$//'`.lat
% echo "$FILE_IN_B"
file.lat
I have a variable var=/usr/local/bin/test/exec
Now i have to remove last 2 path components in the above variable say:
var=/usr/local/bin/
After removing the last 2 strings I have to use this variable 'var' in a shell loop.
I tried:
var='/usr/local/bin/test/exec'
echo ${var#$(dirname "$(dirname "$s")")/}
Output:
test/exec
I am getting the truncated part as output, but I was expecting the rest of the part, not the truncated part.
You may be interested in the shell's internal substring processing operators: %, %%, # and##. Observe:
#!/bin/sh
var=/usr/local/bin/test/exec
# use shell substring processing to cut the variable down to size:
var="${var%/*}"
var="${var%/*}"
echo "$var"
# Manipulate the resulting string in a loop
for i in 1 2 3
do echo "${var}${i}"
done
OK after some googling i have found the solution for this:
var1="$(echo $var | cut -d '/' -f-4)"
If you don't know the field count, there is a standard awk solution. However, I'll show another trick using rev
var='/usr/local/bin/test/exec'; echo $var | rev | cut -d/ -f3- | rev
will give
/usr/local/bin
You can try this method also
var=/usr/local/bin/test/exec
sed 's_\(.*\)/.*/.*$_\1_' <<< $var
Another Method
sed 's_\(.*\)\(/.*\)\{2\}$_\1_' <<< $var
Output:
/usr/local/bin
I have a number 2014061200 and I am trying to extract the one's digit of this number (0) and set it to a variable $STR, so that $STR would equal 0, because that is in the one's place of the above number.
I made a shell script showing the following:
$NUM=2014061200
$STR= $NUM | tail -c 2
echo $STR
However when I do this I get a blank for $STR instead of the expected result of 0. I mean when I type
echo $NUM | tail -c 2, I do get the output of 0, but how do I get this into a variable?
Thanks
Use parameter expansion:
num=2014061200
last=${num: -1}
-1 tells bash to extract one character from the right.
Here's another variation using parameter expansion where ${#num}-1 is the length of the string less 1 and ${num:position:length} is a substring expression:
${num:(${#num}-1):1}