String comparison using bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String comparison using bash
# 1  
Old 05-11-2010
String comparison using bash

I have a program to create a directory if not present. Here is the program.

FYI: Directory name format: YYYY_MM_DD

Code:
#!/bin/bash
date=`date +%Y_%m_%d`
presence=$(ls -lrt /TS_File/ | grep "$date" | awk '{print $9}')
if [ $date != $presence ]
then
        mkdir /TS_File/$date
else
        echo "Unable to Create a Directory"
fi

Output:
Code:
./date.sh: line 4: [: 2010_05_11: unary operator expected
Unable to Create a Directory

Please help me to recover from this error and need a solve my purpose.


Moderator's Comments:
Mod Comment Please use code tags

Last edited by Franklin52; 05-11-2010 at 01:21 PM..
# 2  
Old 05-11-2010
Try this...

Code:
#!/bin/bash
today=`date +%Y_%m_%d`
if [ ! -d /TS_File/${today} ]; then
    mkdir /TS_File/${today}
fi


Last edited by dunkar70; 05-11-2010 at 12:16 PM.. Reason: Corrected typo
# 3  
Old 05-11-2010
Code:
#!/bin/bash
fname=$(date +%Y_%m_%d)
[[ -d /TS_file/${fname} ]] || mkdir /TS_file/${fname}

You cannot use the word date for a variable name. It is a command.
# 4  
Old 05-11-2010
Why not? This isn't VB, where 'magic' variable names may mysteriously reset your system clock...

Code:
$ date=$(date)
$ echo ${date}
Tue May 11 09:45:49 CST 2010

...not that it's a good idea. Best to avoid confusion and not have multiple meanings for names.
# 5  
Old 05-11-2010
Thanks Dunkar70. Your code worked for me.
# 6  
Old 05-12-2010
Quote:
Originally Posted by jim mcnamara
Code:
#!/bin/bash
fname=$(date +%Y_%m_%d)
[[ -d /TS_file/${fname} ]] || mkdir /TS_file/${fname}

You cannot use the word date for a variable name. It is a command.

Of course you can. You can even use shell keywords and builtin commands:
Code:
if=1
do=42
cd=whatever

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

2. Shell Programming and Scripting

Bash not equal string comparison

Why does this cause an infinite loop? #!/bin/bash while ]; do echo "$(ipcs | awk '{print $2}')" done echo exit 0 I have verified I eventually get Semaphore so it should break out of the while loop. $ echo $(ipcs | awk '{print $2}') Shared shmid 262145 294914... (6 Replies)
Discussion started by: cokedude
6 Replies

3. Shell Programming and Scripting

Comparison of two file using bash scripting

I need following code in linux bash: Step 1: run a command and create file1 run a command and create file2 compare file1 and 2 if any difference go to step 1 (keep trying 4 times, if no success, abort program) else do nothing and continue program. now I'm... (2 Replies)
Discussion started by: kashif.live
2 Replies

4. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Help with string comparison

#!/bin/sh PRINTF=/usr/bin/printf MACHINE_NAME=`uname -n` TIME=`date +"%H"` $PRINTF "Welcome to $MACHINE_NAME. What is your name?\n" read NAME if ; then $PRINTF "Good morning $NAME, how are you?\n" elif ; then $PRINTF "Good afternoon $NAME, how are you?\n" else $PRINTF "Good... (2 Replies)
Discussion started by: ikeQ
2 Replies

7. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

8. Shell Programming and Scripting

date comparison in bash

Hi I have this simple script: #!/bin/bash date1=2009:07:15:12:36 date2=2009:07:15:12:16 echo $date1 echo $date2 datediff= #datediff=date1-date2 echo datediff is$datediff How do i return the difference in seconds? (6 Replies)
Discussion started by: carp.dk
6 Replies

9. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies

10. Shell Programming and Scripting

string comparison

The script will read a bunch of names, and test if it contains "John", but as below apparently ~ does not work, so what is the easiest way to perform string comparison in bash shell script? thanks ... elif then echo "get John" .... (2 Replies)
Discussion started by: fedora
2 Replies
Login or Register to Ask a Question