New answers tagged

1 vote

awk script replace value with increment with if

I propose this solution: $ awk -F'|' -v OFS='\t' '$2 ~ /^[0-9]{1,3}$/ { $2 = 1100 +(++c) } { print $1,$2 }' file d1000 1000 d1001 1101 d1002 1102 d1003 1103 d1004 d1005
user avatar
4 votes
Accepted

awk script replace value with increment with if

The error may be obvious with more consistent indentation: BEGIN { FS="|"; OFS="\t" } { n=1100 { if (length($2)!=4 && length($2)>0) { $2=++n } }; ...
user avatar
  • 72.1k
1 vote

Listing latest modified sub-directories

In zsh, you could do: for d (<->(NF)) print -rC1 -- $d/backup_<->(N/om[1]) For the newest¹ backup_xxx subdir of each yyy dir. Or: for d (<->(NF)) print -rC1 -- $d/backup_<->(N/...
user avatar
0 votes

Copy/Paste files based on conditions

// copy from /a, and paste into /d and /e, each file in /a that is not found in either /b or /c #!/bin/bash for f in $(find /a ! -type d) ; do \ d=$(dirname "$f|sed -e 's/\/a//g') b=$(...
user avatar
  • 702
1 vote
Accepted

extract tar then enter its directory (shell scripting)

A shell script can never change the working directory for its parent process (the shell invoking the script). So although you may change to the created directory in the script, as soon as the script ...
user avatar
  • 267k
0 votes

how to compare all files inside ONE directory in bash?

Comparing many files in pairs grows cumbersome very quickly. Ten files requires 45 comparisons. 100 files takes almost 5000. My test set is 595 files (totalling 10 GB), which would require over 175,...
user avatar
  • 6,547
0 votes

how to hash a directory

Let the system do the heavy lifting: diff --recursive --brief dir1 dir2 # diff -qr dir1 dir2
user avatar
  • 88.9k
1 vote

how to compare all files inside ONE directory in bash?

Yes, you do need two loops. But it looks like you don't need diff because you're discarding the diff output to /dev/null, you can use cmp instead. For example: #!/bin/bash # Read the list of files ...
user avatar
  • 66.6k
1 vote

One master ksh file to call 2 ksh script files

Take whatever you do to call the two scripts by hand from the command line: perhaps you cd to some directory; maybe you run ksh file1.ksh, or they are executable: /path/to/file1.ksh. Copy those ...
0 votes

Add something to crontab programmatically (over ssh)

To add new job from terminal (crontab -l 2>/dev/null; echo "25 2 * * * command/script") | crontab - To modify existing cron job entry from terminal (crontab -l | sed -e 's|...
user avatar
  • 1
0 votes

How to get whole command line from a process?

pgrep -a <name> In your example : pgrep -a udhcpc pgrep does not truncate line, but ps does. For those that insist on using ps, then do that to avoid truncation : ps <processID> | tee
user avatar
0 votes

Monitor Process Output For Scripts

Is there a way to view the console outputs of a particular script that is running in the background on a machine? There is now, via a brand new utility called pw (Pipe Watch). How it would go is like ...
user avatar
  • 6,656
1 vote

Display the IP addresses of all currently configured interfaces

if we are talking about Linux, this one-liner in bash maybe helps you: ip -br -4 address | awk '{ print $1 " interface has the ip address " $3}'
user avatar
1 vote

Scripting the partition shrinking

I understood what was wrong: First, sfdisk accept the size of the partition, not the increment, so the + sign is wrong. One difference from fdisk is that the end is the sector number from the ...
user avatar
  • 111
-2 votes

Display the IP addresses of all currently configured interfaces

Make yourself familiar with the concept of pipes. Then use the commands ip, and some bash builtins like read and while to filter and enhance the output. See man bash.
user avatar
  • 1,929
-1 votes

Shell script backup directory and check file if exists

#!/bin/bash dat=$(date +%Y-%m-%d) echo $dat if [[ -f /tmp/test_$dat ]] then echo "File test_$dat already exsists proceeding to sync the file with source file" rsync sourcefile /tmp/test_$dat ...
user avatar
-1 votes

How can I test if nginx configuration files are valid within a Bash script?

@DeeEff Above bash script whether that will work nginx container to cloud vm or cloud vm to nginx container Example sudo containername -t bash program/script that can check the Nginx service. The ...
user avatar
0 votes

Have backticks (i.e. `cmd`) in *sh shells been deprecated?

As a note, your title is a little misleading and different from your question, so let's also answer the question in your title (and in no way disagree with the correct answers regarding just the sh/...
user avatar
0 votes
Accepted

How do I notify-send a long command?

The command takes one or two arguments: SYNOPSIS notify-send [OPTIONS] {summary} [body] So, you'll simply wrap the result of your command in double quotes: while :; do notify-send "$(...
user avatar
1 vote
Accepted

Switch case with for loop for matching string

It looks as if you just want to get the part before the first _ in the string read from new.txt and use that in a call with curl. #!/bin/bash headers=( -H 'Content-Type: application/json' ) baseurl='...
user avatar
  • 267k
0 votes

Using ntfs-3g, how to parse Windows extended attributes automatically?

It would be useful if you included the output it gives. But in the meantime, maybe this code will help you out. Perhaps this function specifically: line 46: def print_getfattr EDIT: Okay, after your ...
user avatar
  • 101
2 votes
Accepted

Read file identify string and specific command only

You can use case switch. Something like: while IFS= read -r line do case $line in gl-system-events_2) commandA ;; gl-events_0) commandB ;; *) ...
user avatar
  • 1,163
1 vote
Accepted

reload the i3 conf in a script

Per this page, these commands seem to be available: i3-msg reload i3-msg restart
user avatar
  • 3,292
0 votes

How to check if variable is integer (avoid problem with spaces around) in POSIX shell script?

I'm not 100% certain if octal and hexadecimal support is is available in POSIX sh, but it is in busybox ash. In this case, a more complete solution would be as follows: is_integer() { local n=${1#...
user avatar
0 votes

Is the output mixed like this because of xargs and how can I fix it?

How can I fix this problem? Redirect stdout to a file for each process and cat all of them later, to demo this technique: seq 100 | sort -R | xargs -I@ -P0 sh -c '{ echo begin @ && sleep 0.@...
user avatar
  • 1,039
1 vote
Accepted

bashscript for quick change i3 mod key

The main problem is your sed is missing the -i flag which would allow it to edit the file in place, but your method is unsafe — if you ran the script without a valid command multiple times you’d ...
user avatar
  • 3,292

Top 50 recent answers are included