Script Help to Write Part of Computer Name as txt File

dennisnardi
Contributor

Hello,

I'm trying to create a text file on my computers that will be the assumed user of the computer. I'm doing this so this text file can be read by another application.

My computers are named prefix-ID-suffix (ex. IT-ID-Mac2). The prefixes are different lengths of letters, as are the ID's, and the suffixes. Ideally I'd like to be able to get the computer name, read only the info between the hyphens, and put that in a text file.

I've been unsuccessful getting the reading between the hyphens to work and was wondering if anyone had suggestions? The script below is the basic format I was attempting to use.

#!/bin/sh

CompName=$(scutil --get ComputerName)
    echo $CompName

UserName=$(????)
    echo $UserName

echo "$UserName" > /tmp/test.txt
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III
#!/bin/bash

CompName=$(scutil --get ComputerName)
UserName=$(echo "$CompName" | awk -F'-' '{print $2}')

echo "$UserName"

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III
#!/bin/bash

CompName=$(scutil --get ComputerName)
UserName=$(echo "$CompName" | awk -F'-' '{print $2}')

echo "$UserName"

dennisnardi
Contributor

Ah, duh! Worked perfect, Thank you!