Tag Info

Hot answers tagged

37

Using cat Since your file is short, you can use cat. cat filename Using less If you have to view the contents of a longer file, you can use a pager such as less. less filename You can make less behave like cat when invoked on small files and behave normally otherwise by passing it the -F and -X flags. less -FX filename I have an alias for less ...


18

Bash itself will be mostly the same. There will be a few small differences but none of these will affect the portability of your scripts. The major problem is that the MacOS coreutils are FreeBSD-based while the utilities you are used to are most likely from the GNU project. The FreeBSD coreutils are not always compatible with the GNU coreutils. There are ...


15

Keep the dotfiles as portable as possible and avoid OS dependent settings or switches that require a particular version of a tool, e.g. avoid GNU syntax if you don't use GNU software on all systems. You'll probably run into situations where it's desirable to use system specific settings. In that case use a switch statement with the individual settings: ...


10

If you're using OS X's Terminal.app, it will capture Page up/down keypresses and just scroll the window contents, as if you used the scroll bar. You can use Shift+Page up/down to send them to the application inside the terminal. Using that, you should be able to scroll by a page at a time using: Control+B [ Arrows keys or Shift+Page up/down Control+C when ...


10

Moving a file to the location of an already existing file replaces the existing file. In this case the /dev/null device file is replaced, just as any normal file would be. To avoid this use the -i (interactive, warns before overwriting) or -n (no clober) option for mv. /dev/null only performs its special function as a bit-bucket then the device is opened as ...


9

On Linux, you could use the immutable flag using chattr to achieve read-only on a filesystem level (requires appropriate permissions though). I don't use OS X and do not know if it has something similar, but you could achieve "after script is run, test.txt still exist" using: #!/bin/sh mv test.txt test.bak trap "mv test.bak test.txt" EXIT rm -f test.txt ...


8

Umm, because you overwrite the special file with normal one? What did you expect to happen? dev/null is not a directory, it is a file pointing to a null device. When you mv something to it, you delete the original and replace it with whatever you moved: $ file /dev/null /dev/null: character special $ sudo mv file /dev/null $ file /dev/null /dev/null: ...


8

Looking at the source code for mv, http://www.opensource.apple.com/source/file_cmds/file_cmds-220.7/mv/mv.c : /* * If rename fails because we're trying to cross devices, and * it's a regular file, do the copy internally; otherwise, use * cp and rm. */ if (lstat(from, &sb)) { warn("%s", from); return (1); } return (S_ISREG(sb.st_mode) ? ...


8

The main reason that X terminal applications run non-login shells by default is that in the beginning of time, your .Xsession would have run the .profile to set up your initial login items. Then, since that was all set up already, terminal apps didn't need to run it, they could run the .bashrc. Discussion of why this would matter is at ...


6

The following is what I ended up using to reliably create a temporary directory that works on both Linux and Darwin (Mac OS X), without hardcoding $TMPDIR or /tmp: mytmpdir=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'` Background The GNU mktemp command requires no arguments. Plain mktemp will create a temporary file in the system temporary ...


6

If you want the equivalent to apt-get or yum on Mac OS X, you have two choices. Homebrew: http://brew.sh Macports: http://www.macports.org You can use brew install PACKAGE_NAME or port install PACKAGE_NAME to install the package available.


6

For the shebang line, use: #!/usr/bin/env perl env will search the PATH at execute the first perl that it finds. If you want to specify any options for perl, just add them to the end of the line above. If more than one perl is installed, env will run the first one it finds in PATH.


6

Or, you can have sh take care of it for you: #!/bin/sh exec perl -x "$0" "$@" #!/usr/bin/perl ... Yes, that's sh and Perl all in one file. From man perlrun: -x tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first ...


5

OS X 10.8 is also listed as a UNIX 03 registered product in http://www.opengroup.org/openbrand/register/. If you are using bash, it is not POSIX-compliant by default. echo doesn't support any options by default in sh though. $ bash $ builtin echo -e a; /bin/echo -e a a -e a $ sh $ builtin echo -e a; /bin/echo -e a -e a -e a $ shopt -u xpg_echo $ builtin ...


5

In general wordsize is decided upon target architecture when compiling. Your compiler will normally compile using wordsize for current system. Using gcc (among others) you can also tune this by using various flags. E.g. on a 64-bit host you can compile for 32-bit machine, or force 32-bit words. -m32 # int, long and pointer to 32 bits, generates code for ...


5

Regarding an explanation of the explanation: See the FreeBSD forum. Basically the OS X userspace is essentially FreeBSD (with small elements of NetBSD) but the kernel itself is a fork of the MACH Kernel that makes it more monolithic in nature (like the network stack and process model are in line with FreeBSD). For a technical description, you'll probably ...


5

In the if conditions you should use the defaults read command, and not write. Otherwise the result is always true and of course the elif never runs. Also, that is not the syntax for if conditions. You should use: if [ $(command) == "TRUE" ]; then But in this case, perhaps something like this would be less verbose: STATUS=$(defaults read ...


5

tl;dr: it's doable but you will have to work just a little bit. If you don't have the ability to use Ethernet, and are installing from netinst media, you're basically screwed (although if you're really determined you can make it work). When I originally wrote this answer, I'd only done this once, but now I'm doing it again on a different Mac, so I've split ...


5

I may have found something: The ls command on OS X has this switch: -O Include the file flags in a long (-l) output. The result is: $ ls -O Info.plist -rw-r--r-- 1 root wheel compressed 15730 11 jui 15:02 Info.plist I just checked (experimentally) that du always reports 0 for HFS+ compressed files. Copying compressed files uncompress them; ...


5

It seems you are asking two or three questions. To use ssh to tunnel, I advise you start a non-interactive session ssh -C2qTnN -D 8080 user@IP That will put the ssh tunnel in the background. Use the proxy: You then configure your browser or system settings to use the proxy on localhost port 8080. Stop using the proxy: You then re-configure your system ...


5

basename operates on its command line argument, it doesn't read from standard input. You don't need to call the basename utility, and you'd better not: all it would do is strip off the part before the last /, and it would be slow to call an external command for each entry, you can use a text processing utility instead. find ~ -type f | sed 's!.*/!!' | sort ...


5

The way it's supposed work is that, at the point when you get a shell prompt, both .profile and .bashrc have been run. The specific details of how you get to that point are of secondary relevance, but if either of the files didn't get run at all, you'd have a shell with incomplete settings. The reason terminal emulators on Linux (and other X-based systems) ...


5

Using awk: awk ' BEGIN { fields[1] fields[3] fields[4] fields[5] fields[6] fields[7] last_field=8 } ( NR%21 in fields ) { printf($0",") } NR%21==last_field' in_file.txt Or better yet: awk ' NR%21 ~ /^(1|3|4|5|6|7)$/ { printf($0",") } NR%21==8' in_file.txt GNU sed has a nice extension to match the nth line after a ...


5

Your problem is that you try to enter C code into a shell prompt, this doesn't work for obvious reasons. You could either put it into a correct C file, compile it and get a valid binary you could execute: #include <linux/kd.h> #include <sys/ioctl.h> #include <fcntl.h> #include <unistd.h> #include <err.h> #include ...


4

The UNIX trademark is owned by the Open Group, who use UNIX as a certification mark for SUS conformance. In that sense, OS X (or some versions of it) are UNIX, but Linux distributions and other BSD distributions are not. See http://www.opengroup.org/downloads/UNIX-certprog.pdf and http://www.opengroup.org/openbrand/register/. Parts of the Linux kernel ...


4

Need differentiate the points of views: From the shell-users point of view here is no big differences. OS X is fully certified Unix operating system, so it's command line is fully compliant with Unix standards and POSIX. You have terminal with shell, all shell commands (BSD style), you can install e.g. macports for GNU-versions of commands. e.g. the bundled ...


4

Get the source wget "http://www.sentex.net/~mwandel/jhead/jhead-2.97.tar.gz" Untar the source tar xzf jhead-2.97.tar.gz Or, get and untar the source in one step curl "http://www.sentex.net/~mwandel/jhead/jhead-2.97.tar.gz" | tar xz Now you have a directory called jhead-2.97. Enter that directory and run make. cd jhead-2.97 make This will compile ...


4

Use ls *s1r* *s2r* *s3r* *s19r*. If you care about non existing files you can set the nullglob option: nullglob If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves. If your shell is not bash, there is ...


4

You can see if the server accepts a connection on the port by running telnet HOSTNAME PORT or nc HOSTNAME PORT. If the server is listening, the connection will be established, you'll see the banner sent by the server if any, and you'll be able to type commands. If the server isn't listening or if a firewall is blocking the way, nc or telnet will not be able ...


4

It's actually not too hard, but you do need to have admin privileges (using the sudo command to write to /etc). From Terminal (or your favorite substitute), see if there's anything in the file /etc/launchd.conf: cat /etc/launchd.conf If you get an error like cat: /etc/launchd.conf: No such file or directory then continue with the next step. If ...



Only top voted, non community-wiki answers of a minimum length are eligible