- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell programming: Search a blank
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 12:42 AM
08-06-2001 12:42 AM
Shell programming: Search a blank
I'm creating a shell script, and I want to execute a command only if there is a blank in ${FILE}.
How could I carry it out?
if _?_?_?_ then
[command]
fi
Thank you very much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 12:55 AM
08-06-2001 12:55 AM
Re: Shell programming: Search a blank
if ! [ -s $FILE ] ; then
...
fi
but if you mean a single space as the only line, then:
if [ `cat $FILE | wc -w` -eq 0 ] ; then
...
fi
could be one way to do it.
Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 01:15 AM
08-06-2001 01:15 AM
Re: Shell programming: Search a blank
if [ `grep -c " " $FILE`] ; then
...
fi
Hope this helps
Regards Stefan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 01:26 AM
08-06-2001 01:26 AM
Re: Shell programming: Search a blank
If you are looking for a blank line in a file
while read each_line;do
echo $each_line | grep " " > /dev/null
if [ $? -eq 0 ];then
echo $each_line
.
.
.
fi
done
where a.lst in the input file
..PRB...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 01:26 AM
08-06-2001 01:26 AM
Re: Shell programming: Search a blank
if
echo $FILE | grep " "
then
...
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 02:13 AM
08-06-2001 02:13 AM
Re: Shell programming: Search a blank
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 02:24 AM
08-06-2001 02:24 AM
Re: Shell programming: Search a blank
./myscript.sh[43]: test: A ] character is missing.
grep: can't open ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 02:40 AM
08-06-2001 02:40 AM
Re: Shell programming: Search a blank
if [ `echo $FILE | wc -w` -gt 1 ]
then
commands
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 02:42 AM
08-06-2001 02:42 AM
Re: Shell programming: Search a blank
Try this one.
FILE="File Name" #Let's say var is like this.
echo $FILE|grep " " >/dev/null
if [ $? -ne 0 ]
then
echo " There is sapce" #U can run ur script here.
else
echo "No space"
fi
Hope this may help u.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 03:42 AM
08-06-2001 03:42 AM
Re: Shell programming: Search a blank
first you will have to *quote* anyway ;-)
Then, if you are looking for the values of "${FILE}" as
a variable, test it like this:
if [ "${FILE}" = " " ]
then echo "Bingo - a space only"
fi
Or you can insert some (almost) arbitrary characters
for readability:
if [ "x${FILE}x" = "x x" ]
then echo "Bingo - a space only"
fi
But if you are checking wether there is file with the name
in "${FILE}", and this only contains a single space, you
will have to do something like:
if [ "$(wc -c ${FILE})" -eq 1 ]
then if [ "$(cat ${FILE})" = " " ]
then echo Bingo - the files contains only a space
else echo file is only one char in size but not a space
fi
fi
HTH,
Wodisch
PS: You could use "perl", where you have direct access
to the file's attributes, like size, and where file-
handling is pretty simple...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 03:50 AM
08-06-2001 03:50 AM
Re: Shell programming: Search a blank
# The argument for the grep is one blank
echo $fileName ? grep " " > /dev/null
if [[ $? = 0 ]]
then
echo " File name contains at least one blank"
else
echo " File name does not contains at least one blank"
fi
Magdi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2001 10:06 AM
08-06-2001 10:06 AM
Re: Shell programming: Search a blank
if [[ "$file" = *+( )* ]] then
or for other white space character also
if [[ "$file" = *+([:space:])* ]] then
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2001 01:35 AM
08-07-2001 01:35 AM
Re: Shell programming: Search a blank
echo "$file" | awk 'BEGIN {FS=""} / / {print "*" }'
Will print an asterix if $file contains one or more spaces, including leading and trailing spaces.
Hope this helps.
Phil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2001 05:57 AM
08-09-2001 05:57 AM
Re: Shell programming: Search a blank
set -A temp $FILE
[ ${#temp[*]} -gt 0 ] && echo "$FILE has a blank"
All shell builtin commands - no fork/exec
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2001 09:16 AM
08-12-2001 09:16 AM
Re: Shell programming: Search a blank
*' '*) ... ;;
esac
Simple, efficient, works in every sh-type shell from original Bourne up.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2001 04:52 AM
08-14-2001 04:52 AM
Re: Shell programming: Search a blank
[ ${#temp[*]} -gt 1 ] && echo "$FILE has a blank"