Tag Info

Hot answers tagged

20

A minus sign before the command name is a convention that login programs use to start login shells. A login program is a program where you typically type your password and that starts a session for you, such as login, sudo -i, su -, sshd, etc. A login shell is the initial shell of a text mode session. Conventionally, when a program invokes another program, ...


18

In modern bash (version 4.2 and above): [[ -v name_of_var ]] From help test: -v VAR, True if the shell variable VAR is set


16

It is a bad idea (to have strange characters in file names) but you could do mv somefile.txt "foo bar" (you could also have done mv somefile.txt "$(printf "foo\nbar")" or mv somefile.txt foo$'\n'bar, etc... details are specific to your shell. I'm using zsh) Read more about globbing, e.g. glob(7). Details could be shell-specific. But understand that ...


12

Your problem is not nested loops, per se. It's that one or more of your inner loops is running in a subshell. This works: #!/bin/bash for i in $(seq 1 100); do echo i $i for j in $(seq 1 10) ; do echo j $j sleep 1 [[ $j = 3 ]] && { echo "I've had enough!" 1>&2; exit 1; } ...


12

If you use bash, this command should work. mv a $'b\nc'


10

Creative and involving signals, you say? OK: trap on_exit EXIT trap on_usr1 USR1 on_exit() { history -a trap '' USR1 killall -u "$USER" -USR1 bash } on_usr1() { history -n } Chuck that in .bashrc and go. This uses signals to tell every bash process to check for new history entries when another one exits. This is pretty awful, but it ...


7

What do you do after you've parsed the -i option? There are more arguments to parse. If -i was the first argument, then after processing it, you need to look at the third argument. In the old version of ssh-copy-id you're looking at, -i is the sole option and the argument after that is the target host name. If -i is not passed, the target host name is the ...


6

I know you asked for a mv solution, however, despite the warning, this can be easily done with rename (in the Perl package): ~/tmp$ touch foo ~/tmp$ rename 's/$/\nbar/' foo Unsuccessful stat on filename containing newline at /usr/share/perl5/File/Rename.pm line 69. ~/tmp$ ls foo?bar


5

To be on the safe side, include the slash. This can lead to multiple slashes when concatenating the paths, but at least you avoid problems. A few examples: rsync treats paths differently if the trailing slash is included (it synchronizes that directory instead of making another subdirectory). Symbolic links to directories sometimes behave in an unexpected ...


5

According to POSIX: Definition of a pathname: A string that is used to identify a file. It has optional beginning < slash > characters, followed by zero or more filenames separated by < slash > characters. A pathname can optionally contain one or more trailing < slash > characters. Multiple successive < slash > characters are considered ...


5

If you have the perl version of rename (named prename on some distributions): find . -type f -mindepth 2 -name '*.jpg' -exec rename -n 's!/([^/]+)$!_$1!' {} + This will only show you what the command will do. If you're happy with that, drop the -n option to actually rename the files (the actual rename doesn't print anything).


4

I think your best bet will be to use a shell loop instead of xargs: Then you can control how commands are sent the filename argument. find . -type f -print0 | while IFS= read -rd "" filename; do type=$( file --brief "$filename" ) if [[ $type == *image* ]]; then identify -format "%[fx:w*h] %i\n" "$filename" fi done


4

One aspect of this problem isn't really about awk - and only a little bit about the shell. The problem is that on a standard, canonical tty most of the time the kernel's tty discipline is buffering your input - just echoing it to your screen and nowhere else - so that it can efficiently handle backspacing and such-like. However, when you press return or ...


3

No, you should not. It adds an extra unnecessary slash (/). example say you want to export java's bin directory to your PATH variable , export PATH=$PATH:/opt/jre1.7.0_45/bin/ now check it, user@host:~$ which java /opt/jre1.7.0_45/bin//java notice the extra slash (/) before java, but fortunately it just works in such case.


3

Use for: for i in */; do (cd "$i" && sh *ld); done


3

for i in `cat servlist`;do echo $i;ssh $i 'sudo usermod -c "New Comment" user';done or for i in `cat servlist`;do echo $i;ssh $i "sudo usermod -c \"New Comment\" user";done


2

As mentioned in the answer on SO, here is a way to check: if [ -z ${somevar+x} ]; then echo "somevar is unset"; else echo "somevar is set to '$somevar'"; fi where ${somevar+x} is a parameter expansion which evaluates to the null if var is unset and substitutes the string "x" otherwise. Using -n, as suggested by the other answer, will only check if the ...


2

Chet Ramey says: in POSIX mode, the vi editing mode should not map tab to complete. This is because POSIX.2 completely specifies the behavior of the vi editing mode, and the standard requires tab to be mapped to self insert by default. Add the following line to ~/.inputrc: TAB: complete Or you can add bind 'TAB: complete' to ~/.bashrc


2

man itself is a tool to format manual pages, not to browse them. The tool you use to browse the man pages is a pager. The default pager on most systems is less. In less, when you press / or ? to search, a few characters have a special meaning if you type them at the beginning of the search expression. The exclamation mark is one of them. To avoid ! having a ...


2

Except the IP2Dec and dec2ip script, which need to be performed only on part of the file, it should work. Here an example of dec2ip which applies the function only on the first entry of the line. #!/bin/bash dec2ip () { local ip dec=$@ delim="" for e in {3..0} do ((octet = dec / (256 ** e) )) ...


2

You could use the -exec sh -c '...' construct with find: find . -type f -exec sh -c 'file --brief --mime-type "$0" | \ grep -q ^image/ && identify -format "%[fx:w*h] %i\n" "$0"' {} \; or with exiftool: exiftool -q -if '$mimetype =~ /image/' -p '$megapixels $directory/$filename' -r .


2

As steeldriver pointed out, your problem is not awk, it's file. There is no NUL in the input you are giving to awk because file ate it. I would do this whole thing in the shell instead: find . -type f -print0 | while IFS= read -r -d '' file; do file --mime-type "$file" | grep -qP "\bimage/" && printf '%s %s\0' $(identify -format ...


2

Looks like this code is where the problem lies: test-host: @if [ $$EUID -ne 0 ] ; then \ echo "You must be logged in as root." && exit 1 ; fi @if ! type -p gawk >/dev/null 2>&1 ; then \ echo -e "Missing gawk on host!\nPlease install gawk and re-run 'make'." && exit 1 ; fi When the first if … is ...


2

You could give the shell some arguments, so that it starts slightly differently. eg. /bin/sh -c "cd /mnt/flash;exec /bin/sh" Starting with -c which will execute commands in following string. First command is the directory change, followed by exec which will start a new shell (in the same process) which is now starting in your desired directory. Update: ...


2

Try this, fixes the cp parameter order and limits to just root filesystem rather than trying to traverse /proc and the like. find / -xdev -type f -perm a+r -exec cp {} /home/student/abc \;


2

One way is to look for one of the file names (pick the rarest one if you know which one it is) then filter the matches to retain only those where the other file exists. find . -name foo -exec sh -c '[ -e "${0%/*}/bar" ] && [ -e "${0%/*}/buz" ] && echo "${0%/*}"' {} \; You can use the shell snippet's return code if you want to use a find ...


2

You can run it in the shell, for example like: $ for i in $(find /etc -type f); do wc -l $i; done This runs through /etc and gives out the line count of each file. Added according your comment: $ if [ "hi" == "hi" ]; then echo "this is true"; fi


1

You can do it with find: $ find -maxdepth 1 -not -name subdir -print0 | xargs -0 mv -t subdir Where: -maxdepth 1 restricts the find traversal to the current directory (not a POSIX option, but e.g. available in GNU find) -print0 tells find to terminate lines with 0 bytes - thus, making the command work with unusual filenames (e.g. ones containing ...


1

You need to use full paths to the output files and probably to the executables as well. When run in cron, there is no pre-existing environment for it to know things like a working directory or a path.


1

SSH executes the remote command in a shell. It passes a string to the remote shell, not a list of arguments. The arguments that you pass to the ssh commands are concatenated with spaces in between. The arguments to ssh are sudo, usermod, -c, New Comment and user, so the remote shell sees the command sudo usermod -c New Comment user usermod parses Comment ...



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