1

In diff command am getting following error. Kindly assist how can I specify I want to see difference in two files:

#current_unavail=ranjith

root@iitmserver1 tmp]# cat /tmp/ran
ranjith
[root@iitmserver1 tmp]#

#test=$(cat /tmp/ran)

[root@iitmserver1 tmp]# diff `$current_unavail` `$test`
diff: missing operand after `diff'
diff: Try `diff --help' for more information.
[root@iitmserver1 tmp]#
9
  • 4
    Quote your variables. $current_unavail would appear to have spaces in the filename (or isn't the name you expect it to be). Commented Apr 14, 2015 at 17:11
  • [root@iitmserver1 unavail_cn]# cat /tmp/unavail_cn.out iitmc07n24-ib0 unavail [root@iitmserver1 unavail_cn]# current_unavail=$(cat /tmp/unavail_cn.out) Commented Apr 14, 2015 at 17:14
  • 1
    $current_unavail is the contents of the current file? That's not going to work. Also it seems it does have a space there (update the post instead of putting things in comments so you can format them). Which is why you need to quote your variable but that isn't enough here because diff takes files as arguments. Commented Apr 14, 2015 at 17:19
  • Hi Etan, kindly have a look on my full code.. this we are using for sending mail, when node down. Commented Apr 14, 2015 at 17:35
  • 1
    Right $last_unavail is a filename. You pass that file to diff. $current_unavail is a string (of two "words"). You also pass that to diff (unquoted). The fact that $current_unavail is unquoted and more than one word is why you get the extra operands error. diff is seeing three arguments iitmc07n24-ib0, unavail, and the $last_unavail file. That's the first problem. Quote "$current_unavail" and that will go away. The second problem, however, is that you are trying to pass the files contents to diff directly but diff takes filenames. Use /tmp/unavail_cn.out. Commented Apr 14, 2015 at 17:39

1 Answer 1

2

diff takes two filenames as arguments, where you appear to be passing in file contents as the first argument. You will want to change your script/commands to look more like:

current_unavail=/tmp/unavail_cn.out
result=$(diff $current_unavail /moes/home/pharthiphan/scripts/monitoring/unavail_cn/$last_unavail)

Alternatively, you can use Process Substitution to pass the output of a command into another command that is expecting a file. eg:

diff <(echo -e "foo\nbar") <(echo -e "foo\nbaz")

However, while good to know about, this would seem to be a needless level of complexity for your current problem.

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

1 Comment

[root@iitmserver1 ~]# result=$(diff <(echo -e $current_unavail) <(echo -e /moes/home/pharthiphan/scripts/monitoring/unavail_cn/$last_unavail)) [root@iitmserver1 ~]# echo $result 1c1 < --- > /moes/home/pharthiphan/scripts/monitoring/unavail_cn/nodes_20150415_101602 [root@iitmserver1 ~]# depends this o/p we follows # if [ $? -eq 0 ]; always produce 1 o/p.. means difference is there. kindly look into our scripts in above. kindly assist.

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.