LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   User agnostic terminal commands (https://www.linuxquestions.org/questions/linux-newbie-8/user-agnostic-terminal-commands-4175446210/)

Longeron 01-18-2013 01:02 PM

User agnostic terminal commands
 
I'm trying to create a directory, and user agnostic command.

How can this be done? i.e.

/test1/Longeron/test.txt

or

/test1/ANYUSERHERE/text.txt

I'm basically looking for some kind of way to insert the current user in the place of some placeholder.

thesnow 01-18-2013 01:14 PM

$USER variable

Code:

lm ~ # mkdir $USER
+ mkdir root
lm ~ # ls
+ ls --color=auto
check_access.bash  instantclient  root  swap2ram.sh  www_backup.bash  www_backup.log


Longeron 01-18-2013 01:20 PM

Wow. So obvious. Thank you!

unSpawn 01-18-2013 01:40 PM

Please note that if you're going to do that you should "anchor" the directory to be created in a sensible location like /tmp or /var/tmp (not "/" or other top level directories), ensure the variable isn't messed with or empty, in short: watch your error handling. Also if these are temporary directories consider using mktemp instead.
Code:

# Empty variable or way too short user name:
[ ${#USER} -eq 0 -o ${#USER} -lt 2 ] || exit 1

# Local user exists:
getent passwd "${USER}" >/dev/null 2>&1 || exit 1

# Obviously it's better to break off ops but here's one way to populate a variable
# and make errors stand out creating a dot-directory with epoch as time stamp and
# break off if directory creation fails:
ERRORVALUE=".mkdir_error_$(/bin/date +'%s')"
mkdir /var/tmp/${USER:=$ERRORVALUE}/ || exit 1



All times are GMT -5. The time now is 12:57 AM.