简体   繁体   中英

Problems comparing strings (BASH)

I'm trying to compare 2 strings. 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. 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. 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. I have to show in the console only the pid and state of the processes that are running.

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). So you should replace that space character with tab character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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