0

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.