So I have an extremely simple shell script, where it inputs an IP address and a domain name in the hosts file of a person's computer. For "development purposes only!" it works fine, it just inputs the IP address and domain name like so
192.168.53.215dev.env.os
192.168.53.215dev.source.os
Where I want it to input it like this:
192.168.53.215 dev.env.os
192.168.53.215 dev.source.os
So basically I want it to add space between the IP address and the domain name, unfortunately I can't really figure it out. Any help would be appreciated, this is a simple script though and shouldn't be cluttered with an over complicated script. Please keep it simple and explain exactly what you're doing I would like to learn it not have it done for me thanks!
Here is the code:
do shell script "/usr/bin/printf \"\\n# Add #####'s ip for Sourcebox\\n192.168.53.215\\dev.env.os\\n192.168.53.215\\dev.source.os\\n\" >> /etc/hosts; /usr/bin/dscacheutil -flushcache" with administrator privileges
Using the -e flag with echo in a bash script will allow you to output tabs.
echo -e "this\thas\ttabs"
The printf can be used as below mentioned for getting tab space
printf "\t"
Related
Is there anyway to stop in a bash_profile alias command and prompt for a user input? I'm fairly new to writing terminal commands.
I'm making a custom command that will take 2 inputs and edit my host files and set up vhosts for me on my local machine, at the moment i'm just passing the arguments into the command
addSite mywebsite.co.uk
But ideally I would like to be able to just run addSite, then the command stops and prompts 'Please enter the domain for your new site'.
Is this possible? If not, can someone point me in the right direction to be able to write a custom terminal command that can do this?
Thanks
You can ask for user input with read and pass a prompt with the -p flag. After the command, you specify the variable name. Then access it like any other variable.
read -p 'Please enter the domain for your new site: ' domain
echo Your domain name: $domain
EDIT: as pointed out by #tripleee in the comments, it's worth noting that this will have unintended side effects if a shell is launched from another source e.g. if you launch an executable file from Finder.
When I use terminal enter zsh, my computer name is garbled, n3-85-8 instead MacBook-Pro. Sometime so as the bash. Do anyone know why? And how to fix it.
There are two effects happening here:
Bash only reads the hostname (as displayed in the prompt) once at shell startup, which means you only see the change when you start a new shell, not when your hostname changes.
macOS by default changes its own hostname based on the network configuration
You can configure your computer not to change its hostname (see for example this question). Or, you can configure bash to use the computer's persistent LocalHostName in the prompt. This value does not change when you connect to a different network.
You can edit your ~/.bashrc (or related file) to have a line like:
PS1=$(scutil --get LocalHostName)':\W \u\$'
Extending Grisha's excellent answer, there are several different host names. Often the same, but may vary to accommodate different naming constraints. The whatami function (below) can help you choose which one you want in your PS1 prompt.
Here's a Bash function to help assess the different names.
function whatami {
local cn=$(scutil --get ComputerName)
local lhn=$(scutil --get LocalHostName)
local hn=$(scutil --get HostName)
local nbn=$(/usr/libexec/PlistBuddy -c "Print :NetBIOSName" /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist)
printf '\e[1mComputerName\e[0m: '"$cn"'\n'
printf '\e[1mLocalHostName\e[0m (Bonjour): '"$lhn"'\n'
printf '\e[1mHostName\e[0m: '"$hn"'\n'
printf '\e[1mNetBIOSName\e[0m (SMB): '"$nbn"'\n'
printf '\e[1mIP Address\e[0m: '
for x in $(ifconfig -l); do ipconfig getifaddr $x; done
}
There are a lot of tips (and warnings) on here for obfuscating various items within scripts.
I'm not trying to hide a password, I'm just wondering if I can obfuscate an actuall command within the script to defeat the casual user/grepper.
Background: We have a piece of software that helps manage machines within the environment. These machines are owned by the enterprise. The users sometimes get it in their heads that this computer is theirs and they don't want "The Man" looking over their shoulders.
I've developed a little something that will check to see if a certain process is running, and if not, clone it up and replace.
Again, the purpose of this is not to defeat anyone other than the casual user.
It was suggested that one could echo an octal value (the 'obfuscated' command) and use it as a variable within the script. e.g.:
strongBad=`echo "\0150\0157\0163\0164\0156\0141\0155\0145"`
I could then use $strongBad within the shell script to slyly call the commands that I wanted to call with arguments?
/bin/$strongBad -doThatThingYouDo -DoEEET
Is there any truth to this? So far it's worked via command line directly into shell (using the -e flag with echo) but not so much within the script. I'm getting unexpected output, perhaps the way I'm using it?
As a test, try this in the command line:
strongBad=`echo -e "\0167\0150\0157"`
And then
$strongBad
You should get the same output as "who".
EDIT
Upon further review, the addition of the path to the echo command in the variable is breaking it. Perhaps that's the source of my issue.
You can do a rotate 13 on any command you want hidden beforehand, then just have the the obfuscated command in the shell script.
This little bash script:
#!/bin/bash
function rot13 {
echo "$#" | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
}
rot13 echo hello, world!
`rot13 rpub uryyb, jbeyq!`
Produces:
rpub uryyb, jbeyq!
hello, world!
I have trying to make auto install shell script for Sun java web server.
But, from the start, have some trouble.
when I start up the setup manually, I need to press enter key several times to agree licenses.
but I don't know how to replace the enter key automatically on the shell script.
could you someone help me?
Use echo and pipes. Something like:
(echo; echo; echo) | my_setup
I am creating a script that I can run and it will simply ask me the common location name...i.e SEC-DF1 and it will fetch the ip of that site from within script. My problem is taking that IP and replacing
right=IP_ADDRESS
with
right=NEW_IP_ADDRESS
I need this so I can call the script as I will be changing the value of right so often for testing.
I have been messing with sed until someone mentioned awk...this stuff has such horrid documentation I keep getting all types errors or weird results on the test file I am messing with.
Since this is a straight forward substitution, I would just use sed:
sed -e 's/^right=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/right=192.168.1.92/' filename
This will match right= at the beginning of a line followed by an IP address and replace it with the IP of your choosing.
This command will modify your script:
NEW_IP_ADDRESS=101.102.103.104 sed -i "s/^(right=).*$/\\1$NEW_IP_ADDRESS/" script