I am trying to script my SCREEN access and automate as much as possible my connection to my bastion host.
here my bash code:
#!/bin/bash
# set TERM to xterm-256color
export TERM=xterm-256color
# here we source bashrc
. .bashrc
# Detecting Command Line Arguments
if [ "$1" != "" ]; then
# check if the screen argument exist
if ! screen -list | grep -q "$1"; then
# create screen with new argument
screen -S $1
# At this point, argument is not found on screen
else
# Create it with argument specified.
screen -x $1
# Detecting if default screen exist
elif [[ ! screen -list | grep -q "myscreen" ]]; then
# no default screen exist, Create it !
screen -S myscreen
else
# attache to the default screen
screen -x myscreen
fi
there the output:
$ ./myscreen.sh test123
./myscreen.sh: line 18: syntax error near unexpected token `elif'
./myscreen.sh: line 18: `elif ! screen -list | grep -q "myscreen" ; then'
I tried also to [[ ! EXPR ]] no much better.
any one have a idea?
I reviewed your script and saw that it is missing a fi before the elif. Of course, I write bash code everyday, so this was easy for me to spot. You can use a bash syntax checker to help review your scripts (www.shellcheck.net).
Here's the results provided by that site:
$ shellcheck myscript
Line 8:
if [ "$1" != "" ]; then
^-- SC1009: The mentioned syntax error was in this then clause.
Line 10:
if ! screen -list | grep -q "$1"; then
^-- SC1046: Couldn't find 'fi' for this 'if'.
^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.
Line 18:
elif [[ ! screen -list | grep -q "myscreen" ]]; then
^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.
$
You’re missing a fi statement to close out your internal if-else:
if
if
...
else
...
fi # You were missing this line
elif
...
else
...
fi
Related
I have a -d option in a bash script but I dont what it does
cp -r ${version} ${version}tm
[ -d "latest-oi" ] rm latest-oi #line 35
[ -d "latest-tm" ] rm latest-tm #line 36
ln -sf "${version}" latest-oi
ln -sf "${version}tm" latest-tm
and when I execute the script I have this error message, I don't know why
./script.sh: line 35: [: missing `]'
./script.sh: line 36: [: missing `]'
I would like to know what -d means and what it does and why there is an error when the script is executed, Thanks
I don't see any -d option to your bash, but to the command [. See man test for this command. If you want to have two commands in the same line, you have use a command separator between them. Valid command separators are a newline, a ;, a && or a ||.
If you write a
[ -d "latest-oi" ] && rm latest-oi
it means that the rm is executed if [ returns exit code zero. Another way to write it would be
test -d latest-oi && rm latest-oi
If you would use a || instead, the rm would be executed whenever [ returns non-zero exit code.
Hi everyone I need to check if a file exist with a shell script. I did some digging and ended up with this syntax but I'm not sure why it isn't working
(please bear in mind that you are talking to beginner)
I've found that you can add -e for example to check if it exist but I didn't get where these shortcuts came form or their names
#! /bin/bash
if [ "$#" = "1" ]
then
if [ -e $($1) ] && [ -f $($1) ]
then echo 'the file exists'
fi
fi
In idiomatic Bash:
#!/usr/bin/env bash
if [[ -f "${1-}" ]]
then
echo 'the file exists'
fi
Correct shebang
[[ rather than [
-f implies -e
No need for semicolons or single-use variables.
Please keep in mind that this does not tell you whether the file is a text file. The only "definition" of a text file as opposed to any other file is whether it contains only printable characters, and even that falls short of dealing with UTF BOM characters and non-ASCII character sets. For that you may want to look at the non-authoritative output of file "${1-}", for example:
$ file ~/.bashrc
/home/username/.bashrc: ASCII text
More in the Bash Guide.
#!/bin/bash
if [ "$#" == 1 ]; then
if [[ -e "$1" && -f "$1" ]]; then
echo 'The file exists';
fi
fi
You should put every conditional && between [[ ]] symbols otherwise it will be interpreted as execute if success.
#! /bin/sh
FILE=$1 # get filename from commandline
if [ -f $FILE ]; then
echo "file $FILE exists"
fi
See the fine manual page of test commands, which are built-in in the different shells: man test; man sh; man bash
You will also find many shell primers which explain this very nicely.
Or see bash reference manual: https://www.gnu.org/software/bash/manual/bash.pdf
Just learning bash and trying to implement a function in a script.
The script below runs thru ShellCheck fine, but I get a syntax error running bash from the command line
It must be the way I've defined my function, but I can't figure out the right way to do it - especially if it passes ShellCheck
error is on line 24
syntax error near unexpected token `${\r''
`autounload() {
Script:
#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
#
# Called from {SCRIPT_DIR}/usb-initloader.sh
# make sure to chmod 0755 on file
#
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
#
# CONFIGURATION
#
LOG_FILE="$1"
MOUNT_DIR="$2"
DEVICE="$3" # USB device name (from kernel parameter passed from rule)
#
# check for defined log file
if [ -z "$LOG_FILE" ]; then
exit 1
fi
#
# autounload function to unmount USB device and remove mount folder
#
autounload() {
if [ -z "$MOUNT_DIR" ]; then
exit 1
fi
if [ -z "$DEVICE" ]; then
exit 1
fi
dt=$(date '+%Y-%m-%d %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt"
sudo umount "/dev/$DEVICE"
sudo rmdir "$MOUNT_DIR/$DEVICE"
# test that this device isn't already mounted
device_mounted=$(grep "$DEVICE" /etc/mtab)
if ! "$device_mounted"; then
echo "/dev/$DEVICE successfully Un-Mounted"
exit 1
fi
}
autounload >> "$LOG_FILE" 2>&1
This is the part of the code where I start defining the function
#
autounload() {
if [ -z "$MOUNT_DIR" ]; then
exit 1
fi
I've tried to move function above and below where it gets called but it seems to makes no difference.
As discovered in comments, you have a Windows (DOS) file that then runs on a UNIX machine. This has the problem that the line endings in DOS are \r\n while in UNIX are \n, so there is a superfluous \r in every line.
To get rid of those you can use either of these:
In UNIX: dos2unix
In Windows: any of the tools described in Windows command to convert Unix line endings?
I am getting an error when I try and run my assignment.
#!/bin/bash
## Assignment 2
echo Please enter a User Name:
read u
if [ $u!="root"]; then
echo Searching for Username!
grep $u /etc/passwd|sed 's/$u/hidden/gi'
elif [ $u!="toor"]; then
echo Root is NOT allowed.
else
echo Toor is definetely NOT allowed.
fi
Output:
Please enter a User Name:
user1
./assign2.sh: line 6: [bthiessen: command not found
./assign2.sh: line 9: [bthiessen: command not found
Toor is definetely NOT allowed.
What is wrong with my if statements?
Try that :
#!/bin/bash
echo Please enter a User Name:
read u
if [[ $u != "root" ]]; then
echo Searching for Username!
grep "$u" /etc/passwd | sed "s/$u/hidden/gi"
elif [[ $u != "toor" ]]; then
echo Root is NOT allowed.
else
echo Toor is definetely NOT allowed.
fi
problems founds :
[ $u!="root"] need spaces around !=
if you use variables in sed, you need " quotes, not simple '
note :
[[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals . Unless you're writing for POSIX sh, we recommend [[
Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words
Whitespace counts here:
if [[ $u!="root" ]]; then
And:
elif [[ $u!="toor" ]]; then
Also prefer [[ over [.
if [ $u!="root"]; then
elif [ $u!="toor"]; then
There needs to be spaces inside the square brackets, and around the != operator. The whitespace is required. It's also good practice to quote "$u" in case the username has spaces or is blank.
if [ "$u" != "root" ]; then
elif [ "$u" != "toor" ]; then
There are other issues with your script which I suppose should be left to you to find.
To debug bash scripts, you can also use bash -x and set -x:
bash -x script.sh runs an existing script with debug messages, it will echo lines before executing them.
With set -x you can enable this behavior directly in your shell script, e.g. in the first line after the shebang. (This is kind of like echo on in Windows scripting.) set +x disables this option.
It is even possible, although hardly useful, to set -x in interactive shells.
This is all nicely explained in the Bash Guide for Beginners, under Debugging Bash scripts.
#!/bin/bash
for dir in /home/username/git/*/
do
for file in "$dir"/*
do
if [[ -f $file ]]
then
echo "$file"
fi
done
done
When I try to run it. I got
syntax error near unexpected toke' `do
'rocTest.sh: line 3: `do
Why?
Use "$file" (with quotes) consistently to deal with "problematic" file names; in particular if [[ -f $file ]] should be
if [[ -f "$file" ]] ...
Note that bash is not always in /bin (e.g. FreeBSD places it in /usr/local/bin); for wider portability, either use
#!/usr/bin/env bash
or #!/bin/sh and make sure to remove bash-isms (e.g. use checkbashisms on Debian/Ubuntu). E.g. write if test -f "$file" instead of [[ -f "$file" ]]