Invalid aliases in linux shell - linux

I'd like to run some script.
It need alias to modify command.
but it seem invalid.
ex:
root#161310ea476b:/tmp# cat test.sh
#!/bin/bash
ls /tmp
root#161310ea476b:/tmp#
root#161310ea476b:/tmp# ./test.sh
test.sh
root#161310ea476b:/tmp# . test.sh
test.sh
now I used alias
root#161310ea476b:/tmp# shopt -s expand_aliases
root#161310ea476b:/tmp# alias ls="ls -al"
it works.
root#161310ea476b:/tmp# . test.sh
total 12
drwxrwxrwt 2 root root 4096 Jun 21 09:41 .
drwxr-xr-x 21 root root 4096 Jun 21 09:39 ..
-rwxr-xr-x 1 root root 22 Jun 21 09:41 test.sh
but this case wasn't works. alias seem invalid.
root#161310ea476b:/tmp# ./test.sh
test.sh
root#161310ea476b:/tmp#
How to fix this problem?
Thanks.

Aliases are not inherited by the subshell used to execute your script.
Please see the answer provided here:
Aliases in subshell
Alternatively, you could create your alias as a function (instead of an alias), export it, and THEN run your script.
Create a function...
function ls() {
/bin/ls -al
}
...export it...
export -f ls
...and run your script
. ./test.sh
Of course, don't forget you've created a function ls. (Note: it should only exist as long as the shell you exported it from --- and any subshells from it --- is/are open).

Related

Why cannot root writes on a file that it owns and has write access to?

I need to write to a.txt. The file is owned by root with a read-write access. But still I cannot write over it with a sudo. Why?
% ls -l
total 8
-rw-r--r-- 1 root staff 6 Mar 24 00:30 a.txt
% sudo echo "hi" >> a.txt
zsh: permission denied: a.txt
The redirection happens before the commands are run, i.e. using the original user.
Work-around:
sudo sh -c 'echo "hi" >> a.txt'

Why file is not created as owned by a specific user i designated

I have a php script that will collection data and write log into a file, the directory belongs to an user called 'ingestion' and a group called 'ingestion'. I was using the command
sudo -u ingestion php [script] &>> /var/log/FOLDER/adapter.log
The owner and group of FOLDER is ingestion. However, the created adapter.log still belongs to root user and root group, how is this possible?
Your file is created by the bash running as root, not by the process that you run via sudo as ingestion.
That's because the >> foo is part of the command line, not of the process started by sudo.
Here:
#foo.sh
echo foo: `id -u`
Then:
tmp root# sudo -u peter bash foo.sh > foo
tmp root# ls -l foo
-rw------- 1 root staff 9 Mar 2 18:52 foo
tmp root# cat foo
foo: 501
You can see that the file is created as root but the foo.sh script is run as uid 501.
You can fix this by running e.g.:
tmp root# sudo -u peter bash -c "bash foo.sh > foo"
tmp root# ls -l foo
-rw------- 1 peter staff 9 Mar 2 18:54 foo
tmp root# cat foo
foo: 501
In your case, of course, replace "..." with your php command.

why sh softlink to bash doesn't work? [duplicate]

I have a shell script which uses process substitution
The script is:
#!/bin/bash
while read line
do
echo "$line"
done < <( grep "^abcd$" file.txt )
When I run the script using sh file.sh I get the following output
$sh file.sh
file.sh: line 5: syntax error near unexpected token `<'
file.sh: line 5: `done < <( grep "^abcd$" file.txt )'
When I run the script using bash file.sh, the script works.
Interestingly, sh is a soft-link mapped to /bin/bash.
$ which bash
/bin/bash
$ which sh
/usr/bin/sh
$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 9 Jul 23 2012 /usr/bin/sh -> /bin/bash
$ ls -l /bin/bash
-rwxr-xr-x 1 root root 648016 Jul 12 2012 /bin/bash
I tested to make sure symbolic links are being followed in my shell using the following:
$ ./a.out
hello world
$ ln -s a.out a.link
$ ./a.link
hello world
$ ls -l a.out
-rwx--x--x 1 xxxx xxxx 16614 Dec 27 19:53 a.out
$ ls -l a.link
lrwxrwxrwx 1 xxxx xxxx 5 May 14 14:12 a.link -> a.out
I am unable to understand why sh file.sh does not execute as /bin/bash file.sh since sh is a symbolic link to /bin/bash.
Any insights will be much appreciated. Thanks.
When invoked as sh, bash enters posix
mode after the startup files are read. Process substitution is not recognized in posix mode. According to posix, <(foo) should direct input from the file named (foo). (Well, that is, according to my reading of the standard. The grammar is ambiguous in many places.)
EDIT: From the bash manual:
The following list is what’s changed when ‘POSIX mode’ is in effect:
...
Process substitution is not available.

How to display column headers for 'ls -l' command in unix/linux?

I want to display all the column headers when I type ls -l command in bash shell in unix/linux
When we type ls -ltr on command prompt we get something like the following.
-r--r--r-- 2 makerpm root 1898 Jan 28 14:52 sample3
-r--r--r-- 2 makerpm root 1898 Jan 28 14:52 sample1
What I want is to know whether ls has any options to display with column headers:
File_Permissions Owner Group Size Modified_Time Name
-r--r--r-- 2 makerpm root 1898 Jan 28 14:52 sample3
-r--r--r-- 2 makerpm root 1898 Jan 28 14:52 sample1
exa is a replacement/enhancement for ls. If you pass on the arguments -lh with exa, it will include a header row printing the column names like so:
exa -lh
Example output:
Permissions Size User Date Modified Name
.rwx------ 19 username 29 Sep 11:25 dont_cra.sh
drw-r----- - username 29 Sep 11:26 f1
.rw-r--r--# 811k username 29 Sep 11:25 row_count.dat
.rw-r--r-- 54 username 29 Sep 11:25 some_text.txt
You can set up an alias in .bashrc that replaces ls with exa.
The last answer using sed was slick, but unfortunately, if you have color added to your output (which most people do) it removes all color. I would like to suggest a better way, and ironically, simpler too.
First off, my .bashrc USED to have the following:
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
fi
alias ls='ls -AFhls --color --group-directories-first'
To be honest, you don't need the dircolors part, that is just a little extra I use, you could have something as simple as:
alias ls='ls --color=auto'
I wanted column headers too, Googled it, and wound up here. However after I tried what the previous user suggested, with sed and realizing everything was white, and my colors have all gone away.
That's when I tried something different in my .bashrc file, and it worked.
Simply alias ls, echo first, then place a semi-colon, then your ls command.
My .bashrc file now has the following line.
alias ls='echo "Dir Size|Perms|Link Count|Owner|Group|Size|Mod. Time|Name"; ls -AFhls --color --group-directories-first'
When doing it this way, utilizing echo instead of sed, all colors continue to work.
Not sure if this is specific to my terminal's output but, this worked for me.
And this is the output it yields. As you can see, it preserves color using this method. Just be sure to keep the ${1} variable in double quotes so files and directories with spaces in the name won't cause an error.
Here is the code so you can copy and paste for testing.
long_ls() {
local VAR="Permissions|Owner|Group|Size|Modified|Name"
if [ ! "${1}" ]; then
echo -e "$VAR" | column -t -s"|" && ls -l
else
echo -e "$VAR" | column -t -s"|" && ls -l "${1}"
fi
}
alias lls=$"long_ls ${1}"

Using sed within "while read" expression

I am pretty stuck with that script.
#!/bin/bash
STARTDIR=$1
MNTDIR=/tmp/test/mnt
find $STARTDIR -type l |
while read file;
do
echo Found symlink file: $file
DIR=`sed 's|/\w*$||'`
MKDIR=${MNTDIR}${DIR}
mkdir -p $MKDIR
cp -L $file $MKDIR
done
I passing some directory to $1 parameter, this directory have three symbolic links. In while statement echoed only first match, after using sed I lost all other matches.
Look for output below:
[artyom#LBOX tmp]$ ls -lh /tmp/imp/
total 16K
lrwxrwxrwx 1 artyom adm 19 Aug 8 10:33 ok1 -> /tmp/imp/sym3/file1
lrwxrwxrwx 1 artyom adm 19 Aug 8 09:19 ok2 -> /tmp/imp/sym2/file2
lrwxrwxrwx 1 artyom adm 19 Aug 8 10:32 ok3 -> /tmp/imp/sym3/file3
[artyom#LBOX tmp]$ ./copy.sh /tmp/imp/
Found symlink file: /tmp/imp/ok1
[artyom#LBOX tmp]$
Can somebody help with that issue?
Thanks
You forgot to feed something to sed. Without explicit input, it reads nothing in this construction. I wouldn't use this approach anyway, but just use something like:
DIR=`dirname "$file"`

Resources