Score:0

Bash script with different CWD from parent shell?

si flag

I'm doing a puzzle for my computer science class where I need to execute the binary /challenge/executable via a shell script, from the CWD of /tmp/exampledir, with the shell's CWD being a different directory from the shell script's, or as the challenge phrases it:

  • the challenge checks for a specific parent process : shellscript
  • the challenge will check that it is running in a specific current working directory : /tmp/exampledir
  • the challenge will check to make sure that the parent's parent CWD to be different than the challenge's CWD

My interpretation of what I'm supposed to do was run a shell script, stored in /tmp/exampledir, from a different directory, but the cwd of the bash script changed to where I ran it from. I tried putting cd /tmp/exampledir into the bash script and running it from another directory, but when I ran it, it said that both the shell script and the parent process were in the CWD of /tmp/exampledir. Is there something I'm missing here? Thanks!

waltinator avatar
it flag
Do `(cd /tmp/exampledir;/challenge/executable)` instead? Inspect `/challenge/executable` with `file`, then with `less` or `readelf`. This last may be Forbidden, Against The Rules, a no-no.
SpaghettiMan avatar
si flag
Tried subshells already, still says parent CWD is `/tmp/exampledir`
Score:2
us flag

Since the current working directory is inherited from the parent process, you need something that will change the directory and then replace itself with the desired command. For that, this should work:

sh -c 'cd /tmp/exampledir; exec /challenge/executable'

The key part being exec, which replaces the shell process with the specified command.

For example, this script should behave like the challenge executable:

#! /bin/bash

printf "My PWD is %q. The PWD of my parent process is %q.\n" "$PWD" "$(readlink /proc/$PPID/cwd)"

And then:

# ./foo.sh
My PWD is /home/muru. The PWD of my parent process is /home/muru.
# sh -c 'cd /tmp; exec /home/muru/foo.sh'
My PWD is /tmp. The PWD of my parent process is /home/muru.
# mkdir -p "foo bar"
# cd "foo bar"
# sh -c 'cd /tmp; exec /home/muru/foo.sh'
My PWD is /tmp. The PWD of my parent process is /home/muru/foo\ bar.
Raffa avatar
jp flag
Probably automating that by adding to the top of the script e.g. bash specific `cd -- "${0%/*}"` or maybe a more portable `cd -- "$(dirname "$(realpath -- "$0")")"`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.