0

Below is my code to which i'm trying to pass a 3 argument like "client name with datacenter" hostname ip

so, "client name with Datacenter" is the 1st argument likewise hostname and ip is 2nd and 3rd argument.

is there any way i could pass the first argument as it is with spaces, as of now it's now not working as expected, because after space every word it's taking as different argument.

Actual issue is i want to pass this whole sentence as argument. "Actual Servers to be added". Is there any way i could do this? So this argument will be further used to look into a text file and search the string where the additional arguments $2 and $3 will get add.

#! /bin/bash

user=whoami #file1=/home/xymon/server/bin/ghostlist.cgi logfile=logfile1 ###Local Log file###### client="$@" hostname=$2 ip=$3 ####taking backup while editing hosts file everytime##### #cp -pr /home/xymon/server/etc/hosts.cfg "/home/xymon/server/etc/hosts.$(date +"%Y%m%d")" echo $ip echo $hostname

echo "hostfile is modified @ date" >>$logfile printf '%s\n' 'g/"'$client'"/+2i' ''$ip' '$hostname' # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"' 'wq' | ed -s hosts

  • It's unclear how your call your script, but if you quote the first argument, it should be available as "$1" in your script. Could you clarify your issue? – Kusalananda Dec 17 '20 at 20:42
  • You say “now it's now working”. Is that a typo for “now it's *not* working”? … … … … … … Please do not respond in comments; [edit] your question to make it clearer. – G-Man Says 'Reinstate Monica' Dec 17 '20 at 22:46
  • its a shell script so we are executing it by using ./script name. – Varun Dogra Dec 21 '20 at 07:27
  • @VarunDogra But the script that you show takes at least three parameters, so you can't execute it using just ./script. See also my first comment again. – Kusalananda Dec 21 '20 at 07:51
  • yes correct.. i'm executing it with three arguments. – Varun Dogra Dec 21 '20 at 08:14
  • ./script-name "Actual Servers to be added" hostname ip <--- in this way.. here when i'm executing in this way. the first string "Actual Servers to be added" is causing issue. The script is not taking the whole line as argument. – Varun Dogra Dec 21 '20 at 08:16
  • It is, but you never look at "$1" in your code. You use "$@" which is all arguments. – Kusalananda Dec 21 '20 at 12:14
  • if i use $i instead of $@, it will give me same output. – Varun Dogra Dec 21 '20 at 12:19
  • printf '%s\n' 'g/"'$client'"/+2i' ''$ip' '$hostname' # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"' 'wq' | ed -s hosts – Varun Dogra Dec 21 '20 at 12:20
  • here in this , $client is the string, but it's not taking whole string "title AHS - abct CBTS Linux Servers" as argument. – Varun Dogra Dec 21 '20 at 12:22

0 Answers0