将尾部输出重定向到程序中

时间:2016-04-10 03:47:18

标签: linux bash tail

我想使用tail作为stdin从文本文件中发送程序最新的行。

首先,我向程序回显一些每次都相同的输入,然后从输入文件发送尾输入,该输入文件应首先通过sed处理。以下是我期望工作的命令行。但是当程序运行时,它只接收回声输入,而不是尾部输入。

(echo "new" && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex' && cat) | ./program

但是,以下工作完全符合预期,将所有内容打印到终端:

echo "new" && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex' && cat

所以我尝试了另一种类型的输出,并且在发布回声文本的同时,尾部文本没有出现在任何地方:

(echo "new"  && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex') | tee out.txt 

这让我觉得这是一个缓冲问题,但我尝试了unbuffer程序和所有其他建议(https://superuser.com/questions/59497/writing-tail-f-output-to-another-file)没有结果。尾部输出在哪里,如何按预期进入我的程序?

3 个答案:

答案 0 :(得分:1)

当我使用以下命令为sed命令添加前缀时,解决了缓冲问题:

stdbuf -i0 -o0 -e0 

使用非缓冲更为可取,这对我来说根本不起作用。 Dave M建议使用sed相对较新的-u似乎也可以解决问题。

答案 1 :(得分:1)

您可能会感到困惑的一件事 - |(管道)的优先级高于&&(连续执行)。所以当你说

(echo "new" && tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex' && cat) | ./program

相当于

(echo "new" && (tail -f ~/inputfile 2> /dev/null | sed -n -r 'some regex') && cat) | ./program

所以cat并没有真正做任何事情,sed输出可能有点缓冲。您可以尝试使用-u sed选项来使其使用无缓冲输出:

(echo "new" && (tail -f ~/inputfile 2> /dev/null | sed -n -u -r 'some regex')) | ./program

我认为当输出是终端时,某些版本的sed默认为-u,而不是当它是管道时,因此这可能是您所看到的差异的来源。

答案 2 :(得分:0)

您可以在 //PHOTO ONE UPLOAD TAKE AND SELECT - (IBAction)selectPhoto:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; selectedImageView = self.imageView; // Add this [self presentViewController:picker animated:YES completion:NULL]; } - (IBAction)takePhoto:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; selectedImageView = self.imageView; // Add this [self presentViewController:picker animated:YES completion:NULL]; } - (IBAction)takePhotoTwo:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; selectedImageView = self.imageTwo; // Add this [self presentViewController:picker animated:YES completion:NULL]; } - (IBAction)selectPhotoTwo:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; selectedImageView = self.imageTwo; // Add this [self presentViewController:picker animated:YES completion:NULL]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = info[UIImagePickerControllerEditedImage]; selectedImageView.image = image; [picker dismissViewControllerAnimated:YES completion:NULL]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:NULL]; } - (IBAction)saveButton:(id)sender { //FIRST IMAGE DATA NSData *imgData = UIImageJPEGRepresentation(self.imageView.image, 0.5); NSMutableDictionary *file = [[NSMutableDictionary alloc] init]; NSString *base64Image = [imgData base64EncodedString]; [file setObject:base64Image forKey:@"file"]; NSString *timestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]]; NSString *imageTitle = _itemName.text; NSString *filePath = [NSString stringWithFormat:@"%@%@.jpg",@"public://stored/", imageTitle]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", imageTitle]; [file setObject:filePath forKey:@"filepath"]; [file setObject:fileName forKey:@"filename"]; [file setObject:timestamp forKey:@"timestamp"]; NSString *fileSize = [NSString stringWithFormat:@"%lu", (unsigned long)[imgData length]]; [file setObject:fileSize forKey:@"filesize"]; //SECOND IMAGE DATA NSData *secondimgData = UIImageJPEGRepresentation(self.imageTwo.image, 0.5); NSMutableDictionary *secondfile = [[NSMutableDictionary alloc] init]; NSString *secondbase64Image = [secondimgData base64EncodedString]; [file setObject:secondbase64Image forKey:@"file"]; NSString *secondtimestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]]; NSString *secondimageTitle = secondtimestamp; NSString *secondfilePath = [NSString stringWithFormat:@"%@%@.jpg",@"public://stored/", secondimageTitle]; NSString *secondfileName = [NSString stringWithFormat:@"%@.jpg", secondimageTitle]; [secondfile setObject:secondfilePath forKey:@"filepath"]; [secondfile setObject:secondfileName forKey:@"filename"]; [secondfile setObject:secondtimestamp forKey:@"timestamp"]; NSString *secondfileSize = [NSString stringWithFormat:@"%lu", (unsigned long)[secondimgData length]]; [secondfile setObject:secondfileSize forKey:@"filesize"]; [DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"File uploaded!"); //FIRST IMAGE FILE [file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"]; [file removeObjectForKey:@"file"]; fid = [responseObject objectForKey:@"fid"]; NSLog(@"%@",responseObject); NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject: [NSString stringWithFormat:@"%@", fid] forKey:@"fid"]; NSLog(@"%@", fid); NSDictionary *fidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:dict] forKey:@"und"]; [nodeData setObject:fidLangDict forKey:@"field_photo"]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Node did not save!"); }]; [DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"File uploaded!"); //SECOND IMAGE FILE [secondfile setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"]; [secondfile removeObjectForKey:@"file"]; fid = [responseObject objectForKey:@"fid"]; NSLog(@"%@",responseObject); NSMutableDictionary *secondDict = [NSMutableDictionary dictionary]; [secondDict setObject: [NSString stringWithFormat:@"%@", fid] forKey:@"fid"]; NSLog(@"%@", fid); NSDictionary *secondfidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:secondDict] forKey:@"und"]; [nodeData setObject:secondfidLangDict forKey:@"phototwo"]; [DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Node saved!"); UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil]; ipadaccountViewController *AccountViewController = (ipadaccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"]; [self.navigationController popViewControllerAnimated:TRUE]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Node did not save!"); }]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Node did not save!"); }]; 中使用<field_photo> <und is_array="true"> <item> <fid>265</fid> <uid>5</uid> <filename>phone.jpg</filename> <uri>public://stored/phone_1.jpg</uri> <filemime>image/jpeg</filemime> <filesize>161858</filesize> <status>1</status> <timestamp>1460264800</timestamp> <rdf_mapping/> <alt/> <title/> <width>1536</width> <height>1536</height> </item> </und> </field_photo> <phototwo> <und is_array="true"> <item> <fid>266</fid> <uid>5</uid> <filename>phone.jpg</filename> <uri>public://stored/phone_2.jpg</uri> <filemime>image/jpeg</filemime> <filesize>161858</filesize> <status>1</status> <timestamp>1460264800</timestamp> <rdf_mapping/> <alt/> <title/> <width>1536</width> <height>1536</height> </item> </und> </phototwo> 命令(有关详细信息,请参阅联机帮助页中的命令列表),以便在开头插入:

i