Hot answers tagged

31

moreutils' chronic command does just that: chronic mycommand will swallow mycommand's output, unless it fails, in which case the output is displayed.


6

Usually in case of error the command outputs messages to stderr so for you task you can just supress stdout mycommand > /dev/null


5

I'm going to assume that what you've posted is a sample, because it isn't valid XML. If this assumption isn't valid, my answer doesn't hold... but if that is the case, you really need to hit the person who gave you the XML with a rolled up copy of the XML spec, and demand they 'fix it'. But really - awk and regular expressions are not the right tool for the ...


4

You can filter it in find by using -mindepth option. Like this: function enterDIR { find . -mindepth 1-maxdepth 1 -type d | while read DIR ; do #recurse into every directory below top-level directory cd "$DIR/" enterDIR done createDirectory convertFLAC } But the whole script doesn't look like a good ...


4

To make your own chronic my_chronic() { tmp=$(mktemp) || return # this will be the temp file w/ the output "$@" > "$tmp" 2>&1 # this should run the command, respecting all arguments ret=$? [ "$ret" -eq 0 ] || cat "$tmp" # if $? (the return of the last run command) is not zero, cat the temp file rm -f "$tmp" return "$ret" # return the ...


4

You can use rsync linux utility. You need to specify source and destination,it works locally as well as remotely. Please check man rsync for more details. For solaris, you'll need to install it.


4

With zsh: dirs=(test/{1..10}/{1..15}) n=0 mkdir -p $dirs && for d ($dirs) {:>$d/$(([##36]n%26+10)); ((n++))} The trick above being to use base36 numbers where digits 10 to 35 are expressed with A.. Z. Would be a bit more legible and generalisable with: dirs=(test/{1..10}/{1..15}) n=0 l=({A..Z}) mkdir -p $dirs && for d ($dirs) : > ...


4

Here is a solution built around the inotifywait utility. (You could use incron too, but you'd still need code similar to this.) Run this at boot time, for example from /etc/rc.local. #!/bin/bash # cd /path/to/samba/folder # Rename received files to this prefix and suffix prefix="some_prefix" suffix="pdf" inotifywait --event close_write --format "%f" ...


4

The venerable Gnu Screen can do what you want, but it can't attach to an already running gnome-terminal. You'll need to start screen at work if you want to pick it up later at home. See Session Management specifically.


4

You could maybe use shuf (from the GNU coreutils package), which generates permutations rather than individual random samples - something like for f in *; do read i; echo mv -- "$f" "file-$i"; done < <(shuf -i 1-10) or (perhaps better) shuffle the filenames - and then simply rename them sequentially i=1; shuf -z -e -- * | while IFS= read -rd '' f; ...


3

getline reads in the next line. You don't need to use it. The current line is already in $0: awk '{print NR, $0}' name_list.txt


3

From man grep -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) So it's just checking for the presence of = as a literal string in $line


3

I do something like this in my makefiles: if (mycommand) &> mycommand.log; then echo success else c=$?; echo;echo -e "Bad result from previous command, see mycommand.log for more details";echo; command_to_run_on_fail (exit $c) fi Adapting that to your situation, you could do something like this: if ! (mycommand) &> ...


3

example.sh script to xcalc "4*9=", and view capture-result image: #!/usr/bin/env bash save="$DISPLAY" # save original X display number export DISPLAY=:44 # set random choosen display for xvfb case "$1" in # and x-programs called below start) Xvfb $DISPLAY & ;; ...


3

If the filename is in a variable, the canonical way to remove a preffix is: removepre="foo bar " filename="foo bar XXX doo par.jpg" filename="${filename#"$removepre"}" echo "$filename" The problem with the suffix you present is that there is an extension that you want to preserve, so it becomes a bit longer: removesuf=" doo par" filename="foo bar XXX doo ...


2

From man top: -b : Batch mode operation Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.


2

First: Create a script. You can call it whatever you want. I will call it downloader.sh. #!/bin/bash PROTOCOL="ftp" URL="server.example.com" LOCALDIR="/home/user/downloads" REMOTEDIR="dir/remote/server/" USER="user" PASS="password" REGEX="*.txt" LOG="/home/user/script.log" cd $LOCALDIR if [ $? -dt 0 ]; then echo "$(date "+%d/%m/%Y-%T") Cant cd to ...


2

Sort of. You don't need -exec ls -l {} \;, the find command already lists the files. If you want to list them with more details, you can use find -ls. There's nothing wrong with -exec ls ... either, it's fine if you prefer that, just not needed. The -mtime N will find files that were modified exactly N days ago. The details are in man find: +n for ...


2

For automatic login, the way to go is using key-based authentication. A nice tutorial here You can then use the default one (the one in the ~/.ssh/id_rsa ) or use another key passing -i MyKeyFile parameter to ssh.


2

The $HOME variable isn't something you should mess with, generally; it's user-dependent. You don't want the user to suddenly have a different home directory! (And if you do, you should edit his home directory that is set in /etc/passwd—which of course requires root permissions.) The $PATH is another matter and should be set in his ~/.bashrc or ...


2

Two points: find "ignores fractional parts". I guess it calculates the number of hours, divides by 24, and integerizes the result (discards the fraction). So -mtime 0 checks a file, compares the mtimes, converts to hours, divides by 24. If the integer part of that result is 0, it's a match. That means 0.99999 hours ago will match. Then -mtime +0 matches any ...


2

find . -type f -printf %u:%g\\n | awk '{usergroup[$0]++}; END {for(key in usergroup) printf "%-20s %d\n",key,usergroup[key]}' hl:hauke 401 hl:1003 10 root:root 1 find prints for every file a line of the kind username:groupname. awk uses an associative array to count the occurrences. For every line the variable with the ...


2

What you can do is to (if not already done) generate a set of public and private ssh keys on your machine for your user with: $ ssh-keygen Answer the questions in order to generate the set of keys. copy your public key to the remote host: $ ssh-copy-id remote-user@remote-host This will enable login-in from your username@host to ...


2

Not bash but awk, and a one-liner at that. echo {A..O} | xargs -n 1 | awk '{system("mkdir "NR" && touch "NR"/"$1)}'


2

Why not something like the following? user="foo" group="bar" perm=741 for dir in $(cat dirname.txt); do mkdir ${dir} chown ${user}:${group} ${dir} chmod -R ${perm} ${dir} done I find it best to utilize variables to make future changes easier. Iterating the directory list instead of a one liner is easier to maintain as well


2

echo /test/{1..10}/{1..15} " " | ( chars=({A..Z}); i=0; while read -d " " dir; do test -z "$dir" && break; echo mkdir -p "$dir"; echo touch "${dir}/${chars[i]}"; ((i++)); i=$((i%26)); done )


2

for f in ~/1/2/*/*_1.txt; do file_without_path="${f##*/}" exptool input1= "${file_without_path}" input2= "${file_without_pathf%_1.txt}_2.txt" done


2

There are scripts for html2text, but i think what you are looking for is xpath (e.g. xmllint supports it) to get the content of some elements of your html page.



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