LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-18-2013, 01:02 PM   #1
Longeron
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 01-18-2013, 01:14 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
$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
 
1 members found this post helpful.
Old 01-18-2013, 01:20 PM   #3
Longeron
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Wow. So obvious. Thank you!
 
Old 01-18-2013, 01:40 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Low Level commands (embedded Linux, terminal commands) JonCombat Linux - Newbie 2 01-05-2013 02:34 PM
[SOLVED] Terminal commands working in terminal but not in a script GreYFoXGTi Linux - Newbie 1 08-04-2010 09:36 AM
making a script that opens terminal and enter commands into that terminal Cinematography Linux - General 8 12-16-2008 10:34 AM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - Software 2 07-02-2008 06:26 PM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - General 2 07-01-2008 06:53 AM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration