Sponsored Content
Top Forums Shell Programming and Scripting Getting canonical path with sed Post 302972750 by chebarbudo on Tuesday 10th of May 2016 10:46:35 AM
Old 05-10-2016
Getting canonical path with sed

Hi there,

Please read the preliminary notes:
Code:
readlink does the same ....... I know
realpath does the same ....... I know
awk can do it ................ I know
perl can do it ............... I know
You're losing time ........... I know

I wrote the following script that I think prints a canonical path without following symbolic links.
It's supposed to work with any filename including space, tabs, carriage return, etc.
Can anyone think of any path that wouldn't resolve to a canonical form with this script?

Thanks for your help.

Code:
filename=$1

# Squeeze double slashes.
filename="$(echo "$filename" | sed -r 's#/+#/#g')"

# Strip special '.' directory entries.
filename=$(echo "$filename" | sed -r ':a;s#^\./##;s#/\./#/#g;s#/\.$#/#;ta')

# Strip special '..' directory entries.
filename=$(echo "$filename" | sed -r ':a;$!N;$!ba;:b;s#^/\.\./#/#;s#/([^/.]|[^/.][^/]|[^/][^/.]|[^/]{3,})/\.\.(/|$)#/#;tb')

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED - How to return PATH from PATH/NAME

Hi, How can I get /usr/people/me/ from /usr/people/me/file.abc with sed? Thanks... (2 Replies)
Discussion started by: cybotic
2 Replies

2. Shell Programming and Scripting

Replace a filename with full path using sed

hi, i need to replace a line a file with a new raw device location.. original file.. /opt/sybase/ASE1502/ASE-15_0/bin/dataserver \ -d/data/TST_AKS1/sybdevices/master.dat \ -e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \ -c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \... (2 Replies)
Discussion started by: aksaravanan
2 Replies

3. Shell Programming and Scripting

need sed command to read a path and set to variable

I have a variable called PATH that contains a path example: /Users/rtipton/Desktop/testusers/test I need a sed command to set a variable called USER to the last directory name in that path PATH="/Users/rtipton/Desktop/testusers/test" and from that PATH i need USER to = test I know sed... (4 Replies)
Discussion started by: tret
4 Replies

4. Shell Programming and Scripting

Extract The File Path using SED

hi, I have a file path like usr/scripts/pass/bin and usr/scripts/pass/line I want to extract first three characters using sed Like for path usr/scripts/pass/bin i want to extract usr/scripts/pass and for path usr/scripts/pass/line i want to extract usr/scripts/pass (10 Replies)
Discussion started by: Diggi
10 Replies

5. Shell Programming and Scripting

How to get file path using sed

Hi , Hi I want to know how can i extract file path using sed? Eg: C:/folder1/abc/file.txt Now , i want C:/folder1/abc/ only, So that i can move to that directory containing that file . So how can i do it using sed? Thanks in Advance Sarbjit (2 Replies)
Discussion started by: sarbjit
2 Replies

6. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

7. Shell Programming and Scripting

sed/awk for extracting directory from file path

Hi, I have following path: set file_path = D:/forums/prac/somedir/new1/file1.txt or set file_path = E:/new/forums1/prac/somedir/new2/file2.txt I need to grep "somedir" from file path. In this case preceding directory "prac" remains same for both the paths, but directories preceding... (7 Replies)
Discussion started by: sarbjit
7 Replies

8. Shell Programming and Scripting

How to remove a directory path in a file using sed

Hi, I want to remove a directory path from a file starting with the bracket using sed command. eg. (cd /man/abc/def ; \ aaaaa bbbb (cd /man/aaaa/aa; \ op. aaaaa bbbb The "(cd /man" is to be consideres as the start. I tries the below thing, but it didnt worked. Can anyone help.... (3 Replies)
Discussion started by: vdhingra123
3 Replies

9. Shell Programming and Scripting

find and replace a path with sed?

Hi, I am attempting to find the path /subject/grin* and replace it with /subject/$i My attempt: for i in $(ls) do sed -e 's:"/subjects/grin.*":"/subjects/$i.*":g' and several variants with no luck. Please help. Thanks! (8 Replies)
Discussion started by: JC_1
8 Replies

10. UNIX for Dummies Questions & Answers

What is wrong with: echo $PATH | sed s/:/\\n/g

Hello all! I am on Mac (10.8.4) and my shell tcsh (man says version: Astron 6.17.00). Just to precise my tcsh: echo $LC_CTYPE UTF-8 I want to replace all ':' with a new line, to get all paths on one line. I don't find a way to make my shell accept the "\n" My start was: echo... (17 Replies)
Discussion started by: marek
17 Replies
IS_UPLOADED_FILE(3)							 1						       IS_UPLOADED_FILE(3)

is_uploaded_file - Tells whether the file was uploaded via HTTP POST

SYNOPSIS
bool is_uploaded_file (string $filename) DESCRIPTION
Returns TRUE if the file named by $filename was uploaded via HTTP POST. This is useful to help ensure that a malicious user hasn't tried to trick the script into working on files upon which it should not be working--for instance, /etc/passwd. This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system. For proper working, the function is_uploaded_file(3) needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work. PARAMETERS
o $filename - The filename being checked. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 is_uploaded_file(3) example <?php if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { echo "File ". $_FILES['userfile']['name'] ." uploaded successfully. "; echo "Displaying contents "; readfile($_FILES['userfile']['tmp_name']); } else { echo "Possible file upload attack: "; echo "filename '". $_FILES['userfile']['tmp_name'] . "'."; } ?> SEE ALSO
move_uploaded_file(3), $_FILES, See Handling file uploads for a simple usage example.. PHP Documentation Group IS_UPLOADED_FILE(3)
All times are GMT -4. The time now is 12:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy