LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 08-21-2018, 10:42 AM   #1
vysero
Member
 
Registered: May 2018
Posts: 137

Rep: Reputation: Disabled
Method to check if symlink from the current linux header directory to /usr/src/linux exists?


So I was given a script as text. Apparently the script will generate a symlink from the current linux header directory to /usr/src/linux. I took the text and created a file copied it into the file and saved it as script.py. I then tried running the script with a sudo ./script command but that doesn't work. So I tried saving the file as script without the .py extension and tried running ./script but that did not work either.

I am not sure why those attempts did not work but I tried just running it with an interpreter via a sudo python script and it might have worked? I have no way of knowing because I don't even know what a symlink is.
 
Old 08-21-2018, 11:08 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Number one is that you should consider learning what symbolic links are.

Freely available on the web and my personal explanation is that they are just a pointer to a file that is somewhere else in your system. Therefore you see the file in whichever directory you are visiting, and can view it, run it, etc, just the physical file is somewhere else.

You should post this script and place it within [code] tags to retain the formatting of it and then people can help you better. I'm not sure if the whole purpose of that script is just to create a symbolic link, it might be. Meanwhile there is a command line command which makes symbolic links in a single call, ln(1)

Finally. Since you were given this script. Ask the person who gave it to you, and make sure you trust the origin of anything before you try to run it.
 
Old 08-21-2018, 11:24 AM   #3
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
The script:

Code:
#!/usr/bin/env python 
import os 
import sys 
import subprocess
def _runningKernel():
    result = subprocess.check_output(['uname', '-r']).strip()
    return result
def _kernelHeaderPath(kernelName):
    return os.path.join('/', 'usr', 'src', 'linux-headers-%s' % kernelName)
_linkName = os.path.join('/', 'usr', 'src', 'linux')
_headersPath = _kernelHeaderPath(_runningKernel())
if not os.path.isdir(os.path.join(_headersPath, '.')):
    sys.stderr("Error: Did not the header files directory at %s\n" % _headersPath)
    sys.exit(1)
if (not os.path.islink(_linkName)) and os.path.exists(_linkName):
    sys.stderr("Error: %s is not a symlink.  (Cowardly refusing to overwrite it.)\n" % _headersPath)
    sys.exit(1)
if os.path.islink(_linkName):
    os.remove(_linkName);
os.symlink(_headersPath, _linkName)
Is there a way to check if the link was created?
 
Old 08-21-2018, 02:23 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Yes, you perform an "ls -l" in the directory where the link is supposed to be and look at the result.
 
1 members found this post helpful.
Old 08-21-2018, 02:34 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Use readlink
Code:
keithhedger@LFSBomb20:/media/BigBackup/TestRestore-> readlink "/usr/src/linux-4.15.3" 
/LFSSourceArchives/8.2/KERNEL/kernel-4.15.3_1/linux-4.15.3
keithhedger@LFSBomb20:/media/BigBackup/TestRestore-> readlink "/usr/src/linux-4.15.4" 
keithhedger@LFSBomb20:/media/BigBackup/TestRestore->
The 1st link above exists the second don't.
 
Old 08-21-2018, 02:54 PM   #6
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
Yes, you perform an "ls -l" in the directory where the link is supposed to be and look at the result.
rob@linux038:/usr/src$ ls -l
total 24
lrwxrwxrwx 1 root root 40 Aug 21 09:41 linux -> /usr/src/linux-headers-4.15.0-32-generic
drwxr-xr-x 27 root root 4096 Jul 20 15:13 linux-headers-4.15.0-29
drwxr-xr-x 8 root root 4096 Jul 20 15:13 linux-headers-4.15.0-29-generic
drwxr-xr-x 27 root root 4096 Aug 7 06:52 linux-headers-4.15.0-30
drwxr-xr-x 8 root root 4096 Aug 7 06:52 linux-headers-4.15.0-30-generic
drwxr-xr-x 27 root root 4096 Aug 15 06:27 linux-headers-4.15.0-32
drwxr-xr-x 8 root root 4096 Aug 15 06:27 linux-headers-4.15.0-32-generic


thanks
 
  


Reply



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
Deleted Entire usr/src/linux Directory JakeyHards Linux - Newbie 3 08-13-2009 09:05 AM
/usr/src/linux symlink GazL Slackware 8 02-18-2009 08:37 AM
invisible directory /usr/src/linux-2.6.21... PankajDS Linux - General 1 01-17-2008 01:28 PM
updating kernel, no '/usr/src/linux' directory? rubbercash SUSE / openSUSE 2 04-30-2005 01:00 PM
cd /usr/src cd linux no such file or directory Peacedog Slackware 2 12-31-2003 03:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:10 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
Open Source Consulting | Domain Registration