Tag Info

New answers tagged

0

A way to do this that includes (IMHO) the best parts of the other two answers is to write a shell function that embodies all the functionality that you want to be invariant, and takes arguments specifying the things that you want to be different on different instances: my_rsync_grep() { rsync -av --delete -R "$1" "$2" "$3" | grep -E '^deleting|[^/]$' ...


1

The usual way is to make only command's arguments as variable _mn=/backup.raw/ _rscmdarg='-av --delete -R' _greparg='-E "^deleting|[^/]$"' _rslog=/var/log/rsync.log as $_rslog is a file, and $_mn a dir, it is okay to keep them. and the "final" line is: rsync $_rscmdarg --exclude=alternatives /etc/ $_mn | grep $_greparg >> $_rslog


1

The splitting into different parts (command & arguments) does not work if variables are used. Use eval for this case: eval $_rscmd --exclude=alternatives /etc/ $_mn | eval $_grep >> "$_rslog" In general, it is better to use shell functions or aliases than using variables: alias my_grep='grep -E "^deleting|[^/]$"' ... ... | my_grep >> ...


0

I've since tried tackling the problem from a different angle, using the 'expect ' command. However the following fails to work: #!/usr/bin/expect spawn sudo mount.cifs "//192.168.1.2/My Pictures" /home/pi/Desktop/Pictures -o user=Rob_ expect "Password: " { set send_slow {1 .1} send -s "a_password" } It responds to the password prompt by typing it ...


-1

There are multiple reasons: Your debug printf didn't work, because mount prints its prompt to the terminal, not stdout. read prompt failed and the loop was never entered. You are trying to echo the password to /dev/stdin. This will not work, you can only read from it. the mount's stdin is still connected to the terminal. There is no way to emulate user ...


0

exit does terminate the whole shell, or the current sub-shell: $ bash -c 'for i in 1 2 3; do for j in 4 5 6; do echo $i; exit 1; echo $j; done; done' 1 $ echo $? 1


12

Your problem is not nested loops, per se. It's that one or more of your inner loops is running in a subshell. This works: #!/bin/bash for i in $(seq 1 100); do echo i $i for j in $(seq 1 10) ; do echo j $j sleep 1 [[ $j = 3 ]] && { echo "I've had enough!" 1>&2; exit 1; } ...


0

You can use break. From help break: Exit a FOR, WHILE or UNTIL loop. If N is specified, break N enclosing loops. So for exiting from three enclosing loops i.e. if you have two nested loops inside main one, use this to exit from all of them: break 3


1

The first process is the shell running your script. The second process is a shell sub-process of this script, possibly due to commands running inside parentheses, which causes the process to fork. An example is: "( date; cat )". The third process is the binary of the actual server you want to run. If you want to kill the server, kill the third process. ...


0

There's three process running because you're running 1 script and that script has commands written in it that run others apps: Bash (To run the script) The script itself. The app. Inside the script: root@onare:/home/onare# cat Downloads/TeamSpeak3-Client-linux_amd64/ts3client_runscript.sh #!/bin/bash export KDEDIRS= export KDEDIR= export QTDIR=. ...


4

The key bit of syntax you're missing here is: git commit -m "$(printf "Updated $submodule Submodule\n\n" ; git diff $submodule)" The use of the $() form of command substitution inside double quotes sends the output of git diff... to git commit as a commit message with newlines intact. I used printf here instead of echo to prepend the subject line since ...


3

Not sure of the purpose of for loop in your script which is not needed and does not serve any purpose. The following will work as you expected. find /var/backup/web2 -maxdepth 1 -type f -mtime +1 -delete


1

Just put the value in a variable compiler_options_for_debug_version="" if something; then compiler_options_for_debug_version="-g -O0" fi Then make sure you quote the variable when you use it: this is just as important. CFLAGS="$compiler_options_for_debug_version" ./configure ...


0

If you haven't yet considered how usefully sed might be applied to your data, please, allow me to demonstrate: sed ' s/\([^, ]\{1,\}\),*/\1,/2 s//\1/3;H;y/,/\n/;P;/^M_D.*/!d s///;x;s/[^, ]*, *//g ' <<\IN EXCHANGE_ID: 192, ...


1

How about a sed script with gotos (shock, horror). This could be simplified if you describe your data more precisely. sed -n ' :start /Server .* Health Check/{ n /Date - Count/n :loop1 / : /{p; n; b loop1 } b start } /Errors caused by/{ n :loop2 /Server/n /^[0-9]/{p; n; b loop2 } b ...


1

Well, if you have GNU tools you can do: for match in \ Server\ Health\ Check\ 1 Server\ Health\ Check\ 2 Errors\ caused\ by\ X do grep -Fxm1 "$match" case $match in (S*) sed -nEu '/^[0-9/:]+/!q;p';; (*) sed -u '4q;3d;1d';; esac;done <file The assumption here is that there is some intervening data between the sections you're ...


0

This happens when a file contains \r\n as a line terminator instead of \n, since \r is a C0 control code meaning "go to the beginning of the current line". To fix, run dos2unix foo.py. Example session: ben@joyplim /tmp/cr % echo '#!/usr/bin/env python' > foo.py ben@joyplim /tmp/cr % chmod +x foo.py ben@joyplim /tmp/cr % ./foo.py ben@joyplim /tmp/cr % ...


0

Could it be a file permission issue? $ chmod +x foo.py If you don't specifically indicate you wish to maintain then they are stripped/altered by most ftp clients.


0

To handle an input file with just 1 group of 6 lines and just 2 data columns, a simple –but limited, and greedy on resources– approach makes the coding overhead minimal, ie, no need for arrays to hold data from all 6 line: f='src.txt' # input fule d=' +: +| +|, *' # field delimiter regex set {2,3} # data columns - not label (which is ...


0

How about: awk -F'[ \t,]+' '{a=a$1"\t"$2"\t"$3"\n"; b=b$1"\t"$2"\t"$4"\n"} END {print a; print b}' data.txt Here we treat one or more space, tab, or comma as a field separator. Then on each line we build the output. Finally we print the results. This is a pretty dirty one liner; for example it has to read the whole file before it prints anything so for ...


0

Type crontab -e to edit your crontab file and then add @reboot gtk-launch ts3 /usr/share/applications and then save it... Breakdown Essentially this means that every time you reboot run the .desktop file ts3 from folder /usr/share/applications More Information There are some other ways of doing this, but they are more complicated and are ...


1

Here is your script modified slightly: #!/bin/bash if files=$(find . -name "*.gz" -size -10k -mtime -1 -print) ; then echo "The backup test has failed!\n$files" | mail -s "BACKUP FAILURE" myemail@gmail.com fi You don't need to put this in a for loop; there is an implicit loop in the find comand. One debugging technique you can use if you haven't ...


0

I assume you want to check for small gz files in this folder /usr/test, the follwoing should help you. it will send you a log file of all the gz files under this path and email you the list. You can use this list for later use. My script would be: #!/bin/bash size_checker () { cd /usr/test ls *.gz > gzfiles for FILE in `cat gzfiles` do SIZE=`ls -la ...


0

Simpler answer than the one's I've seen above. if [ "$a" != "0" -o "$b" != "100" ]; then


3

Possible solution could be with using awk: awk -F";" '{ for (i = 2; i <= NF; i++) { printf("%s;%s\n", $1, $i); } }' file With awk -F";" we set FS(field separator) to ;. Then for every row(record) we start from field 2 to the last field (NF): for (i = 2; i <= NF; i++) and we print field 1 and current field ($i).


5

If you're running bash you can do it with regexp matching: if [[ $Y =~ (ERROR|ORA-) ]]; then echo error fi Or, if you insist, you can also do it with awk: if ! printf '%s\n' "$Y" | awk '/ERROR|ORA-/ {exit 1}'; then echo error fi The point is, awk uses regular expressions, not shell expression logic. The simplest approach is probably to just ...


0

In most cases you can avoid your if looping structure by: chmod 777 -- my_dir/[^ab].so my_dir/*??.so chown igor -- my_dir/[^ab].so my_dir/*??.so


3

If you don't want to process hidden files: for f in my_dir/*.so; do case $f in (*/[ab].so) : ;; (*) chmod 777 -- "$f"; chown igor -- "$f" ;; esac done Note that setting file permission to 777 is very bad ideal, causing security hole and making chown command later wasted.


6

The standard (POSIX sh and utilities) canonical legible ways would be: string comparison: if [ "$a" != 0 ] || [ "$b" != 100 ]; then... decimal integer comparison (0100 is 100, whether leading blanks are ignored or not depend on the implementation though). if [ "$a" -ne 0 ] || [ "$b" -ne 100 ]; then... integer comparison (0x64, 0144 are 100 (POSIX mode ...


1

Use arithmetic evaluation and parentheses to avoid ambiguity: if (( ($a != 0) || ($b != 100) )); then # some commands Or, if you want to deal with strings as well, use [[.


1

[ "$((a||b^100))" -eq 1 ] && some commands A shell's math expansion will handle the boolean && AND || OR and ! NOT conditions by evaluating the expression to either 1 for true or 0 for false. It will handle the bitwise & AND | OR and ^ XOR operators as well, but obviously those won't necessarily get you a 0 or 1, though a bitwise ...


0

arp -a will dump all that information without needing to install nmap. However, nmap does an active scan, so it will be more up to date.


2

You might use lftp instead of the regular ftp client. With lftp you can use mget -E /path/to/files which will delete the source files after succesful transfer. See http://lftp.yar.ru/lftp-man.html for the manual.


0

From the last line with text: $Id: script-name.py 474 2010-12-10 12:16:36Z adminName $ you can assume that these files were kept in Subversion (there is an explanation of the $Id$ keyword here). The older CVS would also use the $Id$ keyword, but that would expand the filename to script-name.py,v (as is done here). The two entries before that could come ...


2

Using sudo: #!/bin/sh ip -s -s neigh flush all ufw enable sudo -Hu username sh -c '"/home/back/Downloads/tor-browser_en-US/Browser/start-tor-browser" --detach || ([ ! -x "/home/back/Downloads/tor-browser_en-US/Browser/start-tor-browser" ] && "$(dirname "$*")"/Browser/start-tor-browser --detach)' dummy %k -H: Sets the $HOME environment variable ...


1

There are two ways to go about this. 1) Run the script as a non-root user and use sudo to raise privileges to the root user (prefix the commands to be ran as root with sudo). or 2) Run the script as root user and use su to run the tor command as non-root user. su allows you to stipulate what user to run the command as and the -c option to specify what ...


3

You can use find for all of that including the size (man find): -size n[cwbkMG] File uses n units of space. The following suffixes can be used: `b' for 512-byte blocks (this is the default if no suffix is used) `c' for bytes `w' for two-byte words `k' for Kilobytes (units of 1024 bytes) ...


2

So you have something like this: $ foo='my dir' $ ls $foo my: no file or directory found dir: no file or directory found Instead, try this: $ foo='my dir' $ ls "$foo" Or even better: $ ls "${foo}" Both of these methods shield the [:space:] character from being interpreted by the shell, and ensures that it is read as a [:space:] character, not the ...


0

awk $ awk '{print $1*$3/100}' file 13721725 7.20358e+08 261414528 Assuming you don't want the "scientific" notation: $ awk '{printf "%.1f\n", $1*$3/100}' file 13721725.0 720357926.4 261414528.0


1

Try this with ksh: while read A B C; do tmp=$(($A*$C/100)) echo $tmp done < foo.txt > out1.txt Output to out1.txt: 13721725 720357926 261414528 See: Performing arithmetic on variables in the Korn shell


1

Usually this means that rvm is not setup correctly. The usual mistake is that you didn't modify your bashrc correctly (or simular) or you did not reload your terminal. You should not set GEM_HOME or GEM_PATH manually when using rvm. You can try ´source ~/.rvm/scripts/rvm´ and see if that works. It could also be that you have not built a gemset yet. Do ...


1

Sounds like you want the command export for setting environment variables: export PATH=$PATH':/path/to/add' export GEM_HOME=$HOME/.gem export GEM_PATH=$HOME/.gem That will only take effect for the current session, though. To make them more permanent, add those lines to your ~/.bashrc.


0

From man echo Normally you could distinguish between a flag and a string that begins with a hyphen by using a (double hyphen). Since no flags are supported with the echo command, a (double hyphen) is treated literally. And in the 2nd example: Note: You must put the message in quotation marks if it contains escape sequences. Otherwise, the shell ...


0

Another possible option is to use awk: $ echo "Name Age Gender Address" | awk -F' ' '{ printf "%s\t%s\t%s\t%s\n", $1, $2, $3, $4 }' produces: Name Age Gender Address Or using printf directly: $ printf "%s\t%s\t%s\t%s\t\n" Name Age Gender Address produces: Name Age Gender Address


0

Use the \n to add newlines to your header: header="-------------------------------------\nName\tAge\tGender\tAddress\n-------------------------------------\n" Then use for instance echo -e $header to display: echo -e $header ------------------------------------- Name Age Gender Address ------------------------------------- If echo -e does not work, ...


2

If you use ANSI strings: header=$' ------------------------------------- Name\tAge\tGender\tAddress ------------------------------------- ' you have the correct translation of any ANSI escaped characters already in your variable. (Note: the newline characters could also be defined as \n, but it wouldn't add to legibility in your case, so I'd keep your ...


1

From man bash: set -u Treat unset variables and parameters other than the special parameters "@" and "*" as an error when performing parameter expansion. If expansion is attempted on an unset variable or parameter, the shell prints an error message, and, if not -interactive, exits with a nonzero status. POSIX states that, in the event of an expansion ...


1

(I'm using bash 4.2.53). For part 1, the bash man page just says "An error message will be written to the standard error, and a non-interactive shell will exit". It doesn't say an ERR trap will be called, though I agree it would be useful if it did. To be pragmatic, if what you really want is to cope more cleanly with undefined variables, a possible ...


2

case $(file -b --mime-type - < "$attachment") in (text/*) printf '%s\n' "$attachment is probably text according to file" case $(file -b --mime-encoding - < "$attachment") in (us-ascii) echo "and probably in ASCII encoding" esac esac


5

From the fact that it says file $attachment rather than file "$attachment", I guess your script cannot handle filenames that contain spaces.  But, be advised that filenames can contain spaces, and well-written scripts can handle them.  Note, then: $ file "foo bar" foo bar: ASCII text $ file "foo bar" | cut -d' ' -f2 bar: One popular and highly ...



Top 50 recent answers are included