0

I am adding some rules to an application. I want to make a bash script which would take my input, for example: 'abc', add a prefix 'pqr' and a suffix 'xyz' to the input and send it as a command.

In this case the command to be sent would be pqrabcxyz without any spaces as I didn't add them.

The script I am using is

#!/bin/bash
read -p "ID to be banned: " cmd
"iptables --append INPUT --match string --algo kmp --string '${cmd}' --jump DROP -p udp"
  • Sample of real input (is it form a file? How are you getting this input?) and expect result would help the most. Any code you have tried will also let us know what your skill level is really like too. – Tigger Dec 6 '17 at 6:00
1
#!/bin/bash
read -p "Please type in your command" cmd
pqr"${cmd}"xyz

Based on the limited information in the question, this should be enough. I hope that @ayush-mishra can be more specific.

Answer edited thanks to reminder by @grgarside

  • The Script I am Using is: #!/bin/bash read -p "ID to be banned: " cmd "iptables --append INPUT --match string --algo kmp --string '${cmd}' --jump DROP -p udp" – Ayush Mishra Dec 6 '17 at 7:10
  • But It is Not working. It Shows no command found – Ayush Mishra Dec 6 '17 at 7:10
  • You have to be root to run iptables. Switch to root first with su or use sudo in a proper way so that iptables is invoked as root. You may also need to change iptables to /sbin/iptables – Weijun Zhou Dec 6 '17 at 7:55
  • Its not working – Ayush Mishra Dec 6 '17 at 8:54
  • @Ayush Don't use "…" around the iptables command. – grg Jan 6 '18 at 14:14
0

You can try with below command

Method1

echo "abc" | sed "s/^/pqr/g" | sed "s/$/xyz/g"

Method2


#!/bin/bash
j="pqr"
z="xyz"
echo "abc" | sed "s/^/"$j"/g" | sed "s/$/"$z"/g"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.