Programming :: Redirection From A File To Command?
Apr 18, 2011
I am always confused about the redirection operator <Lets say i have a file input.txt that contains one word "hello" without the quotes when i do the following why don't i see any output? $echo < input.txt Secondly, i am slightly confused between input redirection < and pipe | operator. Sometimes they seem to do the same thing. For example i can achieve the output from the above command as follows $ cat input.txt | xargs echo
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.
I have a program that writes to stdout. Is there a way that I can redirect the output to the linux diff command or do I have to write the output to a file and then compare that. For example I have a bunch of test input files for a program and the corresponding expected output in another set of files. And I'd like to do something like ./program < t1.input | diff t1.expected.
Within a bash script, I'm attempting to redirect file descriptors with exec, e.g. exec 3>&1 1>&2; however, I'd like to do something like exec $FD>&1 1>&2, which doesn't work because bash tries to execute the value of $FD. Various placements of eval fail, as well. Is there a way around this, or am I stuck hard-coding the descriptor?
I have this piece of code in my shell script file: Code: useradd $UserName; passwd <& $password I am not able to perform Input Redirection while setting password programatically- without any user intervention. I think there is a way to do it, but it is just getting slipped from my mind at the moment.
Code: # dbus-monitor --system >> /data/eject.txt This one works as expected ... dbus-monitor never terminates and whenever it outputs new lines, they are
I have a linux script that generates a string and prints it to the console. I want this string to be the name of a file and open it for editing in vi. How can this be done?
i've checked the link, and it makes it better. but it doesn't include all the information. i'll continue searching the internet. However i have seen an example of creating a fd:
Code: exec 5<&1 echo "TEST" >&5 exec 5>&-
as in the page, this was intended to redirect the stdout to the fd 5 and create it, and close it. i have the following questions:- what is exactly the meaning of second command? is it to redirect the command stdout "test" to the fd 5? and how i can see the contents of the fd 5? - in the first command, why the < is used instead if > and what is the difference between the below two commands as in the info bash *Redirection section It will be helpful if anyone could include a graph for file descriptor before and after different command execution.
I'm working on some scheduled task script files to keep nightly backups of some of our database information in place, and it's a bit annoying when they blow up. I know how to redirect stdout and stderr to a flat file I can view when I come in, and I know that 2>&1 maps them both to the same file (whatever was named in 1). However, I'm running into some cron-time situations where it's easier to have the two streams together, and other cron-time situations where it's easier to have them separated. I can't really tell which is going to happen; is there some way I could create both kinds of output file for my scripts, so that I've got a std_err only file and an interleaved std_out/std_err file?
Note: I've looked at the 'tee' command, but I don't think it will work for what I'm after. 'tee' appears to only work with stdout; I'm trying to work with stderr.
I am trying to grep multiple numbers from file, grep does have the -f option for that.
Code: grep -f <`seq 500 520` /etc/passwd I know this could be done with
Code: for i in `seq 500 520`; do grep "$i" /etc/passwd; done But my question is fare more behind this example. It is possible to redirect one command output which will be treat as a content of file for another command ?
it dosent work though i originally started off with
Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]+/,/g' this puts a comma inbetween them but takes out the space and dosent put a comma at the end of the line.. basicaly what i needed to know is how i could you multiple sed commands to fix this
I have a number of files.tar.gz that I have to gunzip to get .tar then I tar -xvf .tar, to get a readable file. Is there a way to do that all in one command. I tried to pipe it being new and all and I get errors.
I want to know that is there any method to grep a particular data from a file without using the "cat --- | grep ' ' " command....I need to use a system call for this functionality.
I am using an awk command to print a line from a cvs file.the awk command includes an if statement that filter the output-lets say i want to print all the lines that the price field is greater than 30.i have it working when i put the parameters myself.. but when i try to send them with vars it wont work..i am sending the sign of the if statement - can only be: == , < , >it looks like this:
I'm gonna replace my machine's ip address and hostname using awk command. the pattern of the file is like the following...ip address="192.168.1.100"the script must ask the ip address from the user and replace it with the ip address in the quotation.
My question deals with me creating a name pipe (file) in the my /group directory called chat.I then have to write a script to read from the named pipe and save data into a file called chat.log until the words End of File are passed to the program.
-When I created the named pipe file (chat) I used the mknod chat p command..Is this the correct command to create a named pipe file? -Then I'm having trouble with my script and how to make it run until the words End of File are entered in. This is what I have so far.
I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command.
perl -pi -e "s/$findstring/$replacestring/" testfile where as $findstring = print F_WC_TMP"$line "; and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line ";
But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string.
We make everyday a DB Mysql backup on Linux redhat Enterprise. We are using a bash shell script (and putting it in the crontab) to execute it automatically everyday. We added a line to this script telling, once the backup has completed, to find old backup files (stored on hard disk after each backup) older than x days to remove them. We use the find command (search for file type) with the mtime option and in combination with rm command. Everything runs ok but we also want to add some new code to the same line: If find command cannot find anything or fails, for example if it cannot delete file or fails, send the error message (standard error output) to an error file (like error000001 and increasing) and mail the errorxxxx file to an email address for example to admin@companyname.com. What would be the code for this issue to add it to our find command in the same bash shell script??
Here is the block of code : (The red part is the code that doesn't work) The file is not created and see the output after the code. # i loop create environment structure and k loop create std procedure sub structure.
for i in TRAX2 TRAX BENCH PROD do eval mkdir $"acsayul02501_${i}" eval chmod 2770 $"acsayul02501_${i}"
$cmd If this script is executed, an error is generated. The reason written was that "The execution fails because the pipe is not expanded and is passed to date as an argument".What is meant by expansion of pipe. When we execute date | wc on the command line, it goes fine.then | is not treated as an argument. Why?
I am trying to learn how to pass more than a one-command startup for gnome-terminal.
I will give an example of what I'm trying to do here:
Code: #! /bin/bash # #TODO write this for gnome and xterm USAGE=" ${0##*/} [-x] [-g] code....
However, running with the -g option to invoke gnome-terminal, I get a "There was an error creating the child process for this terminal" error.
This same error occurs if the gnome-terminal line is changed to
Code: gnome-terminal -e mcTerm
Is there any way to pass more than one command on to gnome-terminal? I have tried various single and double quoting senarios and in a final attempt, I abstracted to an exported function all to no avail. Perhaps even though gnome-term is better at many things than xterm, xterm trumps it in this instance.