New answers tagged

1

Using parameter expansion to replace spaces resulting from %-s by dots: #!/bin/bash list=(localhost google.com nowhere) C=1 for M in "${list[@]}" do ping -q -c 1 "$M" >/dev/null machine_indented=$(printf '%-20s' "$M") machine_indented=${machine_indented// /.} if (($?)) ; then printf "(%2d) %s CONNECTION FAIL\n" "$C" "$...


0

I've tried a bunch of tools, including iftop, ntop, iptraf, and of course the very useful built-in netstat -tupln, but the most practical for my use case turned out to be nethogs - it aggregates connections by the originating app, and is the least noisy of all. Installable via: sudo apt-get install nethogs Run as root: sudo nethogs


3

The Careful Approach Try: while IFS== read -r var value do value="${value#\"}" [ "$var" ] && declare "$var=${value%\"}" done <myconf echo "$cli" This produces the output: /opt/Solartis_Monitoring/JON_CLI/bin/rhq-cli.sh How it works while IFS== read -r var value; do This starts a loop. It temporarily sets the field separator, IFS,...


1

There are many problems in your snippet, but basically the syntax [![...]] is not valid, in the bash (and many other shells) [[ is a single command, which cannot be split by any other character. You can negate equality operator instead: if [[ "$LINE" != \#* ]]; then echo yes; fi Take also closer look to spaces surrounding brackets.


0

In the interest of readability, I would do something like: checkrange() { if [[ "$1" -le 3 ]] && [[ "$1" -ge 1 ]]; then return 0 else return 1 fi } L=0 C=0 prompt="Please enter values for L and C >" while ! ( checkrange "$L" && checkrange "$C" ); do read -p "$prompt" L C prompt="Invalid input; please enter new ...


2

There are two streams of output from a process that are generally sent to the terminal: standard output (which is bound to file descriptor 1), and standard error (which is bound to file descriptor 2). This allows for easily separated capture of expected output, and error messages or other diagnostic information that is not generally expected to be output. ...


1

expect is written in the tcl language, so strings containing whitespace must be quoted with double-quotes " not single quotes '. So replace your spawn line with spawn rsync -arvz -e "ssh -p 1690" --protect-args --progress /home/pappu/ "backup@xx.xx.xx.xx:/volume1/56 - Backup Server/pappu" Also, as mentioned by @steeldriver, a carriage-return is written \...


4

Sounds like node is on a non-standard path, or at least not on root's path. There are a couple of ways around this. Use the full path to node Find the full path using which node If say it's in /home/user/bin/node then call sudo /home/user/bin/node app.js Or as @UlrichSchwarz suggested you can combine the two with command substitution: sudo $(which node) ...


0

I don't see a need for a function here. If you do need a function, it might be best to pass the variable as a parameter, rather than attempting to use it as a global variable. However, it seems pretty straight forward to just nest the loops: for mac in $(awk -F";" '{print $2}' macs.txt); do iptables -t nat -A PREROUTING -i eth1 -m mac --mac-source $mac -...


1

Often differences between running a script manually and running it via systemd are due differences in the environment. Before the xdg-open call, add the command env on it's own line, which dumps the environment. Now run the test manually and via systemd. Look for other variables besides DISPLAY that could be causing the difference. By continuing to add ...


1

The sed command would look something like sed -i -e 's/var One =.*/var One = "'$One'";/' -e 's/var Two =.*/var Two = "'$Two'";/' file.js Edit to add: Beware of a gotcha... if the user enters nasty characters like foo/bar as the answer :-) 2nd Edit: If you don't want to allow the bad character then you can abort eg after the read One you can add: if [[ ...


0

To List all PNG and JPEG files which are 7 days old with absolute path. $ find $PWD/ -mtime -7 -print -exec grep -e ".png\|.jpg" {} \; Here $PWD will be added to every file matched.


0

Hope this helps you: #!/usr/bin/expect spawn sftp USERNAME@ip_address:/path/to/folder expect "Password:" send "PASSWORD\n" expect "sftp>" send "put file1\n" expect "sftp>" send "bye\n" or if you don't want to dedicate the whole script to expect: #!/bin/sh expect << 'EOS' spawn sftp USERNAME@ip_address:/path/to/folder expect "Password:" send ...


0

I tried setting up alias for cpulimit -l 50 ffmpeg in .bashrc alias ffmpegl = "cpulimit -l 50 ffmpeg" and then used it in my script with the following code to source aliases shopt -s expand_aliases source /home/your_user/.bashrc Now i can use cpulimit with ffmpeg anywhere inside the script for multiple commands using the alias. Tested on scientific ...


0

There are simpler ways to do this if one can assume that no file names has white space or newlines in its name. In the modern world, one cannot assume this and, besides, it is bad form to write scripts so fragile that they fail if such a legal file name is found. To be safe, use the following (GNU tools may be required): find ../ini/ -type f -printf '%T@ %...


0

When you run an interactive shell it sets the PATH variable according to your "rc" files (e.g .bash_profile). When you run that "non-interactive" ssh command then many of these scripts aren't run and so PATH isn't set. You should include the pathname to pm2 even if it is the current directory (eg ./pm2 or /path/to/pm2) to ensure it is found, or else export ...


5

The manage.py runserver command isn't forking off as a daemon and so the init script is sitting there waiting to finish. You can put a & at the end of the two lines to make them both be backgrounded. python3 "/home/pi/Python Projects/episode_tracker/manage.py" runserver 0.0.0.0:12345 --insecure & python3 "/home/pi/Python Projects/shifts_server/...


3

The reason for the failure can be found in man 2 flock: Locks created by flock() are associated with an open file description (see open(2)). This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of these descriptors. This means that because all ...


3

This approach works: fun()( (flock 9 || { echo >&2 "$BASHPID: FAIL: $?"; exit 1; } echo "$BASHPID begin" sleep 1; echo "$BASHPID end" ) 9>test ) This ensures the file on which the lock is held isn't closed as long as the commands which need to be protected haven't finished. (Obviously you should replace test with something more ...


2

To use shell variables in awk give them to the awk script using -v awkvariable="$shellvariable" instead of trying to paste them into the script itself, i.e. awk -v d="$domain" '$2==d {print $1, "1.0"} $2!=d {print $1,"-1.0"}' If the script in doing the right thing or not, I don't know, but you might want to use ${domain} rather than $domain in the ...


4

The last item on this line more "file.txt" | awk '$2=='$domain' {print $1, "1.0"} $2!='$domain' {print $1,"-1.0"}' >"$number_domain.txt" is probably intended to be >"${number}_${domain}.txt" corresponding to the two variables which your script reads. It would be nice if your script also ensured that the variables are not empty strings. If they ...


1

I don't think find has an option like this, you could build a command using printf and your exclude list: find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f {} + file.txt will have list of files to exclude in find command.


2

Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work: find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d '\n' rm -f Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.


0

echo "Sending files to destination" USER='(username)' PASSWD='(********)' sftp user@ip_address << EOF quote USER $USER quote PASS $PASSWD cd path put file1 bye EOF


-1

echo "ftping files to destination" USER='(username)' PASSWD='(********)' sftp user@ip_address << EOF quote USER $USER quote PASS $PASSWD cd path put file1 bye EOF


0

As Matteo mentioned you are trying to use perl syntax. Should be; for i in $(ls *.DAT); do if [ $(wc -l ${i}) -le 2 ]; then mv ${i} /path/to/ fi done


2

There are many problems: foreach is not a bash keyword: use for if you want to execute a command use $( ... ) and not just the parenthesis the command execution in the parenthesis is not needed you can just use shell expansion for i in *.DAT; do (in general see Why *not* parse `ls`?) to test if a value is less or equal (see man test): if [ $a -le 2 ] ; ...


1

Ok, I came up with this simple one-liner by using bc to compare the values and shell do the rest: $ value=0.5 $ [ $(bc <<< "$value - 0.8 > 0") -eq 1 ] && echo $(bc <<< "$value - 0.8") || echo 0.1 0.1 $ [ $(bc <<< "$value - 0.2 > 0") -eq 1 ] && echo $(bc <<< "$value - 0.2") || echo 0.1 .3 Or using ...


1

You have two options: Modify the Python script, or write a shell script wrapper. To modify the Python script: You should loop around what it is you want to be doing. Install a signal handler to catch the INT signal (sent by Ctrl-C) and TERM signal (sent by plain kill). When the signal is caught, set a variable telling the Python script that it should no ...


1

You can create a cron job to run your command on regular basis. For example if you want to execute the job on every hour you should create the following cron job: 0 * * * * /path/to/your/command >/dev/null 2>&1 If you want to stop the execution of the job type ps aufx|grep yourcommand|grep -v and kill the pid of it.


1

You can try using a watch 'loop' to run the program repeatedly and see the result, or a while loop. Obtain the PID of that process with echo $! and use kill to terminate that PID. For example : while :; do ./test.py; done & echo $! > /tmp/testpy.pid Use kill to terminate program when you want : kill -9 `cat /tmp/testpy.pid`


-1

You can press CTRL + c or send the SIGTERM signal with kill or killall.


-1

Make sure your first line is: !/bin/bash Enter your path to bash if it is not /bin/bash Try running: dos2unix script.sh That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF). More details about the dos2unix command (man page) Another way to tell if your file ...


1

You need: shopt -s extglob Thanks @Stéphane Chazales for pointing me at extglob.


0

If you have the column utility (most free Unices has): $ column -t -s ';' infile.csv >outfile.txt $ cat outfile.txt Column A Company code Company Desc Region Code Region Desc H Hindustan 13 Maharashtra I Unilever 28 Dadra Then it's a matter of inserting the "Column B", "Column C" etc., but it was ...


0

Using your question (slightly modified, with a line-break before the but not aaeiou because of the 'aa') as input: $ cat hehe.txt The problem is to write a script that takes a file as argument and prints all the lines that contain -at least- all the vowels , -regardless of ordering-, but with [no two same consecutive vowels]. e.g aeaiou is allowed, but ...


0

GNU ed supports numerical offsetting of regex addresses, so you can address a range from /pattern/+1 to the end of the file $. Then you can delete any lines matching the same regex within that range as g//d. Finally write the modified file and quit. ed file << EOF /^yyyy-mm-dd/+1,$ g//d wq EOF or equivalently printf '/^yyyy-mm-dd/+1,$ g//d\nwq\n'...


2

This is not command substitution, it's variable substitution. You aren't assigning a command into a variable, you're assigning a string. The command runs when you use the variable, not at the time of the assignment. g=date stores the string date in the variable g. If you then run echo "$g", this prints the value of g, i.e. date (followed by a newline), ...


1

YouTube-dl supports this: $ youtube-dl fast.wistia.net/embed/iframe/xcorh9bx2t WARNING: The url doesn't specify the protocol, trying with http [Wistia] xcorh9bx2t: Downloading JSON metadata [download] Destination: Opticon 2015 Keynote Address-xcorh9bx2t.mp4


2

The right tool for this is awk. It lets you express the logic in a straightforward way: set a seen variable the first time the pattern is seen, and skip lines matching the pattern if it has been seen already. If a line hasn't been skipped, print it. awk '/^yyyy-mm-dd[ \t]+hh:mm:ss$/ {if (seen) next; ++seen} 1 {print}' This can be abbreviated, but it ...


2

sed -e '0,/^yyyy-mm-dd\s\+hh:mm:ss$/!{/^yyyy-mm-dd\s\+hh:mm:ss$/d}' The 0,/pattern/ (a GNU sed feature) range matches up to the first occurrence, so 0,/pattern/! runs the contents of the {...} on the remainder of the file, removing all later matches.


3

If you only want to remove all the duplicate yyyy-mm-dd lines then % sed '/^yyyy-mm-dd/,$ { n ; /^yyyy-mm-dd/d }' srcfile Will use per-minute statistics Stats from 2016-06-26 00:00:00 to 2016-06-26 23:59:00 yyyy-mm-dd hh:mm:ss 6/26/2016 0:01:00 6/26/2016 0:03:00 6/26/2016 0:01:00 6/26/2016 0:01:00 6/26/2016 0:02:00 6/26/...


0

Assuming you know how to start a browser - I'd use Greasemonkey, it can be used to pressing buttons, clicking links and whatsoever the browser can do. For Chrome there is a Tampermonkey - for the same purpose.


0

Try this (note the !) dir1=/source/dir/path/ while ! inotifywait -qqre modify "$dir1"; do rm -r /destination/dir/path find /source/dir/path/ -name .svn -exec rm -rf '{}' \; cp -ruv /source/dir/path/* /destination/dir/path/ done


1

Upstart might do your job. Create a file $HOME/.config/upstart/firefox-with-yoururl.conf with following content: start on desktop-start stop on desktop-end exec firefox <url> Reference : http://ifdeflinux.blogspot.in/2013/04/upstart-user-sessions-in-ubuntu-raring.html


2

In interactive use, the shell reads lines of input from the terminal device. After a line has been entered, it is parsed by splitting into tokens (words and operators). The tokens or words are then expanded or resolved in a specific order. Note: in general, it’s usually best to refer to the canonical source of information such as project documentation or ...


0

Instead of invoking grep several times, you could also use a single invocation of sed, either sed -rn '/(aa|ee|ii|oo|uu)/d; /a/{/e/{/i/{/o/{/u/p}}}}' or sed -rn '/([aeiou])\1/d; /a/{/e/{/i/{/o/{/u/p}}}}' if you prefer to use back-references.


2

This is expected behaviour and has been working for ages. It is useful for building up commands in scripts. Say, for example, that I sometimes should apply decompression on some input before sending it into a pipeline (and compression on the output afterwards), but sometimes not. Using bash: if (( use_compression == 1 )); then infilter='gzip -d -c' ...


2

It is simply using shell variables, a concept that has existed and worked like that for decades (without exaggeration). When you put a variable in a command line, the shell substitutes it and then runs the command, so if your variable is at the beginning, it will obviously be considered as the command.


2

As long as you want to exclude the entire line if there are double vowels, this should work: grep -i a file | \ grep -i e | \ grep -i i | \ grep -i o | \ grep -i u | \ grep -v -i '\([aeiou]\)\1'



Top 50 recent answers are included