Tag Info

Hot answers tagged

15

If you know you are dealing with two integers a and b, then this simple shell arithmetic expansion using the ternary operator is sufficient to give the numerical max of them: $(( a > b ? a : b )) E.g. $ a=10 $ b=20 $ max=$(( a > b ? a : b )) $ echo $max 20 $ a=30 $ max=$(( a > b ? a : b )) $ echo $max 30 $


13

bash defines several metacharacters. From man bash: metacharacter A character that, when unquoted, separates words. One of the following: | & ; ( ) < > space tab Because metacharacters separate words, it does not matter whether they are surrounded by spaces. The pipe symbol, |, is a metacharacter and hence, as you ...


10

sort and head can do this: numbers=(1 4 3 5 7 1 10 21 8) printf "%d\n" "${numbers[@]}" | sort -rn | head -1 # => 21


8

This sounds like a task for join: join -t":" -o "1.1,1.2,1.3,1.4,1.5,2.1,2.2,2.3" \ -j 2 <(sort -k2,2 -t: test1) <(sort -k2,2 -t: test2) Output: Julian:Brude:Other:Other:Other:Jb:Brude:kemin Robert:Dillain:Other:Other:Other:R:Dillain:bodent Megan:Flikk:Other:Other:Other:Mb:Flikk:kentin Jesus:Kimmel:Other:Other:Other:Jbb:Kimmel:verlin ...


7

You can compare just two numbers with dc like: dc -e "[$1]sM $2d $1<Mp" ... where "$1" is your max value and "$2" is the number you would print if it is lesser than "$1". That also requires GNU dc - but you can do the same thing portably like: dc <<MAX [$1]sM $2d $1<Mp MAX In both of the above cases you can set the precision to ...


7

Your script is attempting to combine two interpreters. You have both #!/bin/bash and #!/usr/bin/expect. That won't work. You can only use one of the two. Since bash was first, your script is being run as a bash script. However, within your script, you have expect commands such as spawn and send. Since the script is being read by bash and not by expect, ...


5

If your shell supports the ksh ${var/search/replace} form of parameter expansion (ksh93, zsh, mksh, yash, bash): for r1 in *R1*; do r2=${r1/R1/R2} singles=${r1/R1/singles} trimmed1=trimmed$r1 trimmed2=trimmed$r2 sickle pe -f "$r1" \ -r "$r2" \ -o "$trimmed1" \ -p "$trimmed2" \ -s "$singles" done POSIXly, you could do ...


5

This is simple task for awk: awk -F':' -vOFS=':' 'NR==FNR{a[$2]=$0;next}{print $0,a[$2]}' file2 file1 First we set : as field separator both for input (with -F) and output (with OFS) then if first file is processed (file2) we assign whole line to table element indexed with second field. When next next file (file1) is processed we print its lines adding ...


4

Too long for a comment: While you can do these things e.g. with the sort | head or sort | tail combos, it seems rather suboptimal both resource- and error-handling-wise. As far as execution is concerned, the combo means spawning 2 processes just to check two lines. That seems to be a bit of an overkill. The more serious problem is, that in most cases you ...


4

compare file modification times with test, using -nt (newer than) and -ot (older than) operators: if [ "$file1" -ot "$file2" ]; then #do whatever you want; fi


4

I haven't tried it, but find should be able to handle the whole operation just fine: $ find dir/ -type f ! -newer reference -delete ... or... $ find dir/ -type f ! -newer reference ! -name reference -delete Basically: ! -newer reference matches files which have been modified less recently than reference. -delete deletes them. ! -name reference ...


4

Your code can be a lot more concise: #!/bin/bash read -p "Enter file name: " filePath if ! [[ -r $filePath ]]; then echo "cannot read $filePath" exit 1 fi PS3="Where you want to copy? " select host in host1 host2 host3; do if [[ -n $host ]]; then expect <<END spawn scp "$filePath" uname@$host:/usr/tmp ...


3

The <(...) statement in bash is process substitution. The process in <(...) is run with its input or output connected to a FIFO or some file in /dev/fd. See it with: echo <(echo foo) It prints something like /dev/fd/63. That is the file descritor. The <(...) part is then replaced with that file descriptor. So in your statement the call would ...


3

You can define a function as function maxnum { if [ $2 -gt $1 ] then echo $2 else echo $1 fi } Call it as maxnum 54 42 and it echoes 54. You can add validation info inside the function (such as two arguments or numbers as arguments) if you like.


3

Two brace expansions do work, they just don't work the way you want them to: $ touch abc $ mv {a,}b{c,d} mv: target `bd' is not a directory $ echo mv {a,}b{c,d} mv abc abd bc bd They are expanded separately - effectively the first one is expanded, leaving you with mv ab{c,d} b{c,d} and then the second is expanded, leaving you with mv abc abd bc bd.


3

You could try to remove the call to greeter from your .bashrc, and put it in ~/.bash_profile. It's only called with login shell.


3

ls and many other programs detect whether their output (or sometimes input) is attached to a terminal by calling the C function isatty(STDOUT_FILENO). If output is anything else than a terminal (like a pipe or a file), it defaults to an output format that is more program-friendly.


3

From Debian's bash README: What is /etc/bash.bashrc? It doesn't seem to be documented. The Debian version of bash is compiled with a special option (-DSYS_BASHRC) that makes bash read /etc/bash.bashrc before ~/.bashrc for interactive non-login shells. So, on Debian systems, /etc/bash.bashrc is to ~/.bashrc as /etc/profile is to ...


3

So you can get explicit about the way the shell goes about locating commands in a few different ways. You can use... command command_name ...to instruct the shell only to invoke the command_name if it is a $PATHd executable. This is important in cases like yours because, when you've finished alias A refers to alias B which refers to alias A usually ...


3

A long, long time ago: This document details the changes between this version, bash-2.05a-rc1, and the previous version, bash-2.05a-beta1. Changes to Bash … w. Bash no longer auto-exports HOME, PATH, SHELL, or TERM, even though it gives them default values if they don't appear in the initial environment. I don't know what ...


2

You can't use the wildcard as part of the variable if you are also using quotes. Note that in the array variable syntax @jasonwryan suggested, it is looking at the contents of the directory at the time the array is initialized, rather than when the loop is executed. #!/bin/sh DIR='/home/user/.gvfs/analysis$ on ...


2

Maybe by storing the output in variables and writing these variables to the file? output1=`ls -tr | tail -n1` output2=`head -n3 bla-1357135486.xml | awk -F 'scheduleName' '{ print $2 }' | sed -r 's/^.{1}//' | sed -r 's/.{1}$//'` echo "$output1 $output2" > /tmp/output.file


2

With sed you can probably do: sed 's|[^:]*:\([^:]*\).*|/^[^:]*:\1:/s/$/:&/;t|' file2 | sed -f - file1 ...which would involve one sed process reading the second file and writing a sed script for editing the first into a second sed's stdin. As near as I can tell you shouldn't have any problem with directly injecting the contents verbatim into a regexp ...


2

Something like t=1 until [ "$t" -le 0 ] do t=$(($(date -d '2015-02-24 16:40:00' +'%s')-$(date +'%s'))) sleep 1 tput clear echo $t done


2

Just include an actual newline inside the quotes (this works with either single or double quotes). Note that if the second line is indented, the spaces are part of the string. Furthermore always use double quotes around variable substitutions. str="deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free deb-src http://ftp.cn.debian.org/debian/ ...


2

I'm pretty sure you just want sudo's little-k option, sudo -k vim ~/thefile which is documented to completely ignore your cachefile: When used in conjunction with a command or an option that may require a password, this option will cause sudo to ignore the user's cached credentials. As a result, sudo will prompt for a password (if one ...


2

There isn't an option to sudo that will do exactly what you want, but you can make a shell function that will create a new command sudok, which will run the sudo command and then have sudo remove its cached credentials. function sudok () { sudo "$@"; sudo -K; } Add that line to your ~/.bashrc or ~/.bash_profile to make it permanent.


2

Without ls, since you're just populating its list with shell globs anyway, you can cut out the middle-man like: glob_hsli()(IFS=.;set +f set -f -- '' hsli*.*.h5 for h5 do case ${h5#*.} in (*[!0-9]*.*|.*|'') : ;; (*) set $h5 "${1:-0}"; shift $((3>>($2>$4)));; esac;done printf "0.%d\n" "${1:?No Match Found!}" ...


2

First, your snippet executes the command echo {} : ;if [ -f {} ]; then echo file; else echo directory;fi because it needs its output to evaluate the command substitution. Since there is no file named {}, this produces the output {} : directory Then the find command is executed with the arguments -exec, echo, {}, :, directory, so for every file, it ...



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