Tag Info

New answers tagged

1

You can use array assignment directly: A0=($(sed '2q;d' /proc/stat)) This can be tiny faster than using read: $ time for i in {1..1000}; do read -a A0 <<< $(sed '2q;d' /proc/stat); done real 0m2.829s user 0m0.220s sys 0m0.480s $ time for i in {1..1000}; do A0=($(sed '2q;d' /proc/stat)); done real 0m2.388s user 0m0.128s sys ...


0

The syntax seems too complex to my eyes. I'd use the following instead: read -a A0 < <( sed '2q;d' /proc/stat ) I usually use the <<< word syntax when I need a command to take input from a variable, not from another command.


4

Use doublequotes (") in the echo command: echo "$f$result$s" This is because echo interprets the variables as arguments, with multiple arguments echo prints all of them with a space between. See this as an example: user@host:~$ echo this is a test this is a test user@host:~$ echo "this is a test" this is a test In the first ...


0

If you are running the command sh scriptfile < file then file needs to contain the enter char The following works for me: $ cat a #!/bin/sh read -p "Press enter..." $ echo "\n" > file $ sh scriptfile <file $


0

Give a try to this one: grep -Po 'LAN2:\K(\d{1,3}.){3}\d{1,3}/\d*' file This looks for a block of PATTERN:ipdigits.ipdigits.ipdigits.ipdigits/digits and prints the ipdigits.ipdigits.ipdigits.ipdigits/digits part (\K "cleans" the matching). It indicates that ipdigits must have between 1 and 3 characters. More filters could be applied (to match 1 to 255). ...


1

What about awk '{for(i=1;i<=NF;i++) if ( $i ~ /[^:]*:/ ) { n=split($i,A,":"); if (length(A[2]) && n==2) print A[2] ; }} ' b.txt X.X.X.X/XX X.X.X.X/XX X.X.X.X/XX X.X.X.X/XX Edit: (with LAN) awk '{for(i=1;i<=NF;i++) if ( $i ~ /[^:]*:/ ) { n=split($i,A,":"); if (length(A[2]) && n==2) printf "%s -- %s\n",A[1], A[2] ; }} ' b.txt LAN1 -- ...


1

For a single session, I use something like that: cat > screenrc-test <<EOF screen -t test1 sh -c "./myprogram < input_part1.txt > output_part1.txt" screen -t test2 sh -c "./myprogram < input_part2.txt > output_part2.txt" screen -t test3 sh -c "./myprogram < input_part3.txt > output_part3.txt" screen -t test4 sh -c "./myprogram ...


2

If you insert echo "prevexit=3" > ~/.prevexit at the end of your crontab initiated command, then you can use: source ~/.prevexit close to the top of all the scripts that need to include the value, and use it in the rest of those scripts as $prevexit. You should of course replace 3 with the real value you want to share.


1

Use this: "${index}_${lumarr[lum]}" Generally: Interpolate all variables using ${...} notation. Unless you expressly want to use word-splitting, always enclose variable interpolations in double-quoted strings.


0

Calculating the average per core usage from /proc/stat The average cpu usage per core can be directly computed from /proc/stat (Credits to @mikeserv for the hint for using /proc/stat.): # Here we make use of bash direct array assignment A0=($(sed '2q;d' /proc/stat)) A1=($(sed '3q;d' /proc/stat)) A2=($(sed '4q;d' /proc/stat)) A3=($(sed '5q;d' /proc/stat)) # ...


3

The "stuff" between the values seems a visual representation of the newline character to me ( octal character code 12), which you would get when using: echo -e 'a\012b' What you could try is pipe the output through tr '\n' ' ' as with: echo -e 'a\012b' | tr '\n' ' '


2

This creates a bash array whose elements are the loads for each CPU: loads=($(mpstat -P ALL 1 1 | awk '/Average:/ && $2 ~ /[0-9]/ {print $3}')) Since bash arrays are numbered starting with zero, the load of the second CPU would be printed with: echo ${loads[1]} This requires the utility mpstat. To install it on a debian-like system, run: ...


1

So what I did is that I trimmed the list file down to the first two values (ID;Name), then I used this script on the source text file: #!/usr/bin/env bash dos2unix "$1" sed -e '/Sideboard/,$d' "$1" -e '/^$/,$d' | tee source_strip | cut -d ' ' -f 2- >temp while IFS= read f; do sed -n "s/\([0-9]*\);$f$/\.\1/p" list done <temp >IDs sed -ni ...


0

you can see the frequency for each core with the following: $ cat /proc/cpuinfo


0

Edit the <Directory "/0" > entry in your httpd.conf file and add: Options +ExecCGI AddHandler cgi-script .sh You should end up with something similar to: <Directory "/0" > ... Options +ExecCGI AddHandler cgi-script .sh ... </Directory> Restart your server: # service httpd restart All the CGI files should be ...


7

First, cat must be written lowercase. This command statement uses the concept of here documents. The first part cat << EOF means that stdin (standard input) of the command comes afterwards. All that comes after the first line until the word EOF is the standard input to the command cat. Or from the documentation: This type of redirection instructs ...


2

Try this syntax and I think you don't want the output of grep, so we use the -q (quiet) switch: (traceroute -n google.com | grep -q x.x.x.x) && echo "Yes" || echo "Nope" Or with the if statement: if (traceroute -n google.com | grep -q x.x.x.x) then echo "Yes" else echo "Nope" fi


-1

I think you should consider reading basics of if-else first.....LOL you even have the wrong syntax, and I wonder what bal bal and ova ova ova mean.....LOL tracepath -n google.com | grep x.x.x.x if [ $? -eq 0 ] then echo "bal bal" else echo "ova ova ova" fi


2

This occurs because set -x displays the evaluation of expansions - which is not a factor regarding typical redirections. LC_ALL=C man set | sed -n '/^ *-x/,/^$/p' -x The shell shall write to standard error a trace for each command after it expands the command and before it executes it. It is unspecified whether ...


0

If I understand the purpose of your code correctly, then, in your example case: $6$hcwp49Lr$BjcJYc/nwaufmsOIw4Tw/POaXO4j.0HDLU0 should be replaced with: XXXXX If this is true, you don't need to involve the shell at all: sed '/^user:/s/:[^:]*/:XXXXX/ ' <<\DATA # junk here user:$6$hcwp49Lr$BjcJYc/nwaufmsOIw4Tw/POaXO4j.0HDLU0:16310:0:99999:7::: ...


1

Before you start porting try with GNU Parallel: parallel -j0 --tag --line-buffer ssh {} bash /home/cloudera/bash_script.sh ::: server1 server2 server3 You need version 20130822 or later for --line-buffer.


1

If you want $arr to be substituted, you need to put it outside single quotes. For instance: sed -i "s|$arr|XXXXX|g" /tmp/shadowtest


1

There’s no functionality in the shell to support this.  You could achieve this specific result by doing tee /test << "EOF" a b c d EOF but this is not trivially adaptable to everything you could do in a shell script. You might want to look into programs like screen and Expect.


0

one line: cd $(dirname $([ -L $0 ] && readlink -f $0 || echo $0))


0

Gnouc makes an important point without stressing it: whenever you reference a shell variable ($identifier, e.g., $futur) or a positional parameter ($digit, e.g., $1), you should enclose it in double quotes (e.g., "$1") unless you have a good reason not to and you’re very sure you know what you’re doing.  Constant strings with no special characters (e.g., A, ...


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


0

This line checks if $1 is empty. $1 is the first parameter passed to the script by user when running the script on the command line. If $1 is empty, then nothing is appended to the letter A and A == A is true.


0

I believe the if loop is used as a way to test if the arguments are passed or not to the script. Though technically it is the purpose of the if loop, I think it would be more apt to say it is used to check if the $1 is empty or not. For example, I copied the script and invoked it as, script "some_argument" As expected, it doesn't go inside the if loop. ...


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


0

Within your /etc/inittab add the following line to login: 1:2345:respawn:/bin/login -f <username> tty1 </dev/tty1 >/dev/tty1 2>&1 Where <username> would be "root", if that's the login you're attempting to autologin with. NOTE: Be sure to comment out any getty line that may already be present in the /etc/inittab. This line is ...


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


0

One way to handle literal single-quotes in awk is to use the equivalent octal escape code \047 e.g. FS exactly equal to '|' printf "foo'|'bar'|'baz\n" | awk 'BEGIN{FS="\047\\|\047";} {print $2}' bar Note that the | needs to be escaped in order not to be treated as a regex logical OR; alternatively you can use a [...] character range instead of escaping ...


0

The awk FS variable is expected to contain a regular expression. Some characters are special within regular expressions and the vertical bar character is one of those characters. Placing it within brackets makes it part of an anonymous character class and vertical bar is not special within such classes. This makes awk treat vertical bar as an ordinary ...


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


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


0

#!/bin/bash n=1 echo "$0 got $# args..." while [ $# -gt 0 ] ;do echo "$n: $1" shift n=$(( $n + 1 )) done Alternatively, look up 'Listing arguments with $* and $@' in http://www.tldp.org/LDP/abs/html/abs-guide.html which will elaborate on many aspects of these.


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


1

Provided you have execute permissions on the current directory - or on the directory from which you executed your shell script - if you want an absolute path to a directory all you need is cd. Step 10 of cd's spec If the -P option is in effect, the $PWD environment variable shall be set to the string that would be output by pwd -P. If there is ...


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


0

You may use fetchmailand procmail programs. fetchmail can * download new POP3 messages with or without deleting them at POP3 server * pass downloaded messages to another program (procmail) standard input (see --mda option) procmail can * execute custom program/script for messages matching specific critera e.g. Subject: matching regular expression


0

To be executed the script must: manifest its interpreter at the first line, e.g. #!/bin/sh; have execute permission, ensure with chmod u+x myscript; started like ./myscript (assuming it's in the current directory) if not placed in a directory in PATH list (see echo $PATH). Alternatively the script can be started using the corresponding interpreter ...


0

Consider looking at RFC1521 "MIME Part One", Appendix C "A Complex Multipart Example". It seems that you need to put a blank line after headers.


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


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


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


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


0

The following is hopefully self explainatory find -maxdepth 1 -mtime -2 -type f -exec bash -c 'name=${1##*/}; cp "$name" /some/other/dir/${name%%[0-9]*}' _ {} \;


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


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



Top 50 recent answers are included