Bash Script compare strings and variable in an if statement - string

I am trying to get a variable to compare to a string, and then do something if it does and go on to the next, but when i look at it in debug mode, the variable just shows up as '' with nothing in it.
#!/bin/bash
echo -e "Enter the name of the document you wish to edit:\c"
read dname
CTYPE= file "$dname" | cut -d\ -f2
echo $CTYPE
VAR="ASCII"
VAR2="cannot"
if [ "$CTYPE" == "$VAR" ]
then
vi $dname
fi
I get this result:
+ VAR=ASCII
+ VAR2=cannot
+ '[' '' == ASCII ']'
Where the '' is empty even though I echod it and see that it is not empty.
I have tried it like these other ways as well, and get the same or similar non working result:
CTYPE= file "$dname" | cut -d\ -f2
if [ "$CTYPE" == "$VAR" ]
ctype= file "$dname" | cut -d\ -f2
if [ $ctype = "ASCII" ]
ctype= file "$dname" | cut -d\ -f2
if [ "$ctype" = "ASCII" ]
ctype= file "$dname" | cut -d\ -f2
if [ "$ctype" == "ASCII" ]
Not sure what I am missing, I've read so many posts I don't know where to go from here. Thank you!

You have an error with CTYPE:
CTYPE=$(file "$dname" | cut -d\ -f2)
You cannot have any spaces between the = and the assignment. Further, you want the return from file "$dname" | cut -d\ -f2 so you will have to enclose it in $() or with backticks.

When you run this command:
d= date
bash does this: set an environment variable d (with the value the empty string) only for the duration of the date command. This is densely documented here.
As David tells you, to capture the outut of a command, you need
d=$(date)

Related

linux bash saved variable is empty

I'm new to shell. I try to get a string in file and use it to make a shortcut link.
tmp/tempfile.tmp
/mnt/sda5
in my test.sh file:
#!/bin/sh
#working just fine!
echo $(cat /tmp/tempfile.tmp | cut -d' ' -f2)
#empty result
USBdev = $(cat /tmp/tempfile.tmp | cut -d' ' -f2)
echo ${USBdev}/
# --making a link
ln -s ${USBdev} $(pwd)/C:
exit 0
why it is empty??
echo ${USBdev} --result is empty!
I'm using this on my openwrt router.
I tried as follow, still empty
echo "$USBdev"
echo "${USBdev%.*}"
SOLUTION:
REMOVE "SPACES" AROUND THE "=", thanks to CDroescher
change
USBdev = $(...)
to (remove spaces)
USBdev=$(...)
You have to add a shebang at the first line https://en.wikipedia.org/wiki/Shebang_(Unix) and make sure that there are no spaces after and before "="
#!/usr/bin/env bash
#working just fine!
echo $(cat /tmp/tempfile.tmp | cut -d' ' -f2)
#empty result
USBdev=$(cat /tmp/tempfile.tmp | cut -d' ' -f2)
echo ${USBdev}/
# --making a link
ln -s ${USBdev} $(pwd)/C:

Unix Script loop through individual variables in a list and execute code

I have been busting my head all day long without coming up with a sucessfull solution.
Setup:
We have Linux RHEL 8.3 and a file, script.sh
There is an enviroment variable set by an application with a dynamic string in it.
export PROGARM_VAR="abc10,def20,ghi30"
The delimiter is always "," and the values inside vary from 1 to 20.
Inside the script I have defined 20 variables which take the values
using "cut" command I take each value and assign it to a variable
var1=$(echo $PROGARM_VAR | cut -f1 -d,)
var2=$(echo $PROGARM_VAR | cut -f2 -d,)
var3=$(echo $PROGARM_VAR | cut -f3 -d,)
var4=$(echo $PROGARM_VAR | cut -f4 -d,)
etc
In our case we will have:
var1="abc10" var2="def20" var3="ghi30" and var4="" which is empty
The loop must take each variable, test if its not empty and execute 10 pages of code using the tested variable. When it reaches an empty variable it should break.
Could you give me a hand please?
Thank you
Just split it with a comma. There are endless possibilities. You could:
10_pages_of_code() { echo "$1"; }
IFS=, read -a -r vars <<<"abc10,def20,ghi30"
for i in "${vars[#]}"; do 10_pages_of_code "$i"; done
or:
printf "%s" "abc10,def20,ghi30" | xargs -n1 -d, bash -c 'echo 10_pages_of_code "$1"' _
A safer code could use readarray instead of read to properly handle newlines in values, but I doubt that matters for you:
IFS= readarray -d , -t vars < <(printf "%s" "abc10,def20,ghi30")
You could also read in a stream up:
while IFS= read -r -d, var || [[ -n "$var" ]]; do
10_pages_of_code "$var"
done < <(printf "%s" "abc10,def20,ghi30")
But still you could do it with cut... just actually write a loop and use an iterator.
i=0
while var=$(printf "%s\n" "$PROGARM_VAR" | cut -f"$i" -d,) && [[ -n "$var" ]]; do
10_pages_of_code "$var"
((i++))
done
or
echo "$PROGRAM_VAR" | tr , \\n | while read var; do
: something with $var
done

How would I use the output of a set of instructions to make a new variable?

So basically I have a variable, lets consider this variable v
v=overflow_12
Now I am doing a cut -d delimiting command at the _ to split overflow and 12 of which I have done:
echo $v | cut -d '_' -f 2
which gives me the answer 12. However how would I store that 12 in a variable and eventually increment it by 1 to make 13?
I tried:
i= echo $v | cut -d '_' -f 2
which should store 12 into $i? However when I then:
echo $i
it is just a blank answer? I have to eventually increment this variable by 1 but I don't know why 12 is not being stored in $i?
Use $(command) to capture the output of a command as a string.
i=$(echo $v | cut -d '_' -f 2)
Alternatively, you can strip off the prefix directly in bash.
i=${v#*_}
# removes a string prefix and *_ matches anything followed by an underscore.
You're just using the wrong syntax for variable assignment. You can't use | to pipe into a variable, and you can't have a space after the =. This works:
v=overflow_12
i=$(echo $v | cut -d '_' -f 2)
echo $i
Use i=$(command) to set i to result of command.
Do
i=$(echo $v | cut -d '_' -f 2)
which will work for most bash-versions, except very old ones. For ancient bash versions do
i=`echo $v | cut -d '_' -f 2`
I would suggest to use the "here string" syntax <<< instead of echo
i=$(cut -d '_' -f 2 <<< $v)

Reading and comparing column in .txt file in bash

I got .txt file which its content is
5742060626,Ms.Pimpan Tantivaravong,Female
5742065826,Ms.Kaotip Tanti,Female
-
I create an interface script to add list in this file
First, I have to compare the input id with the exitsting id in a list.
I use cut command to read only 1st column of .txt file.
But,I got a problem when I am trying to compare it.
Here is my code.
-
!/bin/bash
#
datafile='student-2603385.txt'
while read p;
do
if [ "$id" == (echo $p | cut -d, -f1) ]
then
echo 'duplicate id'
fi
done <$datafile
-
could anyone suggest me, how should I do?
Thank you
Your script has numerous quoting bugs, always quote variable expansion when the variable contains a file name, it is also expected when you want to avoid word splitting and pathname expansion by shell.
Letting that aside, in if [ "$id" == (echo $p | cut -d, -f1) ]:
You need command substitution, $() around echo ... | cut ..., not a subshell ()
you also need quotes around $() to prevent word splitting (and pathname expansion)
== is bash-ism, not defined by POSIX, just a reminder
try to use [[ as much as possible, being a shell keyword [[ handles word splitting
So with test ([):
if [ "$id" == "$(echo "$p" | cut -d, -f1)" ]
better:
if [[ $id == $(echo "$p" | cut -d, -f1) ]]

Shell program doesn't work

For multiple folders provided as input from the user, I'd like to count how many of the files and folders they contain have different permission settings as the container folder itself.
I've written the following shell code. Why does it display the rights, but not count anything?
#!/bin/sh
if [ ! -d $1 ]
then echo $1 nu este director
exit1
fi
ls -R $1 >temp
permission= ls -al $1 | cut -d" " -f1
for i in `cat temp`
do
perm= ls -l $i | cut -d" " -f1
if [ $permission -ne $perm ]
then n=`expr $n + 1`
fi
echo $n
done
You shouldn't use -ne for string comparisons. You need to do this:
if [ "$permission" != "$perm" ]
then
n=`expr $n + 1`
fi
You need to initialise n before you can increment it.
n=0
You need to fix your command substitution:
permission=$(ls -al $1 | cut -d" " -f1)
perm=$(ls -l $i | cut -d" " -f1)
exit1 should be exit 1
you want to use command substitution:
permission=$(ls -al $1 | cut -d" " -f1)
# ...
perm=$(ls -l $i | cut -d" " -f1)
You are not initializing your variable $n, so your call to expr expands to expr + 1 which is a syntax error. You should see lots of "expr: syntax error" messages on stderr. Just add the line n=0 before your loop and you should be fine.
Adding to other's answers:
exit1 should be exit 1

Resources