简体   繁体   English

比较字符串的问题 (BASH)

[英]Problems comparing strings (BASH)

I'm trying to compare 2 strings.我正在尝试比较 2 个字符串。 One comes from a file through the grep command and the other one never changes because I'm always comparing it with the ones I create while reading file texts.一个来自通过 grep 命令的文件,另一个永远不会改变,因为我总是将它与我​​在阅读文件文本时创建的文件进行比较。 If they are equal, the program should print the data associated with the content that the new string contains.如果它们相等,程序应该打印与新字符串包含的内容相关联的数据。 I've tried with all the syntax that bash allows (cause I'm new at bash) but it is just not working like I expected.我已经尝试了 bash 允许的所有语法(因为我是 bash 新手),但它并没有像我预期的那样工作。 It looks like the second if doesn't work, because I tried earlier without that and only print the strings ( echo $text ) and it worked, but not the proper way as the exercise I'm doing asks for.看起来第二个if不起作用,因为我之前尝试过没有它并且只打印字符串( echo $text )并且它起作用了,但不是我正在做的练习要求的正确方法。 I have to show in the console only the pid and state of the processes that are running.我必须在控制台中只显示正在运行的进程的 pid 和状态。

cd
cd /proc
run="State: S (sleeping)"
for i in $( ls -d */); 
do
    cd $i
    if [ -f /proc/$i/status ];
        then    text="`grep -w "S" status`"
             if [ "$text" == "$run" ]
                then grep -w "Pid" status
                     grep -w "State" status
             fi
             cd /proc
        else cd /proc
    fi
done
;;

Your run variable contains State:<space>S (sleeping) , but /proc/<pid>/status files contain State:<tab>S (sleeping) (at least, on my system).您的run变量包含State:<space>S (sleeping) ,但/proc/<pid>/status文件包含State:<tab>S (sleeping) (至少,在我的系统上)。 So you should replace that space character with tab character.所以你应该用制表符替换那个空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM