In this if condition for shell script, i want print user full name + exist(if the file ..../pub_key.asc found)or not exist if the mentioned file not found.
for file in $FILES
do
if [ -f /home/$file/public_html/pub_key.asc ]; then
echo $(cat /home/$file/etc/passwd | head -n | cut -d: -f1) : Exists
else
echo $(cat /home/$file/etc/passwd | head -n | cut -d: -f1) : Not exists!
fi
done
But i get errors because there is something wrong with first echo in if..condition.
Can someone show tell me what i did worng in the echo statement? and how to fix? appreciate your suggestions. :)
You are probably getting an error due to head -n command
head -n has to be followed by a number, e.g head -n 1, or head -1
Related
i am using WSL (Ubuntu) in Windows. i used bash script.sh for the script below:
#! /bin/sh
#################LOAD FILES###################
lead_SNPs=`grep "lead_SNPs" ../prep/files.txt | cut -f2`
bfile=`grep -w "bfile" ../prep/files.txt | cut -f2`
bfile_list=`grep -w "bfile_list" ../prep/files.txt | cut -f2`
r2=`grep "r2" ../prep/parameters.txt | cut -f2`
###############LD###########################
if [ ${bfile} = "NA" ]; then
cat ${bfile_list} | while read line; do
file=${line}
file_n=`echo $file |awk -F '/' '{print $NF}'`
echo 'Calculating LD'
plink --bfile ${file} --r2 --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
done
else
file=${bfile}
file_n=`echo $file |awk -F '/' '{print $NF}'`
echo ${file_n}
plink --bfile ${file} --r2 --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
fi
but i get this error
Syntax error near unexpected token `fi`
can you correct my code please? i can not understand where i made mistake.
Try to change:
if [ ${bfile} = "NA" ]; then
to
if [ "${bfile}" = "NA" ]; then
I suspect ${bfile} is empty which expanded to:
if [ = "NA" ]; then
in your original line.
The first line in your script doesn't look right.
It should be
#!/bin/sh
or if using bash shell:
#!/bin/bash
I would like to make array which put users in a time using for loop. For example:
y[1]="user1"
y[2]="user2"
...
y[n]="usern"
I tried to do it like this
#!/bin/bash
x=$(who | cut -d " " -f1 | sort | uniq | wc -l)
for (( i=1; i<=$x; i++ )); do
y[$i]=$(who | cut -d " " -f1 | sort | uniq | sed -n '$ip')
p[$i]=$(lsof -u ${y[$i]} | wc -l)
echo "Users:"
echo ${y[$i]}
echo -e "Number of launched files:\n" ${p[$i]}
done
Most likely I'm using command "sed" wrong.
Can you help me?
Indeed your sed command seems to be a bit off. I can't really guess what you're trying to do there. Besides that, I'm wondering why you're executing who twice. You can make use of the data first obtained in the following manner.
#!/bin/bash
# define two arrays
y=()
p=()
#x=0
while read -r username; do
y+=("$username")
p+=($(lsof -u $(id -u "$username") | wc -l))
echo -e "User:\n${y[-1]}"
echo -e "Open files:\n${p[-1]}"
# The -1 index is the last index in the array, but you
# could uncomment the x=0 variable and the line below:
#((x++))
done <<< $(who | cut -d " " -f1 | sort | uniq)
echo "Amount of users: $x"
exit 0
I am using below command to find a most recent file with name "candump"
ls *.log | grep "candump" | tail -n 1
The output is "candump-2018-04-19_131908.log"
I want to store the output filename to a variable in my shell script. I am using the below commands:
logfile = `ls *.log | grep "candump" | tail -n 1`
and
logfile = $(ls *.log | grep "candump" | tail -n 1)
However, both times I am getting the same error, "logfile: command not found". Am I doing something wrong? Any help is appreciated.
You have to stick the variable name and its value (no space before and after the =).
Try :
logfile=$(ls *.log | grep "candump" | tail -n 1)
This is working for me.
#!/bin/bash
my_command='ls | grep server.js | wc -l';
my_data=$(eval "$my_command");
echo "value in echo is:" $my_data;
if [ $my_data == "1" ]; then
echo "value is equal to 1";
else
echo "value is not equal to 1";
fi
I want to add some users who are in this file like:
a b
c d
e f
firstname lastname always
#!/bin/bash
Lines=$(cat newusers.txt | wc -l)
first=$(cat newusers.txt | awk '{print $1}')
last=$(cat newusers.txt | awk '{print $2}')
#test
echo $Lines;
echo $first;
echo $last;
until [ -z $1]; then
useradd - m -d /home/$1 -c "$1 + $2" $1
fi
before loop it works fine but I can't add newline.
The echo shows a c e and second for lastname b d f.
I tried to add newline in but it doesn't works.
What can i use for this?
Because I guess I can't add the user because of the newline problem.
I also searched on stackoverflow to find out a way to check if the user already exists by /dev/null but which variable do i have to use for it?
It's easier to process the file line by line:
while read first last ; do
useradd -m -d /home/"$first" -c "$fist + $last" "$first"
done < newusers.txt
I do not understand what you mean to do by your code, but if you want to read the file line by line and get the values of different fields then you can use the following code snippet:
#!/bin/bash
filename="newusers.txt"
while read -r line
do
fn=$( echo "$line" |cut -d" " -f1 )
ln=$( echo "$line" |cut -d" " -f2 )
echo "$fn $ln"
done < "$filename"
Note: You cannot add users the way you want to using bash script; since you will be prompted for password which must be supplied using tty you can use expect to program it; or use system calls.
The same way it’s possible to write a file that autoextracts itself, I’m looking for a way to autorun a program within a script (or whatever it needs). I want the program part of the script, because I just want one file. It’s actually a challenge: I have a xz compressed program, and I wanna be able to run it without any intervention of the xz program by the user (just a ./theprogram).
Any idea?
Autorun after doing what? Login? Call it in ~/.bashrc. During boot? Write an appropriate /etc/init.d/yourprog and link it to the desired runlevel. Selfextract? Make it a shell archive (shar file). See the shar utility, http://linux.die.net/man/1/shar
Sorry but I was just thinking... Something like this would not work?
(I am assuming it is a script...)
#!/bin/bash
cat << 'EOF' > yourfile
yourscript
EOF
chmod +x yourfile
./yourfile
Still, it's pretty hard to understand exactly what you are trying to do... it seems to me that the "autorun" is pretty similar to a "call the program from within the script"..
I had written a script for this. This should help:
#!/bin/bash
set -e
payload=$(cat $0 | grep --binary-files=text -n ^PAYLOAD: | cut -d: -f1 )
filaname=`head $0 -n $payload | tail -n 1 | cut -d: -f2-`
tail -n +$(( $payload + 1 )) $0 > /tmp/$filaname
set +e
#Do whatever with the payload
exit 0
#Command to add payload:
#read x; ls $x && ( cp 'binary_script.sh' ${x}_binary_script.sh; echo $x >> ${x}_binary_script.sh; cat $x >> ${x}_binary_script.sh )
#Note: Strictly NO any character after "PAYLOAD:", not even newline...
PAYLOAD:
Sample usage:
Suppose myNestedScript.sh contains below data:
#!/bin/bash
echo hello world
Then run
x=myNestedScript.sh; ls $x && ( cp 'binary_script.sh' ${x}_binary_script.sh; echo $x >> ${x}_binary_script.sh; cat $x >> ${x}_binary_script.sh )
It will generate below file, which you can directly execute. Upon executing below file, it will extract myNestedScript.sh to /tmp & run that script.
#!/bin/bash
set -e
payload=$(cat $0 | grep --binary-files=text -n ^PAYLOAD: | cut -d: -f1 )
filaname=`head $0 -n $payload | tail -n 1 | cut -d: -f2-`
tail -n +$(( $payload + 1 )) $0 > /tmp/$filaname
set +e
chmod 755 /tmp/$filaname
/tmp/$filaname
exit 0
PAYLOAD:myNestedScript.sh
#!/bin/bash
echo hello world