Skip to contentSkip to conversation

prepare-kernel.sh: expand tilde in --outpatch path parameter

PATCH In review Haiyang He 3 participants 8 messages Apr 14, 2025 – Apr 15, 2025

lore
Share & tools
lore

Haiyang He

author
#0 lore raw

PATCH v2 1/1 prepare-kernel.sh: expand tilde in --outpatch path parameter

Ensure shell expands tilde (~) in the --outpatch parameter path. This fixes "cd: can't cd to ~" errors when using home directory shortcuts in output path.

The issue occurred because the ~ character in paths was not expanded before directory operations.

$ ./scripts/prepare-kernel.sh \
--linux=~/xenomai/linux-dovetail \
--arch=arm64 \
--outpatch=~/xenomai.patch
./scripts/prepare-kernel.sh: 1: cd: can't cd to ~

---

Patch v2:
Change eval "echo $output_patch" to $(eval "realpath $output_patch")

 scripts/prepare-kernel.sh | 1 +
 1 file changed, 1 insertion(+)
scripts/prepare-kernel.sh +1 -0
index ecc830fd2..ea7892b21 100755
@@ -139,6 +139,7 @@ while test $# -gt 0; do
 		;;
 	--outpatch=*)
 		output_patch=`echo $1|sed -e 's,^--outpatch=\\(.*\\)$,\\1,g'`
+		output_patch=$(eval "realpath $output_patch")
 		;;
 	--filterkvers=*)
 		echo "$me: warning: --filterkvers= is deprecated and now a no-op" >&2
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · Haiyang He

Ensure shell expands tilde (~) in the --outpatch parameter path. This fixes "cd: can't cd to ~" errors when using home directory shortcuts in output path.

Quoted 8 lines

The issue occurred because the ~ character in paths was not expanded before directory operations.

$ ./scripts/prepare-kernel.sh \
--linux=~/xenomai/linux-dovetail \
--arch=arm64 \
--outpatch=~/xenomai.patch
./scripts/prepare-kernel.sh: 1: cd: can't cd to ~

Where is this "cd" command that fails? Which line of the script?

Quoted 15 lines

---

Patch v2:
Change eval "echo $output_patch" to $(eval "realpath $output_patch")

 scripts/prepare-kernel.sh | 1 +
 1 file changed, 1 insertion(+)
scripts/prepare-kernel.sh +0 -0
index ecc830fd2..ea7892b21 100755

scripts/prepare-kernel.sh while test $# -gt 0; do · line 142

Quoted 4 lines
scripts/prepare-kernel.sh +1 -0
@@ -139,6 +139,7 @@ while test $# -gt 0; do
 		;;
 	--outpatch=*)
 		output_patch=`echo $1|sed -e 's,^--outpatch=\\(.*\\)$,\\1,g'`
+		output_patch=$(eval "realpath $output_patch")

I think you got me wrong:
Should read: output_patch=$(realpath "${output_patch}")

But: Where exactly are we failing? Just tried to reproduce but failed so far... Would help to know where exactly the "cd" operation is that fails.

We operate on output_patch with basename, dirname, ... which all should support ~.

Maybe it helps the script with "bash -x ./scripts/prepare-kernel.sh" to enable some debugging.

Quoted 5 lines
scripts/prepare-kernel.sh +0 -0
 		;;
 	--filterkvers=*)
 		echo "$me: warning: --filterkvers= is deprecated and now a no-op" >&2
Signature · 1 line
2.34.1
#2 lore raw
Quoted 43 lines · Florian Bezdeka
Quoted 4 lines · Haiyang He

Ensure shell expands tilde (~) in the --outpatch parameter path. This fixes "cd: can't cd to ~" errors when using home directory shortcuts in output path.

Quoted 8 lines

The issue occurred because the ~ character in paths was not expanded before directory operations.

$ ./scripts/prepare-kernel.sh \
--linux=~/xenomai/linux-dovetail \
--arch=arm64 \
--outpatch=~/xenomai.patch
./scripts/prepare-kernel.sh: 1: cd: can't cd to ~

Where is this "cd" command that fails? Which line of the script?

Quoted 15 lines

---

Patch v2:
Change eval "echo $output_patch" to $(eval "realpath $output_patch")

 scripts/prepare-kernel.sh | 1 +
 1 file changed, 1 insertion(+)
scripts/prepare-kernel.sh +0 -0
index ecc830fd2..ea7892b21 100755

scripts/prepare-kernel.sh while test $# -gt 0; do · line 142

Quoted 4 lines
scripts/prepare-kernel.sh +1 -0
@@ -139,6 +139,7 @@ while test $# -gt 0; do
 		;;
 	--outpatch=*)
 		output_patch=`echo $1|sed -e 's,^--outpatch=\\(.*\\)$,\\1,g'`
+		output_patch=$(eval "realpath $output_patch")

I think you got me wrong:
Should read: output_patch=$(realpath "${output_patch}")

But: Where exactly are we failing? Just tried to reproduce but failed so far... Would help to know where exactly the "cd" operation is that fails.

Quoted 5 lines

We operate on output_patch with basename, dirname, ... which all should support ~.

Maybe it helps the script with "bash -x ./scripts/prepare-kernel.sh" to enable some debugging.

Ha, seems I was missing that "~" is a bash feature... So called tilde expansion. See [1].

My main point is that we should not use eval on user input. That might bring us to security hell.

Maybe you can try the following:

scripts/prepare-kernel.sh +1 -0
index ecc830fd2..308627ae2 100755
@@ -139,6 +139,7 @@ while test $# -gt 0; do
 		;;
 	--outpatch=*)
 		output_patch=`echo $1|sed -e 's,^--outpatch=\\(.*\\)$,\\1,g'`
+		output_patch="${output_patch/#\~/${HOME}}"
 		;;
 	--filterkvers=*)
 		echo "$me: warning: --filterkvers= is deprecated and now a no-op" >&2

That's basically a hand-made parameter expansion. Replacing "~" at the beginning (#) with HOME.

[1] https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html

Quoted 7 lines
Quoted 6 lines
scripts/prepare-kernel.sh +0 -0
 		;;
 	--filterkvers=*)
 		echo "$me: warning: --filterkvers= is deprecated and now a no-op" >&2
Signature · 1 line
2.34.1
#3 lore raw
Quoted 2 lines · Florian Bezdeka

My main point is that we should not use eval on user input. That might bring us to security hell.

We are already in this hell.
Both --linux and --dovetail use eval.
AFAIK, this is the only way to fully support tilde expansion.

#4 lore raw
Quoted 8 lines · Richard Weinberger
Quoted 2 lines · Florian Bezdeka

My main point is that we should not use eval on user input. That might bring us to security hell.

We are already in this hell.
Both --linux and --dovetail use eval.
AFAIK, this is the only way to fully support tilde expansion.

Ouch... With that said, I think v2 can be merged.

Just learned that my proposed string replacement is undefined in POSIX sh, (see shebang/first line).

There is obviously some room for improvements in this script...

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
#5 lore raw
Quoted 5 lines · Florian Bezdeka
Quoted 3 lines

We are already in this hell.
Both --linux and --dovetail use eval.
AFAIK, this is the only way to fully support tilde expansion.

Ouch... With that said, I think v2 can be merged.

I'm not sure. v1 does exactly what we do already for --linux and --dovetail. No realpath needed.
We should provide the very same semantics for --linux, --dovetail and --outpatch.

Signature · 2 lines
Thanks,
//richard
#6 lore raw
Quoted 6 lines · Richard Weinberger
Quoted 4 lines · Florian Bezdeka
Quoted 3 lines

We are already in this hell.
Both --linux and --dovetail use eval.
AFAIK, this is the only way to fully support tilde expansion.

Quoted 6 lines
Quoted 1 line

Ouch... With that said, I think v2 can be merged.

I'm not sure. v1 does exactly what we do already for --linux and --dovetail. No realpath needed.
We should provide the very same semantics for --linux, --dovetail and --outpatch.

Yes, v1 is more consistent in that regard.

Quoted 4 lines
Signature · 3 lines
-- 
Thanks,
//richard
author
#7 lore raw
In reply to Florian Bezdeka
Quoted 8 lines · Florian Bezdeka
Quoted 7 lines · Haiyang He

Ensure shell expands tilde (~) in the --outpatch parameter path. This fixes "cd: can't cd to ~" errors when using home directory shortcuts in output path.

The issue occurred because the ~ character in paths was not expanded before directory operations.

Quoted 7 lines
Quoted 5 lines
$ ./scripts/prepare-kernel.sh \
--linux=~/xenomai/linux-dovetail \
--arch=arm64 \
--outpatch=~/xenomai.patch
./scripts/prepare-kernel.sh: 1: cd: can't cd to ~

Where is this "cd" command that fails? Which line of the script?

It fails on line 210 of scripts/prepare-kernel.sh
209 patchdir=dirname $output_patch
210 patchdir=cd $patchdir && pwd

Quoted 11 lines
Quoted 11 lines

---

Patch v2:
Change eval "echo $output_patch" to $(eval "realpath $output_patch")

 scripts/prepare-kernel.sh | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

scripts/prepare-kernel.sh while test $# -gt 0; do · line 142

Quoted 9 lines
scripts/prepare-kernel.sh +1 -0
index ecc830fd2..ea7892b21 100755
@@ -139,6 +139,7 @@ while test $# -gt 0; do
 		;;
 	--outpatch=*)
 		output_patch=`echo $1|sed -e 's,^--outpatch=\\(.*\\)$,\\1,g'`
+		output_patch=$(eval "realpath $output_patch")

I think you got me wrong:
Should read: output_patch=$(realpath "${output_patch}")

I had tried but it did not work and got error msg:
realpath: '~/xenomai.patch': No such file or directory

Quoted 10 lines

But: Where exactly are we failing? Just tried to reproduce but failed so far... Would help to know where exactly the "cd" operation is that fails.

We operate on output_patch with basename, dirname, ... which all should support ~.

Maybe it helps the script with "bash -x ./scripts/prepare-kernel.sh" to enable some debugging.

Here are outputs of line 209~210 when do "bash -x ./scripts/prepare-kernel.sh"
+ dirname ~/xenomai.patch
+ patchdir=~
+ cd ~
./scripts/prepare-kernel.sh: 1: cd: can't cd to ~

Quoted 6 lines
Quoted 6 lines
scripts/prepare-kernel.sh +0 -0
 		;;
 	--filterkvers=*)
 		echo "$me: warning: --filterkvers= is deprecated and now a no-op" >&2
Signature · 1 line
2.34.1