Why do you have to redirect stuff to /dev/null? Do you need to get rid of data or what? I don’t know.
Can someone show me a good simple example of why you need to do this.
Thanks,
Why do you have to redirect stuff to /dev/null? Do you need to get rid of data or what? I don’t know.
Can someone show me a good simple example of why you need to do this.
Thanks,
Well there is a device called null in the device folder to get rid of data. This is the system’s trash, waste or black hole as some may call it. For instance when I don’t care about any output of the program I will send everything to /dev/null. When I want to delete a file from the system I can move it to device null (you should use rm). Also when I have a sequence of commands and put them in a subshell with command substitution, it can be that I don’t want the output of one of the commands and then I can simply sends it’s output to device null. It’s as useful as your disk devices
edit: You can redirect to /dev/null without getting an EOF error and your exit status of your command is successful.
Redirects stdout; only print errors to screen:
command > /dev/null
Redirects stdout; only print errors to screen:
command 1> /dev/null
Redirects errors; only print normal output:
command 2> /dev/null
Redirect stdout and stderr:
command &> /dev/null
Redirect stderr to stdout (could be useful for piping)
command 2>&1
for fun print everything to a terminal window (type in terminal tty to get it’s device)
command > /dev/ttys000
if you don’t send the output to the trash, then where does it go?
To your screen or returned string from do shell script
Hi DJ Bazzie Wazzie,
Thanks for the great summary about redirection. What I don’t understand is what happens when you combine them like this:
set t to (do shell script "security 2>&1 >/dev/null find-generic-password -gl MyNoteItem")
I’m trying to get the note from a secure note. Here the output of “security” goes from stderr to stdout to /dev/null. If everything is going to the trash, then why do I get the hexadecimal output?
From Nigel’s post in the previously mentioned post from this site, I know that the hex output is returned because the secure note has special characters (i.e. newline characters). My guess is that the output goes from stderr to both stdout and /dev/null. Again, I’m not sure why we get output here.
Thanks,
Model: MacBook Pro
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)
Yes I know this can be confusing, but lets do it step by step:
set t to (do shell script "security find-generic-password -gl MyNoteItem")
You’ll see a lot of data but no password right? We didn’t get the result we wanted but where did it go? The thing is that for security reason Apple decided not to print the password to stdout like everything else but to stderr. Well In my before last example you see also the 2>&1 code which redirects stderr to stdout. So when using that we’ll see the stderr as well
set t to (do shell script "security 2>&1 find-generic-password -gl MyNoteItem")
As you can see, the password is visible (first line on my machine) with all the other data from stdout as well. In other words; I can see stdout and stderr mixed together. Because we don’t use the data from stdout but only from stderr we redirect stdout to /dev/null so nobody sees it.
set t to (do shell script "security 2>&1 >/dev/null find-generic-password -gl MyNoteItem")
And now we get only the password with the password label. Some may expect that once stderr is redirected to stdout that by every redirection of stdout, stderr will follow. It’s a quite common mistake but it doesn’t work that way. When you redirect a file descriptor, other file descriptors redirected to this file descriptor doesn’t follow.
edit: corrected typo
Hi DJ Bazzie Wazzie,
I think I have seen the light.
2>&1 >/dev/null
So what this does is send stdout to the trash first. Then, we get a new stdout from stderr (i.e. the password or note). It works backwards!
Thanks a lot,