Reputation: 593
I keep getting this error when i execute the bash script (**./partion.sh: line 11: $'n\np\n1\n\nw\n': command not found **):
./partion.sh: line 11: $'n\np\n1\n\nw\n': command not found
Checking that no-one is using this disk right now ... OK
Disk /dev/sdd: 3 GiB, 3221225472 bytes, 6291456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0ca4ca9d
Old situation:
>>>
New situation:
Here is the script:
#!/bin/bash
hdd="/dev/sdd"
for i in $hdd;do
echo
"n
p
1
w
"|sfdisk $i;done
I am still a newbie so really appreciate all the help i can get=)
Upvotes: 0
Views: 552
Reputation: 95267
You have an extra newline between the command echo
and the string that you are telling the shell to echo, which is what is causing your error message.
But you are also sending interactive commands to sfdisk
, which is not an interactive tool. Your code appears to be based on the top of this article, which @GregTarsa linked in his comment, but that is sending those commands to fdisk, not sfdisk. (You are also missing another newline between the l
and w
fdisk commands.)
The sfdisk program is designed to take a text description of the desired partitioning layout and apply it to a disk. It doesn't expect single-letter commands on input; it expects its input to look like this:
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 2048, size= 497664, Id=83, bootable
/dev/sda2 : start= 501758, size=1953021954, Id= 5
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 501760, size=1953021952, Id=8e
Which is less generic than the fdisk
commands, since it requires actual block numbers, but much easier to use (and for someone reading your script to understand), since you're not trying to remote-control an interactive program by echo
ing commands to it.
If you are going to remote-control an interactive program from the shell, I recommend looking into expect
instead of doing blind echo
s.
Also, in general, if you are trying to print multiline data, echo
is probably not the way to go. With lines this short, I would reach for printf
:
printf '%s\n' n p l "" "" w | fdisk "$i"
But the more generally useful tool is the here-document:
fdisk "$i" <<EOF
n
p
l
w
EOF
Upvotes: 1
Reputation: 2533
I think it should be:
#!/bin/bash
hdd="/dev/sdd"
for i in $hdd;do
echo "n
p
1
w
"|sfdisk $i;done
Multi-line echo should start on the same line. Else, next line is treated as a command on its own.
Upvotes: 1
Reputation: 212268
A newline terminates a command. If you want to pass a multiline argument to echo
, you need to move your quote. For example:
for i in $hdd; do
echo "n
p
1
w
" | sfdisk $i
done
It would probably look better to write:
for i in $hdd; do printf 'n\np\n1\n\nw\n' | sfdisk $i; done
A third option is to use a heredoc:
for i in $hdd; do
sfdisk $i << EOF
n
p
1
w
EOF
done
Upvotes: 0