Tag Info

Hot answers tagged

18

You want the output of who | wc -l assigned to w, not that string, which is what you get because of the quotes around it. You should use command substitution $(...): w=$(who | wc -l) echo "There are $w people online at the moment" (you can also use the backquotes, but you cannot easily nest those).


9

Another solution: echo There are $(who | wc -l) people online at the moment


8

Some reasons I can think about why they used find + xargs: Handling the case when you have too many cache files, leading to an error if you run only one rm command. Globbing * does not expand hidden files. Working recursively. But this find + xargs is not efficient, since when they didn't add any filter, so find result will contain directories along with ...


8

Why use awk at all? date +"%b %d %Y" gives you the values without the hassle.


7

There are a few differences in the behavior of the command lines: The find command line would delete files recursively in subdirectories, the rm command line wouldn't. You need to consider whether or not you want to recurse. The find command line would delete all files, if possible. The rm command line might skip files based on the shell's settings like ...


6

Here's a roughly modified version of your script: $ more cmd.bash #!/bin/bash echo "Whose phone number do you want to know?" read name number=$(grep "$name" telephone | cut -d';' -f2) echo '' echo "The phone number of $name is $number." It works as follows: $ ./cmd.bash Whose phone number do you want to know? Hans The phone number of Hans is ...


6

Using awk: #!/usr/bin/awk -f BEGIN { FS=";" } $1 ~ name { print "The number of " name " is " $2 } phones -v name=Jan telephone The number of Jan is 032569874


6

Found this method on AskUbuntu that shows using a crontab entry along with amixer to mute/unmute the sound. It's titled: How do I automatically mute/unmute sound during a certain time period (e.g. night)?. General steps Create a crontab entry $ crontab -e Add entries to crontab 59 21 * * * amixer set Master mute 00 08 * * * amixer set Master unmute ...


5

Try piping the output from the date command instead, like so: $ date | awk '{print $2, $3, $6}' Dec 1 2014 If you truly want to take the output from date using a command similar to yours then you'll need to redirect it using a HERESTRING, aka. (<<<) (assuming a zsh shell or a fairly recent version of ksh93 or bash). $ awk '{print $2, $3, $6}' ...


4

You can do it with AWK. $ awk '{if(max<$1){max=$1;line=$2}}END{print line}' file /opt/oracle/app/oracle/product/12.1.0 Here first column of each line is compared with the variable max (which is initially 0). If the first column has a value greated that max then the second column is stored in the variable line, this continues for each and every line of ...


4

With grep, filter out just the numbers: grep -Eo '[0-9]+-' file | sort -u | wc -l [0-9] Matches any character between 0 and 9 (any digit). + in extended regular expressions stands for at least one character (that's why the -E option is used with grep). So [0-9]+- matches one or more digits, followed by -. -o only prints the part that matched your ...


4

you should use backtick to execute command w=`who | wc -l` echo "There are $w people online at the moment"


3

You can do this if you are running gpg-agent (and your passphrase is loaded), by looping through the files in your password store and writing them to a separate file. You do have to strip the leading directories from the path ($PASSWORD_STORE_DIR) and the .gpg extension from each of the files in the subdirectories, but otherwise it is straightforward ...


2

name="jan" line="$(grep -i ^"$name" file)" if [ -n "$line" ]; then number="${line#*;}" echo "The phone number of ${name} is ${number}" else echo "There is no phone number for ${name}." fi


2

The other answers miss three points: Don't ever do this: /usr/bin/find /var/www/cache/blah/ |xargs /bin/rm -f >/dev/null 2>&1 because when you have a file with a space in it, rm will attempt to remove two files, with surprising results. If you insist on doing it this way, assuming you are using GNU find and xargs, you want: /usr/bin/find ...


2

Ok, I think I understand what the pathnames in the variables do now. for n in 1 2; do n=$n.txt for f in $(cat "$li/ID$n") do IFS= read -r l <"$in/$f/a/s/t${n%.*}/$n" printf %s\\n "$l" done > "output$n"; done I think that accomplishes what you're after. If so, it does it all with shell built-ins - that is, excepting cat. And speaking of cat - ...


2

sort -k1 -n filename | tail -1 | awk '{print $2}'


2

awk '$1 > max { max = $1; output = $2 } END { print output }' version.log


2

This is why you shouldn't use pure shell scripting. Instead, is better to use something that is meant for the task, like uscan (which is a perl script), instead of doing it yourself: $ uscan --watchfile teamspeak --package TeamSpeak3 --no-download --verbose --upstream-version 3.0.16 -- In test, processing watchfile line: ...


2

Simply you can sort it by using sort command sort -r version.log | head -n1 | awk '{print $2}' Output: /opt/oracle/app/oracle/product/12.1.0


2

You could use something like this: find . -name "*.log" | xargs grep -E 'fatal|error|critical|failure|warning|' This will find every file with .log as extension and apply the grep command.


2

grep -E 'fatal|error|critical|failure|warning|' *.log


2

The [ command should be closed using a ] with a leading space: if [ $string == tweets10_*.tar* ]; then Also, please don't do for x in `ls` Instead, use: for x in * Or, better: for x in tweets10_*.tar and skip the check altogether. You can also extract out the 10_x part more easily: $ a=tweets10_8.tar; echo ${a//[a-z.]/} 10_8 Here, I'm ...


2

Instead of attachmenttype='file $attachment | cut -d\ -f2' you should write : attachmenttype=$(file "$attachment" | cut -d' ' -f2) See http://wiki.bash-hackers.org/syntax/expansion/cmdsubst or to get mime-type : $ file -i "$attachmenttype" | cut -d' ' -f2 text/plain; and decide what you want to do with the file depends of the type.


1

Something along the lines of: name_of_process=$1 ps aux\ | grep $name_of_process\ | grep -v "grep"\ | awk '{print \"{\ \"pid\" : $2,\ \"status\" : $8,\ \"usageCPU\" : $10,\ \"usageMemoire\" : $4,\ \"numFD\" : $some_field_num,\ \"commandline\" : split($NF)[0]\ \":[{ \"pid\" : $2},{\"pid\" : $some_field_num_too }]}' So ...


1

I'm assuming that you are asking this because you want it to be done automatically rather than having somebody tell you "Just turn the volume down when you go to bed". Go to a shell prompt and use "crontab -e" and add two events along this lines of this: 0 21 * * * /usr/sbin/amixer -D pulse sset Master,0 0% 0 8 * * * /usr/sbin/amixer -D pulse sset ...


1

You should pipe the output of date to awk: $ date | awk '{print $2, $3, $6}' 1 Dec 2014 or get date to format it for you: $ date +'%d %b %Y' 01 Dec 2014


1

I'm not sure what it is you're asking exactly. Assuming you mean you want to find the latest version listed in http://dl.4players.de/ts/releases/, you can do this: curl -s http://dl.4players.de/ts/releases/ | \ grep -Po '(?<=href=")[0-9]+(\.[0-9]+){2,3}(?=/")' | \ sort -Vr | head -1 Example output: 3.0.16 Explanation: Note that the directory ...


1

A better way to get local users might be to see if the user has a valid login shell: getent passwd | grep -f /etc/shells Here's something that should work: getent passwd | grep -f /etc/shells | tr ',' ':' | \ awk -F: '{print $1, $5}' | while read USER NAME do echo $NAME:$(chage -l $USER| awk -F': ' '/Password expires/{print $2}') ...


1

In this particular case, you don't need a loop at all: #!/usr/bin/env bash in=/a/b/c li=/a/b/c/d/ g1=/a/s/t1/1.txt g2=/a/s/t2/2.txt files1=$(sed "s#^#${in}/#;s#\$#/${g1}#" $li/ID1.txt | tr '\n' ' ') files2=$(sed "s#^#${in}/#;s#\$#/${g2}#" $li/ID2.txt | tr '\n' ' ') head -qn 1 $files1 > output1 head -qn 1 $files2 > output2 The files1=... used sed ...



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