Tag Info

Hot answers tagged

9

If you want to loop over all the arguments to your script, in any Bourne like shell, it's: for i do something with "$i" done You could also do: for i in "$@"; do something with "$i" done but it's longer and not as portable (though is for modern shells). Note that: for i; do something with "$i" done is neither Bourne nor POSIX so should be ...


5

By default, scp remote paths are interpreted relative to the home directory, so you don't need the ~ at all: scp user@remote.host.com:some/file/name filename will download some/file/name from the home directory of user and save it as filename locally. When you want to use an absolute file path on the remote server, start it with /: scp host:/etc/passwd ...


5

From the source of wc (coreutils/src/wc.c) in GNU coreutils (i.e. the verion on non-embedded Linux and Cygwin): When counting only bytes, save some line- and word-counting overhead. If FD is a 'regular' Unix file, using lseek is enough to get its 'size' in bytes. So using wc -c to count the bytes will perform well. You can easily test this ...


4

This make sure that $1 is not empty string. If $1 is empty string, then if condition is: ( A == A ) evaluated to true. This is a workarround to make sure you don't have an syntax error. If you simply do: if ( $1 == "" ) When $1 is empty string, this expression become ( == "" ) causes a syntax error. At least in bsd-csh, tcsh, ( $1 == "" ) works. With ...


4

The typical tool to query POP3 servers from the commandline is fetchmail which you can instruct to leave the original messages on the mail server (the 'keep' option or nokeep to empty the mailbox after downloading the messages) and then procmail is the typical mail filter, which can be used to trigger your script. A sample .fetchmailrc would be someting ...


3

Try this: find /path/to/file/ -maxdepth 1 -type f -name '*.csv' -print0 | while read -d '' -r file; do sed -n '3,53p' $file; done Notice print0 option which take care of any possible whitespace characters in the file names.


3

If called from any Unix shell, then you need to quote it. sh shell hi "hello guys" bye There is no way to do it in the script, at there is no way for the script to know which spaces are which (which words are together).


3

The shell has a built-in variable expansion field separator. So if you have a string and your delimiter is solid you can do: var=32768,'dff0207a-591f-4435-9f8b-7b9b3e6ba2c1','d1f77359b3f7236806489ba3108c771f','NUMBER','US_EN','LATIN','GREEK','GERMAN' ( IFS=,; set -f for field in $var do printf '\n%s\n\t' "$field - md5:" >&2 printf ...


3

With awk: $ awk 'FNR==NR{a[$1];next} ($2 in a){$2=$2"_terminated"}1' hr.txt empt.txt 21356 suresh 12/12/2012 23511 ramesh_terminated 11/06/2011 31456 biswajit 09/08/2013 53134 archan_terminated 06/02/2009


3

I'm ruling out stat and perl which are not POSIX so are more likely to be missing than ls and awk. I'm ruling out wc too as while the GNU implementation of wc is optimized when the -c option is used, you should not rely on it to be present for a portable script. Moreover, some non standard compliant wc -c might return the number of characters which is not ...


2

You should, as terdon says in his answer, use bash's associative arrays instead. But, if you insist, and if you have a new enough bash, you can use a nameref (that's the magic word to search for in the manpage, by the way). i=1 declare -n tmp="foo_$i" # this is the nameref line tmp="fooval" # actually sets $foo_1 echo "$foo_1" # ...


2

You are not clear: Are you editing old files, or creating entirely new ones? New ones can be created in this style with a single bash script: $ cat script.sh #!/bin/bash cat <<EOF >firstfile 1. The quick brown fox jumped over the lazy dog. EOF cat <<EOF >secondfile 2. The quick brown fox jumped over the lazy dog. EOF echo Done. $ ...


2

You may already know zsh-history-substring-search. It does not do preview you have seen, as far as I can tell, but otherwise it seems to be very similar. If you do not know it, it's worth trying. zsh-history-substring-search is closely based in the history search of the fish shell. fish has some pretty advanced features in terms of interaction, certainly ...


2

You should just quote the second argument. myfunc(){ echo "$1" echo "$2" echo "$3" } myfunc hi "hello guys" bye


2

Have a look at crudini, which is a shell tool designed for this conf=/etc/puppet/puppet.conf crudini --set "$conf" agent server "$PUPPET_MASTER_TCP_HOST" crudini --set "$conf" agent masterport "$PUPPET_MASTER_TCP_PORT" or a single atomic invocation like: echo " [agent] server=$1 masterport=$2" | crudini --merge /etc/puppet/puppet.conf


1

You may be able to accomplish this using find itself find path -type f -name '*.csv' \ -exec sh -c 'for f; do head -n 53 "$f" | tail -n 51; done' _ {} +


1

I think the following should help you, if I understand you correctly. Script/line: $ for csv_files in $(find /home/t1/ -name "*.csv"); do echo $csv_files ; done Output: /home/t1/t4/t4.csv /home/t1/t1/t1.csv /home/t1/t2/t2.csv /home/t1/t3/t3.csv Instead of the echo you can insert your second command cat $csv_files | head -53 | tail -51 For a sense of ...


1

In Tcl documentation, optional elements look like ?this?: example from http://tcl.tk/man/tcl8.6/TclCmd/try.htm showing the handlers and "finally" clauses are optional: try body ?handler...? ?finally script? In the on clause, variableList is not optional, but you can provide an empty list. If you provide {result options}, result is the error message ...


1

Answering the alternative part of your question, "...help me programm it with updating the list properly": You can directly use the functions that list completion items, update the list, or clear it; That's zle -R. To write out your own "unmanaged" text, there is zle -M. From pinfo zsh: zle -R [ -c ] [ DISPLAY-STRING ] [ STRING ... ] zle -M STRING [ ... ] ...


1

find -mtime -2 \ -maxdepth 1 \ \! -type d \ \( -name 'simon*' \ -o -name 'tom*' \ -o -name 'john*' \ \) | tar -T - \ --xform='s/[0-9]*$//' \ -cf - | tar -C ./path/to/destination --keep-newer-files -xf - I think that will do it - it appears to work for me. So long as the simon, tom, and john files are intended to be ...


1

I don't know why you want to use ziped files for your backup. If you use rsync, you will be able to only backup new and changed files (removed files is not affected). Example: #!/bin/bash # see http://ss64.com/bash/rsync_options.html DROPBOX=~/Dropbox/ BACKUP=~/dropboxbackup rsync -rvt $DROPBOX $BACKUP The flags: r - recursive v - verbose t - ...


1

You should use crontab Example: If you wished to have a script named tarBackup.sh run every day at 5am, your crontab entry would look like as follows. First, install your cronjob by running the following command: crontab -e Append the following entry: 0 5 * * * xterm -e /path/to/tarbackup.sh Save and close the file. Your tarBackup.sh Your ...


1

Use the following code, #!/bin/bash #input server names line by line in server.txt cat server.txt | while read line do sshpass -p password ssh root@$line 'hostname;uname -r' done


1

The problem is the read path. While one line creates the $path variable, the next sets it to blank again: ## This line will set the $path variable. path=$(php -i|grep php.ini | awk 'NR==2{print $5}') ## This one expects to read the value of $path from standard input ## so it sets it to empty unless input is given. read path You can test this by adding ...


1

You should use this, I think. As I've just found out, it's a POSIX-specified standard utility. du The POSIX-specified options include: The du utility shall conform to XBD Utility Syntax Guidelines . The following options shall be supported: -a In addition to the default output, report the size of each file not of type directory in the file hierarchy ...


1

You can use su with -c: /bin/su tomcat -c "$CATALINA_HOME/bin/catalina.sh stop 30 -force" From su man page: -c, --command=COMMAND pass a single COMMAND to the shell with -c


1

There's a time for everything, including parsing ls. There's no portable way to prevent ls from mangling file names, but when you're only interested in some of the metadata about one file, it's ok. filesize () { LC_ALL=C ls -dn -- "$1" | awk 'NR==1 {print $5}' } This works on all systems compliant with POSIX with the XSI extension. It works with ...


1

The simpler, more portable maybe perl: filesize() { file=$1 if [ -e "$file" ] then size=$(perl -e 'print -s shift' "$file") printf "%s\n" "$size" return 0 else printf "0\n" return -1 fi }


1

You could try crudini crudini --get file.ini | while read section; do test "$(crudini --get t.ini $section | paste -d, - - -)" = \ 'shortcut,site,theme' || echo error in section $section done



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