Programming :: Bash: How To Discard Unwanted Stdin

Jun 22, 2010

Often in bash we read lines from stdin in a loop and implicitly discard the remaining stdin by terminating the loop. Is it possible to discard it without terminating the loop? It could lead to smaller code.

Here's an example which uses two loops and below is the same algorithm assuming unwanted stdin can be discarded

Code:
found=
while read destination gateway _
do
[[ $destination = default ]] && found=yes && break
done <<< "$( route )"

[Code].....

View 12 Replies


ADVERTISEMENT

Programming :: STDIN, STDOUT, STDERROR Redirection For Scripting - Bash: Log.txt: No Such File Or Directory

Sep 7, 2010

I'm writing a script to execute bash commands in the PHP CLI. I would like to suppress errors from bash and write my own error message if an error occurs. So far I have this (assuming log.txt doesn't exist!):

Code:

tac log.txt 2>/dev/null

Which works as expected, tac kicks up an error but the error is suppressed, but when I use this:

Code:

tac < log.txt 2>/dev/null

I get:

Code:

bash: log.txt: No such file or directory

The tac error is suppressed but bash still gives me a dirty error.

View 2 Replies View Related

Programming :: Get Rid Of Unwanted Lines Without Grep -v In BASH?

Jun 9, 2009

I would like to know how I can get the ouput from the following dmidecode command in example 1 to look like example 2 without having to grep -v all the unwanted lines.Is there a way in awk or sed?Example 1

Code:
Processor Information
Socket Designation: Socket 1 CPU 1

[code]...

View 7 Replies View Related

Programming :: Clearing Stdin Obtained From Pipe

Mar 14, 2010

I have a small program that reads stdin from a pipe using fgets. Now fgets blocks for the first line but after that it will not block.

The code, my_echo.c -
int main(int argc, char **argv) { char buf [2000] ;
char* pc ; printf("hello ") ; while (1)
{ buf[0] = (char) 0 ;
pc = fgets(buf, sizeof(buf), stdin);
if (pc != NULL) printf("%s ",buf); } return 0; }

How its called
* In terminal window 1: ./my_echo < my_fifo
* In terminal window 2: echo "1234" > my_fifo
* In terminal window 1: prints hello then 1234.
* Checking with ksysguard or top shows that my_echo is consuming 40% of CPU time.

Adding a few printf's shows that the gets is not blocking and returns a null pointer.
* In terminal window 2: echo "qwerty" > my_fifo
* In terminal window 1 qwerty prints.
I want a read function that does in fact block so my program does not tie up CPU time, read does not block.

View 7 Replies View Related

Programming :: Write A Script Which Will Get A Number From STDIN?

Dec 15, 2008

I need to write a script which will get a number from STDIN and then with that number echo a set number of questions (its for a firewall config). Heres what I want the user to receive.

How many ports do you want open? 3

1. specify port: 80
2. specify port: 21
3. specify port: 23

In the background this will echo a command out to a text file which will read as follows:

open port 80
open port 21
open port 23

I've done this before but I've completely forgotten how

View 2 Replies View Related

Programming ::make AWK Do *something* Without Giving It Any Stdin Nor Inputfile?

Oct 10, 2010

[code]...

I've given the output from `df` on AWK's stdin. But what I wonder is if there's a way to get AWK to run `df` itself, produce the same output, and exit? Doesn't seem to be that simple. Here's some examples: Works for some reason, and I think only in Bash (nothing is required in the $()

[code]...

does AWK absolutely need *something* on stdin, before it begins to process the data? Can it be made to open a file or stream internally, act upon that as though it were the stdin, and exit?

View 14 Replies View Related

Programming :: How To Redirect Stdin For Child Process

Aug 18, 2010

I'm trying to write a shell script that do ftp and download file periodically, this script should be called by a daemon running in the background.

the shell script "script.sh" is as follows:

Code:
yafc ftp://test:test@192.168.1.225:21 < commands
and the "commands" files is

Code:
d Root/md5* /
quit

if I run script.sh it will work just fine. But when the daemon software calls the "script.sh", the script will send ftp login request to the ftp server, but will not even answer the username or anything.

I believe it is something about child process redirection, but I don't know how to deal with it.

This problem is not only with yafc, it is the same with any ftp client or any application like telnet and so.

View 4 Replies View Related

Programming :: Read From Stdin In Perl Do Not Wait For EOF?

Apr 28, 2010

I am trying to read one line at a time in perl and not wait for stdin eof to start the action:

Code:
foreach(<STDIN>){
<do something>
}

if i pipe ls into it, it waits for ls to be done before doing the action.

View 1 Replies View Related

Programming :: Stuffing A Key Into Another Virtual Terminal's Stdin?

Oct 20, 2010

I want to put a keystroke into another virtual terminal's stdin.A simple 'echo p > /dev/tty7' causes a p to appear on the console of tty7 but not the app running in tty7 to respond as though a p key had been pressed. Per the instructions of a fellow in the software forum I tried using an ioctl, but that returned a permissions error, even when I made the target VT's permissions 777 (and it had the same owner).

View 1 Replies View Related

Programming :: Redirecting Stdin / Stdout And Stderr For Program Run As Fork()/execl()?

Nov 24, 2010

I'm trying to write a program that will fork a series of FTP sessions. For each session, there should be separate input and output files associated with stdin and stdout/stderr.
I keep reading how I should be able to do that with dup2() in the child process before the execl(), but it's not working for me. Could someone please explain what I've done wrong? The program also has a 30-second sniper alarm for testing and killing of FTPs that go dormant for too long.

The code: (ftpmon.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

[code]....

The output:

$ ftpmon
Connected to gila-crstest.gilacorp.com (172.16.20.8).
220 (vsFTPd 2.0.1)
ftp> waitpid(): Interrupted system call

Why am I getting the ftp> prompt? If the dup2() works, shouldn't it be taking input from my script and not my terminal? In stead, it does nothing, and winds up getting killed after 30 seconds. The log file is created, but it's empty after the run.

View 3 Replies View Related

Programming :: Redirecting Stdin / Stdout And Stderr And Finding Files Name And Other Stats?

Aug 8, 2010

I'm working on an application used for backup/archiving. That can be archiving contents on block devices, tapes, as well as regular files. The application stores data in hard packed low redundancy heaps with multiple indexes pointing out uniquely stored, (shared), fractions in the heap.

And the application supports taking and reverting to snapshot of total storage on several computers running different OS, as well as simply taking on archiving of single files. It uses hamming code diversity to defeat the disk rot, instead of using raid arrays which has proven to become pretty much useless when the arrays climb over some terabytes in size. It is intended to be a distributed CMS (content management system) for a diversity of platforms, with focus on secure storage/archiving. i have a unix shell tool that acts like gzip, cat, dd etc in being able to pipe data between applications.

Example:

dd if=/dev/sda bs=1b | gzip -cq > my.sda.raw.gz

the tool can handle different files in a struct array, like:

Code:

enum FilesOpenStatusValue {
FileIsClosed = 0,
FileIsOpen,

[code]....

Is there a better way of getting the file name of the redirected file, (respecting the fact that there may not always exist such a thing as a file name for a redirection pipe).
Should i work with inodes instead, and then take a completely different approach when porting to non-unix platforms? Why isn't there a system call like get_filename(stdin); ?

If you have any input on this, or some questions, then please don't hesitate to post in this thread. To add some offtopic to the thread - Here is a performance tip: When doing data shuffling on streams one should avoid just using some arbitrary record length, (like 512 bytes). Use stat() to get the recommended block size in stat.st_blksize and use copy buffers of that size to get optimal throughput in your programs.

View 4 Replies View Related

Programming :: Deleting Unwanted Characters In File

Mar 31, 2010

Currently, I'm working on personal project. and I'm kinda stuck. What I want to do is that open a file, and edit that file (deleting unwanted characters). The problem arises after I deleted unwanted characters, the file still has the same length of the original one. Let's assume that we have a file with "1234" in it. I deleted "3" ( I overwrite "\0" ) so now when I check the file, it's 124. But when I check the length, the both have the same size as 4

Here is an example source code
int length, length2;
num = open("a.dat", 2)
length = lseek(num, 0, 2); // Initial length
lseek(num, 2, 0); // editing
write(num, "\0", 1);
length2 = lseek(num, 0, 2); // Final length
close(num);

When I print those values those are exactly the same. Length2 should be one less than length, but the both are 4. What's wrong in m code? Am I supposed to use different character rather than "\0"?

View 8 Replies View Related

Programming :: Get Ride Unwanted Compiler / Linker Output

Jul 18, 2011

I am using g++ 4.5.2 I copied and tried a piece simple (Making a Class Writealbe to a Stream) program, from page 363 of book(C++ cookbook), Example 10-6 your can download and test by yourself [URL]

[Code]....

View 1 Replies View Related

Programming :: Write A Script That Will Detect All The Unwanted Files In A Particular Directory

Apr 1, 2011

I want to write a script that will detect all the unwanted files in a particular directory and delete them in one fly..

View 11 Replies View Related

Programming :: Read In Stdin And Ping Line By Line?

Jan 8, 2010

Code:

#!/usr/bin/perl
use 5.10.0;
use strict;
use warnings;
use Net::Ping;

[code]....

View 1 Replies View Related

Programming :: Bash: Printing The Line Number In Bash Script?

Feb 4, 2011

I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened.

View 3 Replies View Related

Programming :: Bash Ctrl+c Tarp And Bash Read With Timeout?

Jan 24, 2010

simple bash code:

Code:
#!/bin/bash
trap "echo 'you got me'" SIGINT SIGTERM # to trap ctrl+c
echo "Press ctrl+c during 5 sec loop"
for ((i=0;i<5;i++)); do

[Code]...

How come code behaves normally and stops when ctrl+c signal is caught and resumes, but after I use at least one timeout read in the code it looks like, if signal is caught again it doesn't pause the execution but skips the loop. If you remove -t (timeout) option from the read, both loops look the same!

View 10 Replies View Related

Programming :: Reading A Bash Variable In Bash Scripting ?

Nov 26, 2008

I have a config file that contains:

my.config:

Code:

Now in my bash script, I want to get the output /home/user instead of $HOME once read. So far, I have managed to get the $HOME variable but I can't get it to echo the variable. All I get is the output $HOME.

Here is my parse_cmd script:

Code:

View 3 Replies View Related

Programming :: Run Multiple Bash And Php Scripts From A Bash Script?

Jul 25, 2011

I have written quite a few separate bash & scripts and php scripts that up to now I have run from cron jobs. However I have to estimate how long each takes to run, before running the next and so it probably takes much longer than necessary to run them all. They have to run in order.

Now there are so many I am thinking it would be better to have a master bash script that would run one after the other, but I am not sure how to get the master script to wait before starting to run the next script. Is this possible and is there a command that will make the script wait between bash and php scripts , for them to finish, before running the next?

View 5 Replies View Related

Programming :: Bash: Get Filename And Extension Using Bash?

Jan 9, 2010

I would like to get the filename (without extension) and the extension separately. The best solution I found so far is:

Let FILE="thefilenameofsomefilesfor_instance.txt"

Code:

NAME=`echo "$FILE" | cut -d'.' -f1`
EXTENSION=`echo "$FILE" | cut -d'.' -f2`

I think it would be better to count the len and remove 3 chars to right to get the extension, but it can be macintosh filenames with have 4 chars for extensions.

View 5 Replies View Related

General :: Partitioning - Trim / Discard A Whole SSD Partition?

Jul 8, 2011

My partition /dev/sda3 on an SSD drive doesn't contain any filesystem, but it contains garbage. How do I do a TRIM/DISCARD operation on the whole partition?

View 2 Replies View Related

Ubuntu :: Cookie Saving In Chromium / Discard All?

Aug 9, 2010

I would like Chromium to discard all cookies at the end of the session except for specified domains. I thought I had it set up right, but it doesn't remember me on either ubuntuforums or facebook.

Does anyone know if I'm missing something or if this is a bug?
Screenshot of configure attached.

View 2 Replies View Related

Ubuntu Servers :: Discard The Spam Mail ?

Aug 11, 2011

I had setup postfix and spam assassin, and in my environment, I forward the incoming email to my exchange server.

Everything works now, in my mail box, I can receive the email with subject ******SPAM*****. but is there a solution, when spam assassin finished the scan and mark spam, if the score is very high, may be stop to forward it to exchange, for example, send it to a special email box?

View 1 Replies View Related

Software :: How To Discard Checked Out Working Copy?

Jun 14, 2010

Let's say I have a checked out a directory from the svn repo and later on I find that I have no need to make any changes. Of course I can just `rm -r` my working copy, but how do I tell the svn repo that I'm no longer using this working copy?

View 2 Replies View Related

Ubuntu :: Use The 'discard' Option When Mounting An Ntfs-partition?

Dec 19, 2010

Is it possible to use the 'discard' option when mounting an ntfs-partition? Because I'm buying an ssd and I intend to dual-boot windows xp (which doesn't support TRIM) and Ubuntu 10.10 (which does).

View 3 Replies View Related

Networking :: Discard The Duplicate Packet While Reading From Socket?

Feb 18, 2011

I am writing a client program which will read an XML file from the server. The issue I am facing is :

1) The client is able to read the complete file but in the end it also receives a duplicate packet which is actually a part of the same(XML) file.

2) I dont want this packet but how can I be sure that this is a duplicate packet and discard it?

3) Why I am getting this packet?

Output example:

suppose this is the file at server to read :

Code:
<root>
<classes>
<class1>CLASS1</class1>
<class2>CLASS2</class2>

[Code]....

View 1 Replies View Related

Programming :: Bash Programming - Rename Files In A Loop?

Mar 31, 2011

I need to rename the resulted searched files from a loopI have the following code:

find . -name DOC* | while read i
do
find $i -type f -name '*.txt'
done

basically, I am searching for all txt files inside any folder starting with DOC name.this code is working fine with me.I need to rename those .txt files to .txtOLDOS: Ubuntu 10.4Bash shell

View 10 Replies View Related

OpenSUSE Network :: Postfix Won't Discard Blank Email From Yahoo

Jun 9, 2011

I am running in a problem that i have put a postfix regex which needs to discard emails having no subjects, while it works fine for gmail,hotmail and other domains but it does not work for [URL] ultimate reason behind this is because yahoo does not send subject line if subject is not included in email.

View 3 Replies View Related

Server :: Make Sendmail Silently Discard 'Catchall' Addresses

Aug 4, 2011

Using sendmail, I am trying to setup a catchall address to silently discard spam mail sent to undefined users. I don't want to bounce a reply for "no user" as this often goes back to munged or innocent addresses.

I have a number of domains, so my virtuser table currently looks rather like this:

Code:

The last line is the "catchall" which is intended to ensure that any undefined users from either my_domain.com or my_domain.co.uk is forwarded to the non-existent "catchall" user account. Note that all mail sent to my_domain.com is passed across to my_domain.co.uk at the very start so all the following rules should apply to messages received at both domains. (Both of these domains appear in the

Being non-existent, I setup the following rules in the access data table:

Code:

This way, if I send an email to 'user1@my_domain.com' it is received and delivered to user1@my_domain.co.uk, but if I send an email to 'rubbish@my_domain.com' it *should* be received and silently discarded.

Unfortunately what actually happens is the server responds with:

Now, (just for testing), I made "catchall" an alias for the "postmaster" account and the mail was then received and delivered into the postmaster mailbox. So I know the virtuser rule is working. The account "catchall", however, is not being silently dropped as desired in either configuration.

View 7 Replies View Related

Red Hat :: Discard Outgoing Mail In Postfix That Have Some Specific Content In Attachment?

Jul 30, 2010

Working in a SW company. guys transfer their codes through email outside the company. mailserver running on postfix. Is there any method to filter mails according to contents of attachment not by file extension.

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved