Simple script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple script problem
# 1  
Old 08-01-2008
Simple script problem

Hi everyone - I am sure this is a really simple problem but I'm a total noob at Linux scripting:

I wanted to create a script that allows me to compare the current week number to the contents of a text file in my home directory:

Code:
VAR1='date +%V'
  VAR2='cat /home/fred/file.txt'


  if $VAR1 = $VAR2
  then
  echo “equal”
  else
  echo “not equal”
  fi

Sadly its not working and I don't know why, can someone help me out Smilie

Many thanks

FR
# 2  
Old 08-01-2008
You have to put your comparison in [].
like this,
Quote:
if [ $VAR1 = $VAR2 ]
# 3  
Old 08-01-2008
Also confirm that while assigning the variable, it should be between tilds(``)..
# 4  
Old 08-01-2008
Also it's usually a good idea to add double quotes around the values; you will get very confusing error messages if either of the variables would turn out to contain an empty string (or a vast array of other possible problematic values).

Code:
if [ "$VAR1" = "$VAR2" ]

In real-world scripts, you frequently see a leading X added to the values, to guard against some of the remaining problematic scenarios (if $VAR1 has a value containing a dash as the first character, for example).

Code:
if [ X"$VAR1" = X"$VAR2" ]

For these reasons, I personally tend to recommend case over if for string comparisons.
# 5  
Old 08-01-2008
To retrieve the week number of the file you can do something like:

Code:
VAR1='date +%V'

# get the week number of the file:

file_date=`stat -c "%y" file | cut -d " " -f1`
VAR2=`date +%V -d $file_date`

if [ "$VAR1" = "$VAR2" ]; then
....
else
....
fi

Regards
# 6  
Old 08-01-2008
You should still use backticks for VAR1, too (ASCII 96, not regular apostrophes).

I was under the impression that the OP really did want to compare against the contents of the file, not the date on which the file was actually changed.
# 7  
Old 08-01-2008
Quote:
Originally Posted by era
You should still use backticks for VAR1, too (ASCII 96, not regular apostrophes).
Right, I just copied the line of the OP, not realising that he used single quotes.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - sftp simple script problem

Hello, when running the scripts below I am not getting message bb2. Can you please help? #!/bin/bash TLOG=/tmp/bb/amatest.log FTPRESULTS=/tmp/bb/amlist export TLOG FTPRESULTS >$TLOG >$FTPRESULTS echo bb1 sftp -oPort=2222 XXXXXXXXXXXXX@sftp.userssedi.com <<EOF cd... (5 Replies)
Discussion started by: biljana
5 Replies

2. Shell Programming and Scripting

Shell Script (simple problem)

I want to find and replace string from files present in one directory. user will input the string to be searched and to replace . Here is my program but Not working echo "Enter Old domain name:" read old echo "Enter New domain name:" read new grep -rl '$old' /var/www/ | xargs sed -i... (4 Replies)
Discussion started by: sunny2802
4 Replies

3. Shell Programming and Scripting

[newb] simple script, big problem

Warning! I'm ridiculously new at all this, so pardon my ignorance... I have a very simple script which is intended to search a hosts file when given a partial hostanme or ip address. The if the partial hostname/ip given is unique, the script automatically logs the user in to that host. If... (6 Replies)
Discussion started by: itomb
6 Replies

4. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

5. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

6. Shell Programming and Scripting

problem writing a simple c shell script

#!/bin/csh echo hello world this is what i got in a text file called ss1. i type "chmod 755 ss1.txt" to make it executable. then when i type ss1 or ss1.txt it says "ss1 command not found" what am i doing wrong? (19 Replies)
Discussion started by: pantelis
19 Replies

7. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

8. Shell Programming and Scripting

one simple shell script problem

Hi everyone, I am facing to one shell script problem, which is as following Write a shell script that: Takes a number of arguments. For each argument, print out all files in the current directory that contain this substring in their name. I know I need to use grep for the second... (7 Replies)
Discussion started by: shaloovia
7 Replies

9. UNIX for Dummies Questions & Answers

simple shell script problem

hi all. i have a little problem. im basically reading input from the user from the keyboard into the variable "phonenumber". I want to do a little error check to check if the user doesnt enter anything in for the value phonenumber. i had this: read phonenumber if then ..... else ........ (2 Replies)
Discussion started by: djt0506
2 Replies

10. Shell Programming and Scripting

Simple ksh script problem

This is a search script that I have developed. It finds the records that I look for. However the only thing I want the program to do now is to display a message such as 'Not Found' to appear when a record is not found. So far when a search doesn't display a record, the screen is blank. ... (14 Replies)
Discussion started by: Warrior232
14 Replies
Login or Register to Ask a Question