• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2898
  • Last Modified:

Expect while loop

Hi,

I am trying to run fsck /dev/sdb and it would take a longtime to finish with the number of inodes.

I have tried the fsck -a/-p option, but there is unexpected inode table.

Now, i have to keep pressing on the letter y for hours as i can't use the fsck -y option as the first question fsck asks is to abort.

i want my expect script to be like this


#!/usr/bin/expect -f

spawn fsck /dev/sdb1
expect "Abort?"
send "n\r"
expect "y?"

here, i want to put a while loop like this

while fsck sends "y?" expect sends "y/r"

in other words as long expect "y?" condition is satisfied, do send "y\r"

any help greatly appreciated
0
p0sreed
Asked:
p0sreed
  • 8
  • 7
  • 2
  • +1
1 Solution
 
Michael WorshamInfrastructure / Solutions ArchitectCommented:
Try running 'fsck -y -f /dev/sdb1'

The '-f' means force and will usually exclude the Abort prompt.
0
 
p0sreedAuthor Commented:
That doesn't work either.

[root@venus ~]# fsck -y -f /dev/sdb1
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
The filesystem size (according to the superblock) is 17657435 blocks
The physical size of the device is 4885760 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort? yes

[root@venus ~]#
0
 
Michael WorshamInfrastructure / Solutions ArchitectCommented:
Do you have software RAID enabled? Also is the drive you're trying to check/fix unmounted?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
p0sreedAuthor Commented:
No, i don't have software Raid enabled.

Yes, the drive i am trying to fix is unmounted.

The volume is on the 2nd hard drive.
0
 
p0sreedAuthor Commented:
Can you please help me fabricate the exact script?
0
 
Duncan RoeSoftware DeveloperCommented:
Happy to do that. What are you starting from (what do you have so far?). I usually start my expect scripts thus (see box)
Uncomment puts for confirmation of passed arguments (if any)
Uncomment exp_internal to debug expect sequences that don't seem to work right (delay exp_internal 1 until you get to the problem area)
Uncomment log_user when it all works (optional)
#!/bin/sh
# expect treats the next line as a comment continuation while bash obeys it \
exec expect -f "$0" -- "$@"
#puts "argc is $argc, argv is $argv"
#log_user 0
#exp_internal 1

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
exp_continue can be a good alternative to a while loop
0
 
p0sreedAuthor Commented:
i want to run fsck /dev/sdb1

the question this command sends out is "Abort?", i want expect to send "n\r" to that, then the next thing fsck sends is "<y?>", i want expect to send "y\r" until there is no more questions to be answered for fsck..
0
 
Duncan RoeSoftware DeveloperCommented:
I can't do this for you - I don't have fsck behaving as you do. I was hoping you'd made some attempt to script this already. Based on what you've posted, the script in the box may get you started. If it doesn't, uncomment  "exp_internal 1" and post the output.
If a timeout occurs you are interacting with fsck until you type \\ (which kills it). I have set the timeout to 60 seconds.
#!/bin/sh
# expect treats the next line as a comment continuation while bash obeys it \
exec expect -f "$0" -- "$@"
#puts "argc is $argc, argv is \"$argv\""
#log_user 0
#exp_internal 1
set pid [spawn fsck /dev/sdb1]
set timeout 60
expect \
{
  Abort? {exp_send n\r;exp_continue}
  <y?> {exp_send y\r;exp_continue}
  eof {}
  timeout {puts "Putting you in control - to exit type \\ twice";interact \\\\}
}

Open in new window

0
 
p0sreedAuthor Commented:
Your script kind of works

./expect
spawn fsck /dev/sdb1
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
The filesystem size (according to the superblock) is 17657435 blocks
The physical size of the device is 4885760 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? no

Resize inode not valid.  Recreate<y>? yes

/1 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Root inode is not a directory.  Clear<y>?Putting you in control - to exit type \ twice

It answers the 2nd question and stalls and then after 60 secs it puts me in control  to type \\

I want the script to send y endlessly until there is no more "<y>?" questions by fsck

Please, find below what u asked for.
==============================



[root@venus home]# ./expect
spawn fsck /dev/sdb1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7829}

expect: does "" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
fsck 1.39 (29-May-2006)

expect: does "fsck 1.39 (29-May-2006)\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
e2fsck 1.39 (29-May-2006)

expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
The
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
filesystem
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 size (according to the
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
super
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the super" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
block
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
) is
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
17657435
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
block
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 block" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
s
The physical size of the
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
device
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 is
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
4885760
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
block
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 block" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
s
Either the
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 blocks\r\nEither the " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
super
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 blocks\r\nEither the super" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
block
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 blocks\r\nEither the superblock" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 or the partition table is likely to be corrupt!

expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 blocks\r\nEither the superblock or the partition table is likely to be corrupt!\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
Abort<y>?
expect: does "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 blocks\r\nEither the superblock or the partition table is likely to be corrupt!\r\nAbort<y>? " (spawn_id exp6) match glob pattern "Abort?"? yes
expect: set expect_out(0,string) "Abort<"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "fsck 1.39 (29-May-2006)\r\ne2fsck 1.39 (29-May-2006)\r\nThe filesystem size (according to the superblock) is 17657435 blocks\r\nThe physical size of the device is 4885760 blocks\r\nEither the superblock or the partition table is likely to be corrupt!\r\nAbort<"
send: sending "n\r" to { exp6 }
expect: continuing expect

expect: does "y>? " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
no

expect: does "y>? no\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no


expect: does "y>? no\r\n\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
Resize
expect: does "y>? no\r\n\r\nResize " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
inode
expect: does "y>? no\r\n\r\nResize inode" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 not valid.  
expect: does "y>? no\r\n\r\nResize inode not valid.  " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
Recreate<y>?
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
yes

expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no


expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
/1
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 contains a file system with errors
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
, check forced.

expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
Pass 1: Checking
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
inode
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inode" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
s,
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
block
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, block" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
s, and sizes

expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\n" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
R
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nR" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
oot
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nRoot " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
inode
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nRoot inode" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
 is not a
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nRoot inode is not a " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
directory
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nRoot inode is not a directory" (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
.  
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nRoot inode is not a directory.  " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
Clear<y>?
expect: does "y>? no\r\n\r\nResize inode not valid.  Recreate<y>? yes\r\n\r\n/1 contains a file system with errors, check forced.\r\nPass 1: Checking inodes, blocks, and sizes\r\nRoot inode is not a directory.  Clear<y>? " (spawn_id exp6) match glob pattern "Abort?"? no
"<y?>"? no
expect: timed out
Putting you in control - to exit type \ twice
defining key \\, action interpreter
tty_raw_noecho: was raw = 0  echo = 1



0
 
Duncan RoeSoftware DeveloperCommented:
Actually, expect didn't answer the 2nd question - fsck did. You have to be especially careful to get the *exact* sequence of characters that you are waiting for - not "y?", not "<y?>" but "<y>?".
When the expect script handed control to you, you could have tried answering "y" a few times before exiting with \\. That's the idea - if the script hangs you answer a few prompts and fix the script to work next time. But this time it hadn't really got far enough to do that meaningfully.
The script should really try to cater for the times that fsck answers "yes" by itself, otherwise it will type ahead many yes answers and possibly a future Abort prompt will get one of them. Not so simple - for now I will assume that fsck always answers Recreate - if it hangs then you'll know why.
By the way, default pattern-matching is glob, which means "?" matches any single character. I'll change to using exact matching.
As before uncomment  "exp_internal 1" and post the output if it hangs (try to limit your post to exclude earlier successful bits, if any ;).
#!/bin/sh
# expect treats the next line as a comment continuation while bash obeys it \
exec expect -f "$0" -- "$@"
#puts "argc is $argc, argv is $argv"
#log_user 0
#exp_internal 1
set pid [spawn fsck /dev/sdb1]
set timeout 60
expect \
{
  -ex Abort<y>? {exp_send n\r;exp_continue}
  -ex Recreate<y>? {exp_continue}
  -ex <y>? {exp_send y\r;exp_continue}
  eof {}
  timeout {puts "Putting you in control - to exit type \\ twice";interact \\\\}
}

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
Another thing you can do is hold down the Enter key. Prompts ending <y>? have "yes" as default, so you will answer them. Don't hold it down too long to start with, or useful information may scroll past and be lost. That's what I do when faced with a lengthy disk-fixing session.
0
 
p0sreedAuthor Commented:
Your script did the trick.

I accidentally did mke2fs /dev/sb1 after fsck completed.

I wanted to do mke2fs -N /dev/sb1.

Do you know of any way to recover my old data?
0
 
Duncan RoeSoftware DeveloperCommented:
Oh dear. Nothing short of going to a professional recovery service. You really comprehensively destroyed it. I feel your pain.
0
 
p0sreedAuthor Commented:
Oh, don't worry, this was a backup from one of the solaris servers i have. i was upgrading veritas storage foundation and i didn't know that after the upgrade vertias would read the private region of the disks in my storage device and mount the filesystems.

Back then, i copied the data over the linux box and went ahead and upgraded. During the upgrade, the second hard on my linux box crashed.In order to fix it, i had to do the fsck.

Couple of days back i found out that the data on my storage devices are intact, i still wanted to find out the solution to my fsck problem,  i will need this in the future.

Your solution has been excellent. Hat's off to you.
0
 
Duncan RoeSoftware DeveloperCommented:
Don't forget to close this question, award points etc. ;)
0
 
p0sreedAuthor Commented:
Thank you, Duncan. You Rock!
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 8
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now