简体   繁体   中英

How can I get my bash script to remove the first n and last n lines from a variable?`

I'm making a script to preform "dig ns google.com" and cut off the all of the result except for the answers section.

So far I have:

#!/bin/bash

echo -n "Please enter the domain: "
read d
echo "You entered: $d"
dr="$(dig ns $d)"
sr="$(sed -i 1,10d $dr)"
tr="$(head -n -6 $sr)"
echo "$tr"

Theoretically, this should work. The sed and head commands work individually outside of the script to cut off the first 10 and last 6 respectively, but when I put them inside my script sed comes back with an error and it looks like it's trying to read the variable as part of the command rather than the input. The error is:

sed: invalid option -- '>'

So far I haven't been able to find a way for it to read the variable as input. I've tried surrounding it in "" and '' but that doesn't work. I'm new to this whole bash scripting thing obviously, any help would be great!

you're assigning the lines to variables, instead pipe them for example

seq 25 | tail -n +11 | head -n -6

will remove the first 10 and last 6 lines and print from 11 to 19. in your case replace seq 25 with your script

dig ns "$d" | tail -n +11 | head -n -6

no need for echo either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM