Tag Info

Hot answers tagged

9

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) ...


6

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 ...


6

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 ...


5

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

This should work: loadval=$(sar -u 1 10 | awk '{print 100-$8}')


5

This will make you clear (Check the Whitespaces): $ var=c $ ["$var" == "c"] && echo "OK" [c: command not found $ ["$var" == "c" ] && echo "OK" [c: command not found $ [ "$var" == "c"] && echo "OK" bash: [: missing `]' $ [ "$var" == "c" ] && echo "OK" OK So, you need to put whitespace prior to and after the condition ...


4

there is one simple error on line 6 and 9. There should be space between [ and $response in if [$response = "y"]then on line 6; . Similarly, for [ and $response on line 9` Also you must put double qoutes around $response to avoid errors in case user enters a input with space.


4

Here is a suggestion on how to do it: First you iterate over the folders, inside every folder you search and find the files and copy them to its parent folder. #!/bin/bash #Iterate over the folders for folder in main/* do #Copy the files into the folder find $folder -name "*.jpeg" -type f -exec cp {} $folder \; -print done #After its done: ...


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

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

You can try creating a .desktop file in /usr/share/applications (or ~/.local/share/applications, if only for one user) like explained here (works for other desktops too). To link this program to .asm files you first have to create a mimetype for .asm files. In order to do so you have to add/modify the line text/x-asm asm in ...


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

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 ...


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

I've made this: #!/bin/bash ## Restores the screen when the program exits. trap "tput rmcup; exit" SIGHUP SIGINT SIGTERM ## Saves the screen contents. tput smcup ## Clears the screen. clear ## Loop through available interfaces. while read interface; do # While reads a line of the output i=$((i+1)) ...


2

You should be able to use grep grep -F 'z.z.z.z' logfile > results The -F (--fixed-strings) argument prevents the period separators from being interpreted using regular expression syntax (which would match 'any character') but rather as literal periods.


2

Also, the set command is more exotic than you think.  You don’t use the set command to set variables. You know that, when you invoke a shell script with parameters, that they get assigned to $1, $2, $3, …; right?  Well, one of the things the set command does is to set $1, $2, $3, … for the current  (interactive) shell.  So, for example, % set ...


2

If you're using bash, there's no need to invoke an external command such as cut for this: you can use the shell's built-in parameter expansion functions. For example to remove the longest trailing string starting with / from variable str='1000/1.0' and assign the result to variable str1 str1="${str%%/*}" So $ str='1000/1.0' $ str1="${str%/*}" $ echo ...


2

Using the df command to keep track of the disk space, and the lsblk command to keep track of the mounted drives, the script below, run in the background, will log changes in the free space of all mounted drives. It creates a log file: ~/disklog where it writes changes to (in k). If you run it in the terminal, it will output the result simultaneously. The ...


2

tr takes input from STDIN (standard input). Try this: $ tr '\n' ':' > output_file < input_file You have used a syntax for Bash's parameter expansion but you are using a file as a parameter which is not correct as the parameter can only be variable or a special shell parameter. To do what you are trying using only bash: $ var="$(<bash_list)" $ ...


2

The sleep command make delay for a specified amount of time (in seconds). With sleep 1 delay would be for 1 second (one word per second). You can change for more time delay by incrementing the second parameter or for delaying less than 1 second divide it to low units; Like sleep .1 for 1/10 second delay or sleep .001 for 1/1000 second delay and etc. So if ...


1

Ok, I figured it out. I created 2 scripts, the first runs the script in screen: #!/bin/bash screen -S server $HOME/rubix/start.sh The next is what the first runs, which you can see above.


1

You need to have the right permission (read+execute) on the directory containing the executable too. If you have a script named script.sh in /home/foo/ and it has the octal permission of 775. Now if you want to run the script as a different user e.g. bar, then bar needs to have read and execute permission on /, /home and /home/foo directories otherwise ...



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