Tag Info

Hot answers tagged

14

The setuid bit can be set on an executable file so that when run, the program will have the privileges of the owner of the file instead of the real user, if they are different. This is the difference between effective uid (user id) and real uid. Some common utilities, such as passwd, are owned root and configured this way out of necessity (passwd needs to ...


10

You can use a vi script: $ vi test.txt -c '%s/aaa/NNN/ | wq' $ cat test.txt NNN NNN bbb ccc ddd You're simply automating what would normally be entered when using vi in command mode (accessed using Esc: usually): % - carry out the following command on every line: s/aaa/NNN/ - subtitute aaa with NNN | - command delimiter w - write changes to file q - ...


9

Another option - ed line editor: ed -s test.txt <<< $',s/aaa/NNN/g\nw'


8

Some reasons I can think about why they used find + xargs: Handling the case when you have too many cache files, leading to an error if you run only one rm command. Globbing * does not expand hidden files. Working recursively. But this find + xargs is not efficient, since when they didn't add any filter, so find result will contain directories along with ...


7

Using sponge: #!/bin/bash pattern='aaa' replacement='NNN' while read -r line do printf '%s\n' "${line//$pattern/$replacement}" done < "${1}" Call with: ./script.sh test.txt | sponge test.txt


7

Yes you can, but it is probably a very bad idea. Usually, you would not set SUID bit directly on your executable, but use a sudo(8) or su(1) to execute it (and limit who can execute it) Note however, there are many many security problems with allowing regular users to run programs (and especially scripts!) as root. Most of them having to do that unless the ...


7

There are a few differences in the behavior of the command lines: The find command line would delete files recursively in subdirectories, the rm command line wouldn't. You need to consider whether or not you want to recurse. The find command line would delete all files, if possible. The rm command line might skip files based on the shell's settings like ...


6

Using awk: #!/usr/bin/awk -f BEGIN { FS=";" } $1 ~ name { print "The number of " name " is " $2 } phones -v name=Jan telephone The number of Jan is 032569874


6

Here's a roughly modified version of your script: $ more cmd.bash #!/bin/bash echo "Whose phone number do you want to know?" read name number=$(grep "$name" telephone | cut -d';' -f2) echo '' echo "The phone number of $name is $number." It works as follows: $ ./cmd.bash Whose phone number do you want to know? Hans The phone number of Hans is ...


5

using awk : awk '{print $4,$5,$6,$7,$8,$9,$1,$2,$3}' log_file you can do it directly from you log file like this: awk '{printf("<wpt %s%s\"%s\" %s%s\"%s\">\n<time>%s %s %s</time>\n</wpt>\n",$4,$5,substr($6,0,length($6)),$7,$8,$9,$1,$2,$3)}' log_file output: <wpt lat="41.858657" lon="-91.345142"> <time>11-06-2014 - ...


4

If you have GNU Parallel you can do this: doit() { i=$1 echo "$i" mri_binarize --i ${in}/${i}/mri/aseg.mgz --o ${out}/${i}/masks/cc.nii.gz --match 41 42 43 44 45 flirt -in ${out}/${i}/masks/cc.nii.gz -ref ${out}/${i}/T1.nii -out ${out}/${i}/masks/cc2T1.nii.gz -omat ${out}/${i}/masks/cc2T1.mat -bins 256 -cost corratio -searchrx -90 90 -searchry -90 ...


4

If you are using bash or ksh, you can use pattern substitution for shell variables. Note however, that basic shell globs are less powerful and extended shell globs have some features that sed doesn't and vice versa. For more details, see 'Parameter Expansion' in man 1 bash: t=$(< test.txt); printf '%s\n' "${t//aaa/NNN}" >test.txt Extended shell ...


4

You can use perl -lane 'print "@F[2..$#F] $F[0] $F[1]"' log_file to flip the log lines.


3

Try this. And play around with it a bit. echo -n "Press enter to use \"$in\" or enter a new value: " read userInput if [[ -n "$userInput" ]] then in=$userInput fi echo ... I will use $in. Error-checking (test -d, etc.) is up to you.... read is a bash builtin, so you can get help for it with the command help read (watch out, because you'll probably ...


3

From : http://stackoverflow.com/questions/14036116/convert-timestamp-to-datetime-with-sed with bit of scripting... ping -D localhost | while read row do awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d %H:%M:%S", substr($0,2,10))) }1' <<< "$row" done Runs as $ ping -D localhost | while read row; do awk '{ sub(/[0-9]{10}/, strftime("%Y-%m-%d ...


2

If the programs don't have any problem running in parallel, why not simply send them to the background: for i in $(cat $inlist/list.txt); do echo "$i" ( mri_binarize --i ${in}/${i}/mri/aseg.mgz --o ${out}/${i}/masks/cc.nii.gz --match 41 42 43 44 45 flirt -in ${out}/${i}/masks/cc.nii.gz -ref ${out}/${i}/T1.nii -out ...


2

Try the following... unset i; for f in *.pnm; do mv "$f" "img$((++i)).pnm";done


2

at is excellent tool for one-off commands. To run a program repetitively at the same times, however, the right tool is cron. Run crontab -e. It will open an editor. Add this line and save the file: 50 11 * * 2,4 /path/to/script This will run /path/to/script every Tuesday and Thursday at 11:50am. crontab runs programs in a limited environment. So, ...


2

You should write the output to a temporary file and then rename: for i in ./*.gz do gunzip -c "$i" | head -8000 | gzip > "$i.tmp" mv -f "$i.tmp" $(basename "$i") done That your version sometimes works is if you gunzip buffers enough while reading.


2

The issue is that you are not creating an array, you are creating a string. You can test this quite easily by attempting to print the first element of your array: $ array="$(find . -maxdepth 2 -type d -iname 'Season*' -print)"; $ echo $array ./Season 3 ./Season 1 ./Season 2 Looks good right? But if that were an array, you would be able to print each ...


2

If fbat insists on getting its input from the terminal and you want to automate, the solution is to use expect (or pexpect). Here is an example of an expect script which might automate your program: #!/usr/bin/expect -f spawn ./fbat expect ">>" send "load map leprmap.txt\r" expect ">>" send "load phe phe_dbpsim2e1.txt\r" expect ">>" send ...


2

Rather than use scp I'd use rsync for this task or even potentially rdiff-backup. $ rsync -avz /var/www/httpmydocs/ \ mega_me@mega_debian.com:/var/www/httpmydocs/. This will perform a similar backup as the scp command, however it'll only transfer the delta of files as they change overtime, or new files, skipping files that have already been copied. ...


2

No need to "invoke" the PrtSrn button, if you install ImageMagick then you can do the following in your script: import -window root output.png If you want a particular window you can try to find its id first with wmctrl (the following captures the Firefox window displaying this post, the grep-ed string has to be unique.): id=$(wmctrl -l | grep -F 'bash ...


2

Here's the same basic idea as Glenn's solution but in Perl: $ perl -F";" -lane '$"=";";print "@F[0..3];", $_ for @F[4..$#F]' file ID ;email ;role ;privilege ;access-to 8charID1 ;first.lastname@domain.org ;usr ;read ;finance 8charID1 ;first.lastname@domain.org ;usr ;read ;HR 8charID1 ;first.lastname@domain.org ;usr ...


2

awk -F';' -v OFS=';' ' { for (i=5; i<=NF; i++) print $1,$2,$3,$4,$i } ' file outputs ID ;email ;role ;privilege ;access-to 8charID1 ;first.lastname@domain.org ;usr ;read ;finance 8charID1 ;first.lastname@domain.org ;usr ;read ;HR 8charID1 ;first.lastname@domain.org ;usr ;read ;accounting 8charID1 ...


2

Use udev for mounting the hard drives. It will automate the task and make identification a lot easier. If connected device is a hard drive, there will be a $ID_ATA=1 variable in your script.


2

At a point in time where you know your USB drive is on /dev/sdc0 do the following: ls -l /dev/disk/by-uuid | grep -F /dev/sdc1 and note the number between the time (HH:MM) and the ->, lets assume its xyz. That xyz is the drives UUID it normally doesn't change (unless you make a new filesystem and don't provide this number again as an option). You can ...


2

You should make an entry in your /etc/fstab file (as per your other question) with a UUID=xyz or LABEL=somelabel identifying your drive instead of using /dev/sdc1 in your scripts (BTW, in the other post you use /dev/sdc0). On that line you also specify noauto,user as options, so that a normal user on the system can mount the drive. That takes care of the ...


2

sed ' s/ *[=;]"* */="/g;s//" /2 #first sub-out spaces; for " s|\(.*M\) \(.*\)|\ #then do the replace <wpt \2">\ <time>\1</time>\ </wpt>| ' <<\INPUT 11-06-2014 - 10:49:06PM lat = 41.858657; lon = -91.345142 11-06-2014 - 10:49:49PM lat = 42.864653; lon = -92.349914 11-06-2014 - 10:50:35PM ...


2

A mount point is the location within the filesystem hierarchy where a block device is mounted. In your case, /dev/mmcblk0p1 is the block device file and /run/media/ssuman/ANGSTROM is the mount point. Since it's a script file, it would be up to the script's authors/maintainers to document the command usage and optional parameters - there is no general rule ...



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