Tag Info

Hot answers tagged

22

read reads a whole line from standard input, splits the line into fields and assigns this fields to the given variables. If there are more pieces than variables, the remaining pieces are assigned to the last variable. In your case $a is assigned 1, $b is assigned 2 and $c the remaining 3 4 5 6.


16

Rewriting the loop this way reveals what is happening: echo '1 2 3 4 5 6' | while read a b c do echo '(iteration beginning)' a="$a" b="$b" c="$c" '(iteration ending)' done This gives, as its output: (iteration beginning) a=1 b=2 c=3 4 5 6 (iteration ending) Notice first that only a single echo command is run. If it were run more than once, you ...


13

Nice question! The script below creates a logfile: ~/viewport_log.txt in your home directory, where it reports the current session's viewport (workspace) usage time per viewport. The report is updated once per two seconds, looking like (in a quick run): workspace1 0:00:24 workspace2 0:00:05 workspace6 0:00:04 workspace8 0:00:05 in the format ...


7

Try the script below. It will detect the size of the terminal for every input word so will even dynamically update if you resize the terminal while it's running. #!/usr/bin/env bash ## Change the input file to have one word per line tr ' ' '\n' < "$1" | ## Read each word while read word do ## Get the terminal's dimensions height=$(tput lines) ...


7

Here you're a very robust bash script for do that: #!/bin/bash ## When the program is interrupted, call the cleanup function trap "cleanup; exit" SIGHUP SIGINT SIGTERM ## Check if file exists [ -f "$1" ] || { echo "File not found!"; exit; } function cleanup() { ## Restores the screen content tput rmcup ## Makes the cursor visible again ...


7

In order to save the output of a command in a variable, you must enclose the command in backtics (`command`) or, better, in $() ($(command)). You are using single quotes which means that you are saving the string wc $FILE and not actually running wc: $ foo='wc /etc/fstab' ## WRONG $ echo $foo wc /etc/fstab $ foo=`wc /etc/fstab` ## RIGHT $ echo $foo 23 96 ...


7

Using the "raw output" mode of sensors for easier scripting: -u Raw output. This mode is suitable for debugging and for post- processing of the output by scripts. It is also useful when writing a configuration file because it shows the raw input names which must be referenced in the configuration file. For example: $ ...


6

Use the following grep expression: grep -vFf file_B file_A Here is a test: $ cat file_A bird, snake dog, cat rabbit,fox eagle,dove $ cat file_B dog dove $ grep -vFf file_B file_A bird, snake rabbit,fox -f will read the patterns from a file (one per line), file_B in this case -F will consider the patterns read from file_B as fixed string, ...


6

First install mediainfo with: sudo apt-get install mediainfo You can now use the following oneliner to get the total video time of a directory: find . -type f -exec mediainfo --Inform="General;%Duration%" "{}" \; 2>/dev/null | awk '{s+=$1/1000} END {h=s/3600; s=s%3600; printf "%.2d:%.2d\n", int(h), int(s/60)}' The find command will call mediainfo ...


5

I think that the correct code may be this: #!/bin/bash function count() { word_count=$(wc -w < "$FILE") zenity --info --title="Word Counted" --text="Counted words $word_count" } function choose() { FILE="$(zenity --file-selection --title='Select a File')" case $? in 0) count;; 1) zenity ...


5

As I mentioned in my comment there are some vital pieces missing here and these commands together don't quite make sense, but I will go over them. if [ ! -w "." ]: . represents the current directory. -w tests if it is writeable. ! negates the test (so the statement returns true if the directory is not writeable vs the other way around). ps -d | grep ...


5

A simple way of doing that is to add those lines to rc.local in your system. For that you need root or sudo rights. You can edit the file with your favourite text editor, eg vim: vim /etc/rc.local (sleep 20 echo 'cpu limit bomi player at 40%' cpulimit -v -e bomi -l 40) & The first line tells the the computer to wait 20 seconds, the other 2 lines are ...


4

You can use sleep and clear commands in your script as following: for word in $(< read) do echo "$word" sleep 1 clear done Explanation: The sleep command make delay for a specified amount of time (in seconds). With sleep 1 delay would be for 1 second. You can change for more time delay by incrementing the second parameter or for delaying ...


4

With bash, I'd do: while IFS=$'\n' read -r line do printf "%-${COLUMNS}s\r" "$line" sleep 1 done < file By using the carriage return (\r) instead of line feed (\n), you can overwrite the current line. The -${COLUMNS} pads the output with spaces so that previous lines are completely overwritten. For a per-word solution, I think a double loop is ...


4

You can use the following script to know Total Duration of all video files in a Directory recursively. I have used avprobe in the following script which comes with libav-tools Install libav-tools as, sudo apt-get install libav-tools Save the script as get_video_duration.sh(say). Give it execution permission from a terminal as chmod u+x ...


3

There is a fi that should not exist in the line 114. The correct code would be this: #!/bin/bash # Bucket brigade - brigade script # The user defines a fire # The user defines number of passers # Then passers extinguish the fire if [[ "$2" = *[!0-9]* ]]; then echo "The second argument may only contain digits" exit fi # $2 must be empty or ...


3

You need to change the quotes around the grep command for backticks: for i in `grep -l matchpattern "$firstdir"/*`; do Or adopt the new-style $(): for i in $(grep -l matchpattern "$firstdir"/*); do Source: What's the difference between $(stuff) and `stuff`?


3

When writing scripts, if you have an error, you need to echo your variables to understand what is happening. This is the first step in debugging. Had you done so, you would have seen that the grep command is not being executed and is instead being saved as $i: $ for i in 'grep -l matchpattern $firstdir/*'; do echo "i is: $i"; done i is: grep -l ...


3

You would be much better off if your input is sorted on the first column, since then you can directly use the join command: $ cat foo a 1 b 2 c 3 d 4 $ cat bar b 1 d 4 e 5 n 2 $ join -t $'\t' <(sort foo) <(sort bar) b 2 1 d ...


3

Your description seems odd: If you get just proj1, how do you know what its parent directory is? Anyway, to give you what you asked for: # for me, this takes a looooooong time [1]. Do it once and cache it mapfile -t _all_dir_names < <( find ~ -type d -printf "%f\n" ) # the function you want completion for myfunc () { echo hello world $*; } # the ...


3

bash function to do the same mpt() { clear ; w=$(( `tput cols ` / 2 )); h=$(( `tput lines` / 2 )); tput cup $h; printf "%${w}s \n" "$1"; tput cup $h; sleep 1; clear; } and then mpt "Text to show"


3

If you're looking for a system command that always returns a non-zero exit code, then /bin/false seems like it should work for you. From man false: NAME false - do nothing, unsuccessfully SYNOPSIS false [ignored command line arguments] false OPTION DESCRIPTION Exit with a status code indicating failure.


3

The NUM=${1:-0} line means that the variable NUM is set to $1 if a parameter is passed to the script, and to 0 if no parameter is passed at all. That explains why you have no output at all; the threshold is always set to 0 if the script is executed without passing a parameter to it, e.g.: bash <script_name> *<script_name> = name of your bash ...


3

Improved and commented code: #!/bin/bash num=${1:-undefined} # If $1 (the first argument passed to the script) is set, then num=$1, else num=undefined. cmd=$(which {banner,echo} | head -1 | xargs basename) # If banner is installed, then cmd=baner, else cmd=echo. until [[ "$num" =~ ^[0-9]+$ ]]; do # ...


2

With bash: Assuming file1: abc123 123456 def456 789123 ghi789 456789 And file2: abc123 789123 This will output file3: abc123 123456 789123 #!/bin/bash while read line1; do var1=$(echo $line1 | tr '\t' '\n') while read line2; do var2=$(echo $line2 | tr '\t' '\n') if [ "${var1[0]}" == "${var2[0]}" ]; then printf ...


2

#!/bin/bash printf "Type an integer number: " && read NUM if [ $NUM -gt 0 ] then while [ $NUM -ge 0 ] do if [ -f /usr/bin/banner ] then /usr/bin/banner "$NUM" else echo $NUM fi NUM=$(($NUM-1)) sleep 2 done fi output: :~$ ./countdown.sh Type an integer number: 10 10 9 8 7 6 5 4 ...


2

Here are your errors (source): SC2002 Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead. SC2006 Use $(..) instead of legacy ... SC2046 Quote this to prevent word splitting. SC2086 Double quote to prevent globbing and word splitting SC2126 Consider using grep -c instead of grep|wc. 1 #!/bin/bash 2 #Script to create unique and ...


2

You can create a new return code with the command bash -c "exit RETURNCODE", replacing "RETURNCODE" with any number. Note that it will be trimmed to an 8bit unsigned integer (0...255) by (RETURNCODE mod 256) You can check the return code of the last shell command inside the terminal(!) with executing echo $?. The "$?" variable contains the most recent ...


2

You could do something like sensors -A | grep -oP '^Core.+? \+\K\d+' | awk '{k+=$1}END{print k/NR}' The grep will print only the relevant numbers (the spaces ensure that only the actual temperature is printed, not the critical or anything else) and the awk does the calculation. NR is the number of lines so that will work if the number of cores changes.


2

echo firstOutput read -s command0 & sleep 8 command1a & command1b & command1c & sleep 5 echo secondOutput command2



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