Solved 'gpart add' in shell script

Is it possible to get the partition number added as a result of gpart add in a shell script?

ie I want something like

gpart add -t freebsd-ufs da0 > 'part-no'
 
quick glance at the man page, I think you'd have to parse any strings on stdout. exit code looks to be simply 0 or 1 based on success/failure.
 
Oops.... error in pasting.....

Should have been

gpart add -t freebsd-ufs -s 1G da0 | cut -d ' ' -f 1

Unless anyone comes up with a better way of doing this, I'll mark this as 'Solved'

I actually wanted to get the partition number into a variable. This is what I came up with. If anyone knows a better way, please feel free to share.

Bash:
export PART=`gpart add -t freebsd-ufs -s 1G da0 | cut -d ' ' -f 1`                                                                                                                             
echo $PART
 
Whenever there are simple white-space separated fields to parse, I just use set and avoid the pipe (processes are expensive; plain /bin/sh code):

sh:
set -- $(gpart add -t freebsd-ufs -s 1G da0)
PART=$1 FROB=$5 FOOBAR="$7 $8 $9"
 
Whenever there are simple white-space separated fields to parse, I just use set and avoid the pipe (processes are expensive):

Bash:
set -- $(gpart add -t freebsd-ufs -s 1G da0)
PART=$1 FROB=$5 FOOBAR="$7 $8 $9"
Is that a bash only or does it work for old fashioned "sh" too?
 
Is that a bash only or does it work for old fashioned "sh" too?
It's plain old Bourne shell. The "code" language selection box does not have "sh", only "bash", which I think is a serious forum SW omission. It uses the set built-in, where set foo bar baz sets $1 to foo, $2 to bar and $3 to baz.
I have edited my original answer and changed bash to sh, which lost both the language tag and syntax coloring. 😖
 
  • Like
Reactions: mer
schweikh Thanks. I thought it was plain old Bourne shell, just wanted to make sure because I've been known to creep bashisms in sometimes which leads to much head banging in sh.
 
Thanks to schweikh I have a means getting the partition number of new partition into a variable...

Code:
set -- $(gpart add -t freebsd-ufs -s 1G da0)
PART=$1

Not sure that I totally understand it though. At least I learnt something new.
 
Thanks to schweikh I have a means getting the partition number of new partition into a variable...

Code:
set -- $(gpart add -t freebsd-ufs -s 1G da0)
PART=$1

Not sure that I totally understand it though. At least I learnt something new.
What's still unclear? $() is the POSIX way for command substitution (backticks don't nest easily). And set is the way to set the shell's positional parameters $1, $2, ... from its arguments.
 
Whenever there are simple white-space separated fields to parse, I just use set and avoid the pipe (processes are expensive; plain /bin/sh code):

sh:
set -- $(gpart add -t freebsd-ufs -s 1G da0)
PART=$1 FROB=$5 FOOBAR="$7 $8 $9"

Doesn't seem to work here:-

Bash:
#!/bin/sh
mount -t ext2fs /dev/ada0p1 /mnt                                                                                                                                               
tar xf /mnt/mfsbsd-se-13.2-RELEASE-amd64.iso -C /media                                                                                                                         
umount /mnt                                                                                                                                                                   
set -- $(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0)                                                                                                                 
PART=$1                                                                                                                                                                       
newfs /dev/$PART                                                                                                                                                               
mount /dev/$PART /mnt                                                                                                                                                         
tar zxf 13.2-RELEASE-amd64/base.txz -C /mnt                                                                                                                                   
tar zxf 13.2-RELEASE-amd64/kernel.txz -C /mnt

My result:-

Code:
+ mount -t ext2fs /dev/ada0p1 /mnt
+ tar xf /mnt/mfsbsd-se-13.2-RELEASE-amd64.iso -C /media
+ umount /mnt
+ gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0
gpart: Invalid argument
+ set --
+ PART=''
+ newfs /dev/
newfs: /dev/: could not open special device
+ mount /dev/ /mnt
mount: /dev: Block device required
+ tar zxf 13.2-RELEASE-amd64/base.txz -C /mnt
tar: Error opening archive: Failed to open '13.2-RELEASE-amd64/base.txz'
+ tar zxf 13.2-RELEASE-amd64/kernel.txz -C /mnt
tar: Error opening archive: Failed to open '13.2-RELEASE-amd64/kernel.txz'
 
What happens when you remove "-l FreeBSD-test"? You do have a da0 device, right? And it has room for another 10G?
The problem was that da0 used an MBR scheme so that gpart add -t freebsd-ufs failed.

It is working OK now that I changed it to GPT.
 
Back
Top