LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2019, 12:06 AM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862

Original Poster
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

Q: How do I handle options in bash-script?
A: An example:
Code:
while [[ "$1" =~ ^- ]]; do 
    Opt="$1"
    shift
    ... process Opt
done
Q: What would be a non-bash-only solution?
A: You can use 'case':
Code:
while true; do
    case "$1" in
    -*) ;;
    *)  break
    esac
    Opt="$1"
    shift
    ... process Opt
done
 
Old 03-19-2019, 11:47 PM   #17
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862

Original Poster
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Q: I wish to process data from an environment variable. What are some wrong ways to do it?
A: Let's see some:
Code:
MyVar='n e m o'; echo $MyVar | sed 's/n/N/' => N e m o
MyVar='n  e  m  o'; echo $MyVar | sed 's/n/N/' => N e m o BAD
MyVar='n  e  m  o'; echo "$MyVar" | sed 's/n/N/' => N  e  m  o
MyVar='-n'; echo "$MyVar" | sed 's/n/N/' => (empty) BAD
MyVar='-n'; printf '%s\n' "$MyVar" | sed 's/n/N/' => -N
So the last one is the safe solution, just don't forget: printf (unlike echo) doesn't automatically append newline-character.

Q: Is there a simpler, bash-only, non-POSIX way?
A: Try this:
Code:
MyVar='n  e  m  o'; sed 's/n/N/' <<<"$MyVar" => N  e  m  o
Mind you, this method does append a newline.

Last edited by NevemTeve; 03-20-2019 at 12:13 AM.
 
Old 03-20-2019, 12:16 AM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862

Original Poster
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Q: My enemies created a file with a new-line (or other control-character between 0x00..0x1f or 0x7f..0x9f) in the filename. What can I do with it?
A: You can delete it with the Midnight Commander (F8). Or in shell, somehow put its name into an environment variable, then you can rename it:
Code:
Var=$'evil\nfile\rname'
mv -- "$Var" "$(printf '%s' "$Var" | tr '\000-\037\177-\237' _ )"
ls -l evil_file_name
Note: In windows-12** or utf8, character-codes 0x80..0x9f are legal.

Last edited by NevemTeve; 03-20-2019 at 12:25 AM.
 
Old 02-16-2020, 09:10 AM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862

Original Poster
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Q: I seem to have permission problems in my PHP-script. How do I debug it?
A: Use functions like posix_getcwd, stat, posix_getuid, posix_getgid. Example:
PHP Code:
<?php
    
function printfilestat ($fname) {
        
$ststat ($fname);
        if (! 
$st) {
            
printf ("\n%s: 'stat' failed\n"$fname);
            return;
        }
        
$userdataposix_getpwuid ($st['uid']);
        
$groupdataposix_getgrgid ($st['gid']);
        
printf ("file '%s': owner=%d(%s) group=%d(%s) access=%o\n",
            
$fname,
            
$st['uid'], $userdata['name'],
            
$st['gid'], $groupdata['name'],
            
$st['mode']);
    }

    
printf ("<PRE>\n");
    
$uidposix_getuid ();
    
$userdataposix_getpwuid ($uid);
    
$gidposix_getgid ();
    
$groupdataposix_getgrgid ($gid);
    
$cwdposix_getcwd ();

    
printf ("posix_pwuid=%d(%s) posix_getgid=%d(%s)\n",
        
$uid$userdata['name'],
        
$gid$groupdata['name']);
    
printf ("posix_getcwd=%s\n"$cwd);

    
printfilestat ($cwd);
    
printfilestat (__FILE__);
    
printfilestat (dirname (__FILE__));

    
printf ("</PRE>\n");
?>
example output:
Code:
posix_pwuid=33(www-data) posix_getgid=33(www-data)
posix_getcwd=/local/home/projects/public_html
file '/local/home/projects/public_html': owner=1000(projects) group=1000(devel) access=40755
file '/local/home/projects/phptest/web/access.php': owner=1000(projects) group=1000(devel) access=100755
file '/local/home/projects/phptest/web': owner=1000(projects) group=1000(devel) access=40755

Last edited by NevemTeve; 02-16-2020 at 09:50 AM.
 
  


Reply

Tags
faq



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
Question: Incremental backups of large files that change frequently haertig Linux - Software 8 09-21-2013 08:27 PM
Begging: Repeated Argus question... how do I get it installed properly? Kayone Linux - Newbie 9 11-19-2009 08:55 AM
Repeated Disk Failure Question bercikr Linux - Hardware 2 11-25-2008 06:52 PM
Repeated RAID 5 Remount Problems After Power Failure HalNineThousand Linux - General 3 01-03-2006 01:18 PM
Repeated request to log in librano LQ Suggestions & Feedback 7 12-18-2005 08:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 PM.

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