6

I'm just starting to learn bash. I have the following script, and I would like to know what does the following commands do in order to start to understand a little bit this world.

if [ ! -w "." ]
then
echo "You need write permission in the directory"
exit 1
fi

firefoxGeditOpen=ps -d | grep -ic -e firefox -e gedit
if [[firefoxGeditOpen>0]]
then
echo "Firefox and Gedit must be closed to let the script work"
exit 1
fi

while(true)
do
firefox &
firefoxPid=$!
gedit &
geditPid=$!
echo "Firefox PID $firefoxPid Gedit PID $geditPid">>result.txt
wait
echo "You have closed all the processes. They will be re-opened">>result.txt
done

Thanks in advance!

3
  • 2
    This might help you: help.ubuntu.com/community/Beginners/BashScripting Apr 17, 2015 at 0:07
  • Now that you've edited in the full script would you like me to expand my answer to include the whole thing?
    – Seth
    Apr 17, 2015 at 0:37
  • 1
    [[ and ]] should be separated from what's between them (and also from what's outside of them) by whitespace.
    – Tom
    Apr 17, 2015 at 1:29

1 Answer 1

7

As I mentioned in my comment there are some vital pieces missing here and these commands together don't quite make sense, but I will go over them.

  1. if [ ! -w "." ]: . represents the current directory. -w tests if it is writeable. ! negates the test (so the statement returns true if the directory is not writeable vs the other way around).

  2. ps -d | grep -ic -e firefox -e gedit: ps -d prints all processes minus session starters. grep searches the piped output from ps for patterns (which we'll get to in a minute). -i makes the search case insensitive. -c outputs a count of the matches instead of the actual matches. -e takes the search expressions. In this case firefox and gedit. So if neither FF nor gedit is running the command will output 0. If one of them is running it will output 1. If both are running it will output 2.

  3. firefox &: Start firefox. & forces the process into the background.

  4. firefoxPid=$!: In a bash shell script $! holds the job number of the last background command. firefoxPid is a variable. So what's happening here is the job number of the last background command (which happens to be firefox's) is stored to a variable named firefoxPid.

6
  • Hey Seth, many thanks for your response, it was really clear!! One more question. What does the WAIT statement in this case? And what about it I use a PID as a parameter for WAIT command? Many thanks again, I'm just starting and feel a bit lost yet. Apr 17, 2015 at 0:40
  • @LautaroAlvarez wait as used in the script above will pause the script's execution until all the background processes have terminated. If you pass it a process ID it will pause script execution until the process in question has terminated.
    – Seth
    Apr 17, 2015 at 0:55
  • 1
    @LautaroAlvarez For more information on the wait command, run help wait in your shell. (help doesn't give information on all commands, just commands that are built into the shell. For external commands man usually provides handy reference information.) Apr 17, 2015 at 0:56
  • 1
    @Seth Thanks for the edit. Btw, the way I noticed this was that I was trying to figure out what the intent of that line was, in the script. As written, I don't think it actually works. The pipeline with ps and grep works fine, but it won't be run when written like that on the right-hand side of a variable assignment. At first I wondered if the goal was to assign its return value, but since grep was run with -c I realized the output is what's probably meant to be used. (Thus the right-hand side of the assignment should be surrounded in $( ) or ` `.) Apr 17, 2015 at 1:17
  • @EliahKagan You are correct, as it is currently written that bit will not work. The original version contained only the ps command and pipeline hence the rest not being in my answer.
    – Seth
    Apr 17, 2015 at 1:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .