Hi,
i want to know how to compare string of file with input string
im trying following code:
file_no=`paste -s -d "||||\n" a.txt | cut -c 1`
#it will return collection number from file
echo "enter number"
read " curr_no"
if [ file_no=curr_no]; then
echo " current number already present"
fi
that's it...
but it was not working...please help to resolve my problem
Thanks in advance.....
#!/bin/bash
#file_no=`paste -s -d "||||\n" a.txt | cut -c 1`
#it will return collection number from file
# maybe you can use like this instead of above cmd
# file_no=`paste -s -d "||\n" a.txt|cut -d'|' -f1`
## but you must use grep for this achieve to me
file_nos=$(grep '^[0-9]*$' a.txt)
## at the now file_nos = "1 2"
## you can use echo now!!
## echo $file_nos is like seem -> 1 2 (with D_FS)
## echo with double quotes for literal values between quotes so we save our newlines
## echo "$file_nos" is like -> 1\n2
echo "enter number"
read "curr_no"
## for exa you entered multiple nrs --> 1 2
## best method is grep for compare any values
## but we must use split to lines for best comparing..
## so our elements are "1 2"
## if compare its for multiple elements with grep
## then we change spaces to newline for grep search
## we can use tr command
## we try to tr command for clearly line comparing
## and you can use $(commands) exprs instead of (``) backticks
new_curr_no=$(echo "$curr_no"|tr ' ' '\n')
## so above command is equal almost! with below cmds
# new_curr_no=`echo "$curr_no"|tr ' ' '\n'`
## and new_curr_no="1\n2"
## new_curr=
#1
#2
# at the now we can assign an array for complete best comparing
new_curr_no_arr=($new_curr_no)
# new_curr_no_arr[0]=1
# new_curr_no_arr[1]=2
# use for loop \for one-to-one the elements of comparison
for nr in ${new_curr_no_arr[@]}
do
## we can use grep with -w for 'word regexp'
if [[ $(echo $nr|grep -w "$file_nos") ]]
## if the grep cmd return to shell with success status so it is 0
## if the grep exit status is 0 then grep is success so means there is in..
then
## then write your message
echo "$nr number already present"
else
## if not
echo "$nr number is NOT present!!"
## the end of fi
fi
## the end of for
done