unable to call a stored variable in shell script - shell

I have a very basic script that starts with saving part of input argument to a varialbe:
dirN= basename $1
echo $dirN
$dirN was displayed as expected
then I try to cat a string with $dirN
tmp="/some/path/$dirN"
when I echo $tmp, it only displays /some/path/
I tried overwriting tmp
tmp=$dirN
and echo $tmp,
it shows nothing.
it's like $dirN was never stored, but it was echoed from line 2
I am very confused, so was my colleague.
Any hint?
Thank you all!

I believe you want
dirN=`basename $1`
to actually store the value returned instead of
dirN= basename $1
The "echo $dirN" is not showing anything in your version, it's the previous line that shows the output you attribute to the echo.

Related

How to split the path to get the directory in shell script

I am trying to split the path of a file to get the directory name to check if the directory exists in the new location or not using shell script.
I tried using
cf=src/classes/CarExperience.cls
echo ${cf%/*}
echo ${cf##/*/}
echo ${cf#/*/*/}
echo ${cf%/*}
echo $(dirname "$cf")
But none of these are giving me desired result
Desired result is get part after the src and check if that inner directory exists or not.
cf=src/classes/CarExperience.cls
directory_name=classes
Appreciate any help on this regard.
You could do:
full_dir=$(dirname "$cf")
last_dir=$(basename "$full_dir")
or in one shot
last_dir=$(basename "$(dirname "$cf")")
Yes, you want all those quotes.
With shell parameter expansion:
full_dir=${cf%/*}
last_dir=${full_dir##*/}
That one has to be done in 2 steps.
Like this, using parameter expansion as you try to do:
cf=src/classes/CarExperience.cls
cf=${cf#src/*} # become 'classes/CarExperience.cls'
echo ${cf%/*} # become 'classes'
Output
classes

problem with shell new line when output to file

i have a problem when I want to output several variables to txt file in shell. I want them in one line, but there appear line break after first and third variable. I have for example this
for s in `ls $TESTDIR/results_orig.txt`
do
t_a=(`grep " a" $s`);
t_b=(`grep "Bytes written to file: " $s`);
t_c=(`grep " Total Time:" $s`);
t_d=(`grep "QP " $s`);
a=${t_a[2]};
b=${t_b[4]};
c=${t_c[3]};
d=${t_d[2]};
echo "$a $b $c $d" >> $TESTDIR/results.txt;
done
I got the variables t_a to t_d are after I parse original txt file to find some values in it. Than I want to find some number ant write them in file without other text that I have in original file. And I want it to write those parsed numbers in one line in new file, like
13.2 1678 3231.5 2422.1
And I get
13.2
1678 3231.5
2422.1
When I echo any variable separately to the console I get right values. So variables are parsed fine. What could be wrong?
Thank you.

Comparing two sets of variables line by line in unix, code only prints out the very last line

this is my first stackoverflow question, regarding bash scripting. I am a beginner in this language, so be kind with me.
I am trying to write a comparison script. I tried to store all the outputs into variables, but only the last one is stored.
Example code:
me:1234567
you:2345678
us:3456789
My code:
#!bin/bash
while read -r forName forNumber
do
aName="$forName"
echo "$aName"
aNumber="$forNumber"
echo "$aNumber"
done < "exampleCodeFile.txt"
echo "$aNumber"
For the first time, everything will be printed out fine. However, the second echo will only print out "3456789", but not all the numbers again. Same with $aName. This is a problem because i have another file, which i stored a bunch of numbers to compare $aNumber with, using the same method listed above, called $aMatcher, consisting:
aMatcher:
1234567
2345678
3456789
So if i tried to run a comparison:
if [ "$aNumber" == "$aMatcher" ]; then
echo "match found!"
fi
Expected output (with bash -x "scriptname"):
'['1234567 == 1234567']'
echo "match found!"
Actual output (with bash -x "scriptname"):
'['3456789 == 3456789']'
echo "match found!"
Of course my end product would wish to list out all the matches, but i wish to solve my current issue before attempting anything else. Thanks!
When you run your following code
aNumber="$forNumber"
You are over-writing the variable $aNumber for every line of the file exampleCodeFile.txt rather than appending.
If you really want the values to be appended, change the above line to
aNumber="$aNumber $forNumber"
And while matching with $aMatcher, you again have to use a for/while loop to iterate through every value in $aNumber and $aMatcher.

Why doesn't this bit of code work? Setting variables and config file

I have recently just made this script:
if test -s $HOME/koolaid.txt ; then
Billz=$(grep / $HOME/koolaid.txt)
echo $Billz
else
Billz=$HOME/notkoolaid
echo $Billz
fi
if test -d $Billz ; then
echo "Ok"
else touch $Billz
fi
So basically, if the file $HOME/koolaid.txt file does NOT exist, then Billz will be set as $HOME/koolaid.txt. It then sucesfully creates the file.
However, if I do make the koolaid.txt then I get this
mkdir: cannot create directory : No such file or directory
Any help would be appreciated
Here is a difference between content of a variable and evaluated content...
if your variable contains a string $HOME/some - you need expand it to get /home/login/same
One dangerous method is eval.
bin=$(grep / ~/.rm.cfg)
eval rbin=${bin:-$HOME/deleted}
echo "==$rbin=="
Don't eval unless you're absolutely sure what you evaling...
Here are a couple things to fix:
Start your script with a "shebang," such as:
#!/bin/sh
This way the shell will know that you want to run this as a Bourne shell script.
Also, your conditional at the top of the script doesn't handle the case well in which .rm.cfg exists but doesn't contain a slash character anywhere in it. In that case the rbin variable never gets set.
Finally, try adding the line
ls ~
at the top so you can see how the shell is interpreting the tilde character; that might be the problem.

concancatinating string unable to write into text file

I've a batch file in that I'm passing a command line argument and concatenating this argument with some string as bellow. suppose if i sent 1.0 as command line argument
echo ^<em:version^>%1^</em:version^>
this prints <em:version>1.0</em:version> and works fine.
if i tried to redirect this string to some text file using :
echo ^<em:version^>%1^</em:version^> >> test.txt
it written only <em:version></em:version> into file leaving the command line argument.
i wanted to write whole string with command line.
What would be the problem ? how to fix this ?
It works fine for me. I called it 1.bat. I call it below:
d:\Uploads\fbi>1 1.0
d:\Uploads\fbi>echo <em:version>1.0</em:version> 1>>test.txt
d:\Uploads\fbi>type test.txt
<em:version>1.0</em:version>
Works with or without an #echo off up top.
What code is it in? Show us the code that calls it. Are you SURE that you are passing it a value?
Whatever variable you are passing it, echo that variable then pause it right before you call it like so:
echo %var%
pause
call 1.bat %var%
If all you get is:
ECHO is off.
Press any key to continue . . .
Then the variable is empty.

Resources